-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.gs
79 lines (72 loc) · 2.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
const exportPDFBlob = () => {
const ssID = 'spreadsheet id';
const sID = 'sheet id';
const print_options = [
['gid', sID, ], // Sheet ID (omit for all sheets as pages)
['format', 'pdf' ], // File type
['size', 7 ], // Paper size
['portrait', true ], // Page Orientation
['scale', 1 ], // Scale (Integer 1-4, see README)
['top_margin', 0.75 ], // Margin in inches (all four are required)
['right_margin', 0.25 ], //
['bottom_margin', 0.75 ], //
['left_margin', 0.25 ], //
['gridlines', false ], // Show gridlines
['printnotes', false ], // Show notes
['horizontal_alignment', 'CENTER' ], // Horizontal alignment
['vertical_alignment', 'TOP' ], // Vertical alignment
['pagenum', 'UNDEFINED' ], // Include page numbers
['printtitle', false ], // Include Workbook title
['sheetnames', false ], // Include Sheet name
['fzr', false ], // Repeat frozen rows on each page
['fzc', false ], // Repeat frozen columns on each page
['r1', 0 ], // Selection to print, start reference
['c1', 0 ], //
['r2', 100 ], // Selection to print, end reference
['c2', 10 ], //
['attachment', true ] // Uncertain, true works
];
// URL Params
let url_params = '?';
for (p in print_options) {
url_params = (
`${url_params}` +
`${print_options[p][0]}=${print_options[p][1]}` +
`${(p == (print_options.length - 1)) ? '' : '&'}`
);
}
let url = (
`https://docs.google.com/spreadsheets/d/${ssID}/export${url_params}`
);
// Export PDF to Blob
let options = {
method: 'GET',
headers: {
'Authorization': `Bearer ${ScriptApp.getOAuthToken()}`
}
};
let blob = UrlFetchApp.fetch(url, options).getBlob();
return blob
}
const emailPDF = (blob) => {
let filename = 'pdf_exported_from_sheets';
// Send Blob as File Attached to Email
let email_options = {
attachments: [{
fileName: `${filename}.pdf`,
content: blob.getBytes(),
mimeType: "application/pdf"
}]
};
MailApp.sendEmail(
'supersim65@gmail.com',
'Email Subject',
'Email Body',
email_options
);
}
const savePDFToDrive = (blob) => {
let filename = 'pdf_exported_from_sheets';
// Save Blob as File to Google Drive
DriveApp.createFile(blob.setName(`${filename}.pdf`));
}