-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfAppscript.js
More file actions
263 lines (214 loc) · 8.73 KB
/
Copy pathpdfAppscript.js
File metadata and controls
263 lines (214 loc) · 8.73 KB
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
//this is code to print hole spreadsheet pdf
function exportSpreadsheetToPDF() {
var blob, exportUrl, options, pdfFile, response, ss, ssID, url_base;
// Get the active spreadsheet and its ID
ss = SpreadsheetApp.getActiveSpreadsheet();
ssID = ss.getId();
url_base = ss.getUrl().replace(/edit$/, '');
// Export URL to export the whole spreadsheet
exportUrl = url_base + 'export?exportFormat=pdf&format=pdf' +
'&id=' + ssID +
'&size=A4' + // Paper size
'&portrait=true' + // Orientation, false for landscape
'&fitw=true' + // Fit to width
'&sheetnames=true&printtitle=false&pagenumbers=true' + // Header and footer settings
'&gridlines=false' + // Hide gridlines
'&fzr=false'; // Do not repeat frozen rows on each page
options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
},
muteHttpExceptions: true
};
try {
response = UrlFetchApp.fetch(exportUrl, options);
if (response.getResponseCode() !== 200) {
Logger.log("Error exporting spreadsheet to PDF. Response Code: " + response.getResponseCode());
return;
}
blob = response.getBlob();
blob.setName('Complete_Spreadsheet_Export.pdf');
// Save the PDF file in Google Drive
pdfFile = DriveApp.createFile(blob);
Logger.log('PDF file created: ' + pdfFile.getId());
} catch (e) {
Logger.log("Error: " + e.message);
}
}
// and this generate pdf for only one sheet
function exportRangeToPDf(range) {
var blob,exportUrl,options,pdfFile,response,sheetTabNameToGet,sheetTabId,ss,ssID,url_base;
range = range ? range : "C4:D4";//Set the default to whatever you want
sheetTabNameToGet = "Sheet1";//Replace the name with the sheet tab name for your situation
ss = SpreadsheetApp.getActiveSpreadsheet();//This assumes that the Apps Script project is bound to a G-Sheet
ssID = ss.getId();
sh = ss.getSheetByName(sheetTabNameToGet);
sheetTabId = sh.getSheetId();
url_base = ss.getUrl().replace(/edit$/,'');
//Logger.log('url_base: ' + url_base)
exportUrl = url_base + 'export?exportFormat=pdf&format=pdf' +
'&gid=' + sheetTabId + '&id=' + ssID +
'&range=' + range +
//'&range=NamedRange +
'&size=A4' + // paper size
'&portrait=true' + // orientation, false for landscape
'&fitw=true' + // fit to width, false for actual size
'&sheetnames=true&printtitle=false&pagenumbers=true' + //hide optional headers and footers
'&gridlines=false' + // hide gridlines
'&fzr=false'; // do not repeat row headers (frozen rows) on each page
//Logger.log('exportUrl: ' + exportUrl)
options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
}
}
options.muteHttpExceptions = true;//Make sure this is always set
response = UrlFetchApp.fetch(exportUrl, options);
//Logger.log(response.getResponseCode())
if (response.getResponseCode() !== 200) {
console.log("Error exporting Sheet to PDF! Response Code: " + response.getResponseCode());
return;
}
blob = response.getBlob();
blob.setName('AAA_test.pdf')
pdfFile = DriveApp.createFile(blob);//Create the PDF file
//Logger.log('pdfFile ID: ' +pdfFile.getId())
}
// and this for gnerate more and seperately for every sheet
function exportRangeToPDF(range) {
var blob, exportUrl, options, pdfFile, response, ss, ssID, url_base;
// Set default range if not provided
range = range || "A1:Z100";
// List of sheet names to export
var sheetTabNameToGet = ["Sheet1", "Sheet2"]; // Replace with your sheet tab names
ss = SpreadsheetApp.getActiveSpreadsheet(); // Assume this Apps Script is bound to a spreadsheet
ssID = ss.getId();
url_base = ss.getUrl().replace(/edit$/, '');
// Iterate through each sheet
for (var i = 0; i < sheetTabNameToGet.length; i++) {
var sh = ss.getSheetByName(sheetTabNameToGet[i]);
if (!sh) {
Logger.log("Sheet not found: " + sheetTabNameToGet[i]);
continue; // Skip to the next sheet if not found
}
var sheetTabId = sh.getSheetId();
// Construct export URL
exportUrl =
url_base +
"export?exportFormat=pdf&format=pdf" +
"&gid=" +
sheetTabId +
"&id=" +
ssID +
"&size=A4" + // Paper size
"&portrait=true" + // Orientation: true for portrait, false for landscape
"&fitw=true" + // Fit to width
"&sheetnames=true&printtitle=false&pagenumbers=true" + // Optional headers/footers
"&gridlines=false" + // Hide gridlines
"&fzr=false"; // Do not repeat frozen rows
Logger.log("Export URL: " + exportUrl);
// Set up request options
options = {
headers: {
Authorization: "Bearer " + ScriptApp.getOAuthToken(),
},
muteHttpExceptions: true, // Ensure all responses are caught
};
// Fetch the file
response = UrlFetchApp.fetch(exportUrl, options);
// Handle response
if (response.getResponseCode() !== 200) {
Logger.log("Error exporting sheet: " + sheetTabNameToGet[i]);
continue; // Skip to the next sheet on error
}
// Save the response as a PDF
blob = response.getBlob().setName(sheetTabNameToGet[i] + "_Export.pdf");
pdfFile = DriveApp.createFile(blob);
Logger.log("PDF created for sheet: " + sheetTabNameToGet[i] + ", File ID: " + pdfFile.getId());
}
}
// and this for combined pdf and download into local computer
function generatePDF() {
var blob, exportUrl, options, response, ss, ssID, url_base;
// Get the active spreadsheet and its ID
ss = SpreadsheetApp.getActiveSpreadsheet();
ssID = ss.getId();
url_base = ss.getUrl().replace(/edit$/, '');
// Export URL to export the whole spreadsheet
exportUrl = url_base + 'export?exportFormat=pdf&format=pdf' +
'&id=' + ssID +
'&size=A4' + // Paper size
'&portrait=true' + // Orientation, false for landscape
'&fitw=true' + // Fit to width
'&sheetnames=true&printtitle=false&pagenumbers=true' + // Header and footer settings
'&gridlines=false' + // Hide gridlines
'&fzr=false'; // Do not repeat frozen rows on each page
options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
},
muteHttpExceptions: true
};
try {
response = UrlFetchApp.fetch(exportUrl, options);
if (response.getResponseCode() !== 200) {
return ContentService.createTextOutput("Error exporting spreadsheet to PDF. Response Code: " + response.getResponseCode());
}
blob = response.getBlob();
blob.setName('Complete_Spreadsheet_Export.pdf');
// Save the PDF file in Google Drive
var pdfFile = DriveApp.createFile(blob);
// Generate download link
var downloadUrl = 'https://drive.google.com/uc?export=download&id=' + pdfFile.getId();
return downloadUrl;
} catch (e) {
return "Error: " + e.message;
}
}
// document.getElementById('generate-pdf').addEventListener('click', function() {
// google.script.run.withSuccessHandler(openPdf).generatePDF();
// });
// function openPdf(downloadUrl) {
// console.log(downloadUrl)
// window.open(downloadUrl, '_blank');
// }
// this the prnt pdf for first sheet and downlod in local computer
function exportFirstSheetToPDF() {
var blob, exportUrl, options, response, ss, ssID, url_base, sheetTabNameToGet, sheetTabId, pdfFile;
// Get the active spreadsheet and its ID
ss = SpreadsheetApp.getActiveSpreadsheet();
ssID = ss.getId();
sheetTabNameToGet = ss.getSheets()[0].getName(); // Get the name of the first sheet
sheetTabId = ss.getSheets()[0].getSheetId(); // Get the sheet ID of the first sheet
url_base = ss.getUrl().replace(/edit$/, '');
// Export URL to export only the first sheet as PDF
exportUrl = url_base + 'export?exportFormat=pdf&format=pdf' +
'&gid=' + sheetTabId + '&id=' + ssID +
'&size=A4' + // Paper size
'&portrait=true' + // Orientation, false for landscape
'&fitw=true' + // Fit to width
'&sheetnames=true&printtitle=false&pagenumbers=true' + // Header and footer settings
'&gridlines=false' + // Hide gridlines
'&fzr=false'; // Do not repeat frozen rows on each page
options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
},
muteHttpExceptions: true
};
try {
response = UrlFetchApp.fetch(exportUrl, options);
if (response.getResponseCode() !== 200) {
return ContentService.createTextOutput("Error exporting first sheet to PDF. Response Code: " + response.getResponseCode());
}
blob = response.getBlob();
blob.setName(sheetTabNameToGet + '_Export.pdf');
// Save the PDF file in Google Drive
pdfFile = DriveApp.createFile(blob);
// Generate download link
var downloadUrl = 'https://drive.google.com/uc?export=download&id=' + pdfFile.getId();
return downloadUrl;
} catch (e) {
return "Error: " + e.message;
}
}