├── README.md └── email.gs /README.md: -------------------------------------------------------------------------------- 1 | # GA-metrics-scripts 2 | 3 | Collection of useful tools to analyse Google Analytics 4 | 5 | **email.gs** is a Google App Script that can be used in Google Sheets to turn the first tab of a sheet into a PDF and email it to selected users. 6 | 7 | The script can be triggered manually or by scheduling a trigger. 8 | 9 | The script is explain in detail at [to follow] 10 | -------------------------------------------------------------------------------- /email.gs: -------------------------------------------------------------------------------- 1 | /* Function 1: creates a Menu when the script loads */ 2 | 3 | function onOpen() { 4 | var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); 5 | // Adds a menu item with a single drop-down 'Email report' 6 | activeSpreadsheet.addMenu( 7 | "Email this report", [{ 8 | name: "Email report", functionName: "emailAsPDF" 9 | }]); 10 | } 11 | 12 | /* Function 2: sends Spreadsheet in an email as a PDF */ 13 | 14 | // reworked from ctrlq.org/code/19869-email-google-spreadsheets-pdf // 15 | 16 | function emailAsPDF() { 17 | 18 | // Send the PDF of the spreadsheet to this email address 19 | var email = "someone@somewhere.gov.uk,someone@elsewhere.gov.uk"; 20 | 21 | // Gets the URL of the currently active spreadsheet 22 | var ss = SpreadsheetApp.getActiveSpreadsheet(); 23 | var url = ss.getUrl(); 24 | url = url.replace(/edit$/,''); 25 | 26 | // Subject of email message 27 | // The date time string can be formatted using Utilities.formatDate method 28 | // see examples at https://developers.google.com/apps-script/reference/utilities/utilities#formatdatedate-timezone-format 29 | // and http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 30 | var subject = "Metrics mailing - " + Utilities.formatDate(new Date(), "GMT", "dd-MMM-yyyy"); 31 | 32 | // Body of email message 33 | var body = "\n\nHello\n\nThis is a mailing of a Google Sheet.\n \n"; 34 | 35 | /* Specify PDF export parameters 36 | // Taken from: code.google.com/p/google-apps-script-issues/issues/detail?id=3579 37 | exportFormat = pdf / csv / xls / xlsx 38 | gridlines = true / false 39 | printtitle = true (1) / false (0) 40 | size = A4 / letter /legal 41 | fzr (repeat frozen rows) = true / false 42 | portrait = true (1) / false (0) 43 | fitw (fit to page width) = true (1) / false (0) 44 | add gid if to export a particular sheet - 0, 1, 2,.. 45 | */ 46 | 47 | var url_ext = 'export?exportFormat=pdf' // export as pdf 48 | + '&format=pdf' // export as pdf 49 | + '&size=A4' // paper size 50 | + '&portrait=true' // page orientation 51 | + '&fitw=true' // fits width; false for actual size 52 | + '&sheetnames=false' // hide optional headers and footers 53 | + '&printtitle=false' // hide optional headers and footers 54 | + '&pagenumbers=false' // hide page numbers 55 | + '&gridlines=false' // hide gridlines 56 | + '&fzr=false' // do not repeat row headers 57 | + '&gid=0'; // the sheet's Id 58 | 59 | var token = ScriptApp.getOAuthToken(); 60 | 61 | // Convert worksheet to PDF 62 | var response = UrlFetchApp.fetch(url + url_ext) 63 | 64 | //convert the response to a blob 65 | file = response.getBlob().setName('mailing.pdf'); 66 | 67 | // Send the email with the PDF attachment. Google sets limits on the number of emails you can send: https://docs.google.com/macros/dashboard 68 | if (MailApp.getRemainingDailyQuota() > 0) 69 | GmailApp.sendEmail(email, subject, body, {attachments:[file]}); 70 | 71 | } 72 | --------------------------------------------------------------------------------