-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.gs
333 lines (294 loc) · 10.7 KB
/
Code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// ____ _ _
// / __ \ | | (_)
// | | | |_ __ | |_ _ ___ _ __ ___
// | | | | '_ \| __| |/ _ \| '_ \/ __|
// | |__| | |_) | |_| | (_) | | | \__ \
// \____/| .__/ \__|_|\___/|_| |_|___/
// | |
// |_|
var SaveRangeAsImageSettings = {
folder_id: '',
subfolder_name: 'Range2Image', // creates subfolder if set
save2drive: true,
measure_limit: 150, // script will assume all other rows/columns has the same size
size_limit: 1100, // the max. number of rows/columns,
}
// _ _
// /\ | | | |
// / \ | |__ ___ _ _| |_
// / /\ \ | '_ \ / _ \| | | | __|
// / ____ \| |_) | (_) | |_| | |_
// /_/ \_\_.__/ \___/ \__,_|\__|
// makhrov.max@gmail.com
// https://twitter.com/max__makhrov
// MIT
// https://github.com/Max-Makhrov/range2image
// __ __
// | \/ |
// | \ / | ___ _ __ _ _
// | |\/| |/ _ \ '_ \| | | |
// | | | | __/ | | | |_| |
// |_| |_|\___|_| |_|\__,_|
// Use this code for Google Docs, Slides, Forms, or Sheets.
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('🖼️ Range2Image Menu')
.addItem("Convert selection to image", "convertSelection2Image")
.addToUi();
}
// __ __ _
// | \/ | (_)
// | \ / | __ _ _ _ __
// | |\/| |/ _` | | '_ \
// | | | | (_| | | | | |
// |_| |_|\__,_|_|_| |_|
function convertSelection2Image() {
var sets = SaveRangeAsImageSettings;
var file = SpreadsheetApp.getActive();
var range = SpreadsheetApp.getActiveRange();
var file_name = file.getName() + '_' +
range.getSheet().getName() + '_' +
range.getA1Notation();
var url = getPdfPrintUrl_(range, sets);
var blob = UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}}).getBlob();
var imageBlob = convertPDFToPNG_(blob);
// As a sample, create PNG images as PNG files.
var folderId = getFolderByNameCreateIfNotExists_(sets.subfolder_name, sets.folder_id);
createPngInFolder_(imageBlob, file_name, folderId);
}
// _____ ___ _____ _ __
// | __ \ |__ \| __ \ | |/ _|
// | |__) |__ _ _ __ __ _ ___ ) | |__) |_| | |_
// | _ // _` | '_ \ / _` |/ _ \ / /| ___/ _` | _|
// | | \ \ (_| | | | | (_| | __// /_| | | (_| | |
// |_| \_\__,_|_| |_|\__, |\___|____|_| \__,_|_|
// __/ |
// |___/
/**
* converts range to Url
* ready to be saved as PDF
*
* More info here:a
* https://stackoverflow.com/questions/46088042
* https://gist.github.com/Spencer-Easton/78f9867a691e549c9c70
* https://kandiral.ru/googlescript/eksport_tablic_google_sheets_v_pdf_fajl.html
*
* @param {int} options.size_limit 1100 rows/columns (in tests)
* @param {int} options.measure_limit 150 rows/columns
*/
function getPdfPrintUrl_(range, options) {
var ratio = 96; // get inch from pixel
range = range || SpreadsheetApp.getActiveRange();
var sheet = range.getSheet();
var file = SpreadsheetApp.getActive();
var fileurl = file.getUrl();
var sheetid = sheet.getSheetId();
var rownum = range.getRow();
var columnnum = range.getColumn();
var rownum2 = range.getLastRow();
var columnnum2 = range.getLastColumn();
if ((rownum2-rownum+1) > options.size_limit) {
throw '😢The range exceeded the limit of ' + options.size_limit + ' rows';
}
if ((columnnum2-columnnum+1) > options.size_limit) {
throw '😢The range exceeded the limit of ' + options.size_limit + ' columns';
}
file.toast('Please wait...', '📐Measuring Range...');
// get width in pixels
var w = 0, size;
for (var i = columnnum; i <= columnnum2; i++) {
if (i <= options.measure_limit) {
size = sheet.getColumnWidth(i);
}
w += size;
if ((i % 50) === 0 && i <= options.measure_limit) {
file.toast(
'Done ' + i + ' columns of ' + columnnum2,
'↔📐Measuring width...');
}
}
if (i > options.measure_limit) {
file.toast(
'Estimation: all other columns are the same size',
'↔📐Measuring width...');
}
// get row height in pixels
var h = 0;
for (var i = rownum; i <= rownum2; i++) {
if (i <= options.measure_limit) {
size = sheet.getRowHeight(i);
}
h += size
/** manual correction */
if (size === 2) {
h-=1;
} else {
// h -= 0.42; /** TODO → test the range to make it fit any range */
}
if ((i % 50) === 0 && i <= options.measure_limit) {
file.toast(
'Done ' + i + ' rows of ' + rownum2,
'↕📐Measuring height...');
}
}
if (i > options.measure_limit) {
file.toast(
'Estimation: all other rows are the same size',
'↕📐Measuring height...');
}
// add 0.1 inch to fit some ranges
var hh = Math.round(h/ratio * 1000 + 100) / 1000;
var ww = Math.round(w/ratio * 1000 + 100) / 1000;
var sets = {
url: fileurl,
sheetId: sheetid,
r1: rownum-1,
r2: rownum2,
c1: columnnum-1,
c2: columnnum2,
size: ww +'x' + hh, //A3/A4/A5/B4/B5/letter/tabloid/legal/statement/executive/folio
// portrait: true, //true= Potrait / false= Landscape
scale: 2, //1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
top_margin: 0, //All four margins must be set!
bottom_margin: 0, //All four margins must be set!
left_margin: 0, //All four margins must be set!
right_margin: 0, //All four margins must be set!
}
var rangeParam =
'&r1=' + sets.r1 +
'&r2=' + sets.r2 +
'&c1=' + sets.c1 +
'&c2=' + sets.c2;
var sheetParam = '&gid=' + sets.sheetId;
var isPortrait = '';
if (sets.portrait) {
//true= Potrait / false= Landscape
isPortrait = '&portrait=' + sets.portrait;
}
var exportUrl = sets.url.replace(/\/edit.*$/, '')
+ '/export?exportFormat=pdf&format=pdf'
+ '&size=' + sets.size //A3/A4/A5/B4/B5/letter/tabloid/legal/statement/executive/folio
+ isPortrait
+ '&scale=' + sets.scale //1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
+ '&top_margin=' + sets.top_margin //All four margins must be set!
+ '&bottom_margin=' + sets.bottom_margin //All four margins must be set!
+ '&left_margin=' + sets.left_margin //All four margins must be set!
+ '&right_margin=' + sets.right_margin //All four margins must be set!
+ '&sheetnames=false&printtitle=false'
+ '&pagenum=UNDEFINED' // change it to CENTER to print page numbers
+ 'horizontal_alignment=LEFT' // //LEFT/CENTER/RIGHT
+ '&gridlines=false'
+ "&fmcmd=12"
+ '&fzr=FALSE'
+ sheetParam
+ rangeParam;
return exportUrl;
}
// _____ _ __ ___ _____
// | __ \ | |/ _|__ \| __ \
// | |__) |_| | |_ ) | |__) | __ __ _
// | ___/ _` | _| / /| ___/ '_ \ / _` |
// | | | (_| | | / /_| | | | | | (_| |
// |_| \__,_|_| |____|_| |_| |_|\__, |
// __/ |
// |___/
/**
* https://stackoverflow.com/a/55152707/5372400
* https://stackoverflow.com/questions/75315605
*
* @param {Blob} blob
*
* @returns {Blob}
*/
function convertPDFToPNG_(blob) {
var fileId = createFileInFolder_("TMP_DELETE.pdf", blob, "application/pdf");
var link = null;
var maxTries = 5;
var tries = 1;
while (!link) {
if (tries > maxTries) {
throw new Error("Could not get link in " + maxTries + " seconds");
}
Utilities.sleep(1000);
link = Drive.Files.get(fileId, { fields: "thumbnailLink" }).thumbnailLink;
tries++;
}
var thumbnailURL = link.replace(/=s.+/, "=s2500");
console.log(thumbnailURL);
var pngBlob = UrlFetchApp.fetch(thumbnailURL).getBlob();
Drive.Files.remove(fileId);
return pngBlob;
}
// ______ _ _ ____ _ _
// | ____| | | | | | _ \ | \ | |
// | |__ ___ | | __| | ___ _ __| |_) |_ _| \| | __ _ _ __ ___ ___
// | __/ _ \| |/ _` |/ _ \ '__| _ <| | | | . ` |/ _` | '_ ` _ \ / _ \
// | | | (_) | | (_| | __/ | | |_) | |_| | |\ | (_| | | | | | | __/
// |_| \___/|_|\__,_|\___|_| |____/ \__, |_| \_|\__,_|_| |_| |_|\___|
// __/ |
// |___/
/**
* @prop {String} folderName
* @prop {String} [parentFolderId]
*
* @returns {String} folderId
*/
function getFolderByNameCreateIfNotExists_(folderName, parentFolderId) {
if (parentFolderId === "") parentFolderId = null;
var mimeTypeStr = 'application/vnd.google-apps.folder';
var q = "name = '" + folderName + "' and mimeType = '" + mimeTypeStr + "'";
var parentId = parentFolderId || "root";
q += " and ('" + parentId + "' in parents)";
var searchFolders = Drive.Files.list({q: q})
if (searchFolders.files) {
if (searchFolders.files.length) {
return searchFolders.files[0].id;
}
}
var newFolder = {
name: folderName,
mimeType: mimeTypeStr
};
if (parentFolderId) {
newFolder.parents = [parentFolderId];
}
var folder = Drive.Files.create(newFolder);
return folder.id;
}
// _____ _______ ______ _ _
// | __ \ |__ __| | ____| | | | |
// | |__) | __ __ _| | ___ | |__ ___ | | __| | ___ _ __
// | ___/ '_ \ / _` | |/ _ \| __/ _ \| |/ _` |/ _ \ '__|
// | | | | | | (_| | | (_) | | | (_) | | (_| | __/ |
// |_| |_| |_|\__, |_|\___/|_| \___/|_|\__,_|\___|_|
// __/ |
// |___/
/**
* @param {Blob} blob
* @param {String} fileName
* @param {String} folderId
*/
function createPngInFolder_(blob, fileName, folderId) {
mimeType = "image/x-png";
createFileInFolder_(fileName, blob, mimeType, folderId);
}
/**
* https://developers.google.com/drive/api/reference/rest/v3/files#File
* @param {String} fileName
* @param {Blob} blob
* @param {String} mimeType
* @param {String} [folderId]
*
* @returns {String} id
*/
function createFileInFolder_(fileName, blob, mimeType, folderId) {
var file = {
mimeType: mimeType,
name: fileName
};
if (folderId && folderId !== "") {
file.parents = [ folderId ];
}
var file = Drive.Files.create(file, blob);
return file.id;
}