-
Notifications
You must be signed in to change notification settings - Fork 4
/
email.gs
71 lines (56 loc) · 2.87 KB
/
email.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
/* Function 1: creates a Menu when the script loads */
function onOpen() {
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Adds a menu item with a single drop-down 'Email report'
activeSpreadsheet.addMenu(
"Email this report", [{
name: "Email report", functionName: "emailAsPDF"
}]);
}
/* Function 2: sends Spreadsheet in an email as a PDF */
// reworked from ctrlq.org/code/19869-email-google-spreadsheets-pdf //
function emailAsPDF() {
// Send the PDF of the spreadsheet to this email address
var email = "[email protected],[email protected]";
// Gets the URL of the currently active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var url = ss.getUrl();
url = url.replace(/edit$/,'');
// Subject of email message
// The date time string can be formatted using Utilities.formatDate method
// see examples at https://developers.google.com/apps-script/reference/utilities/utilities#formatdatedate-timezone-format
// and http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
var subject = "Metrics mailing - " + Utilities.formatDate(new Date(), "GMT", "dd-MMM-yyyy");
// Body of email message
var body = "\n\nHello\n\nThis is a mailing of a Google Sheet.\n \n";
/* Specify PDF export parameters
// Taken from: code.google.com/p/google-apps-script-issues/issues/detail?id=3579
exportFormat = pdf / csv / xls / xlsx
gridlines = true / false
printtitle = true (1) / false (0)
size = A4 / letter /legal
fzr (repeat frozen rows) = true / false
portrait = true (1) / false (0)
fitw (fit to page width) = true (1) / false (0)
add gid if to export a particular sheet - 0, 1, 2,..
*/
var url_ext = 'export?exportFormat=pdf' // export as pdf
+ '&format=pdf' // export as pdf
+ '&size=A4' // paper size
+ '&portrait=true' // page orientation
+ '&fitw=true' // fits width; false for actual size
+ '&sheetnames=false' // hide optional headers and footers
+ '&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false' // hide page numbers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false' // do not repeat row headers
+ '&gid=0'; // the sheet's Id
var token = ScriptApp.getOAuthToken();
// Convert worksheet to PDF
var response = UrlFetchApp.fetch(url + url_ext)
//convert the response to a blob
file = response.getBlob().setName('mailing.pdf');
// Send the email with the PDF attachment. Google sets limits on the number of emails you can send: https://docs.google.com/macros/dashboard
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {attachments:[file]});
}