google script email spreadsheet as pdf

Solutions on MaxInterview for google script email spreadsheet as pdf by the best coders in the world

showing results for - "google script email spreadsheet as pdf"
Jonathon
06 Mar 2018
1function sendSheetToPdfwithA1MailAdress(){ // this is the function to call
2  var ss = SpreadsheetApp.getActiveSpreadsheet();
3  var sh = ss.getSheets()[0]; // it will send sheet 0 wich is the first sheet in the spreadsheet.
4  // if you change the number, change it also in the parameters below
5  var shName = sh.getName()
6  sendSpreadsheetToPdf(0, shName, sh.getRange('A1').getValue(),"test email with the adress in cell A1 ", "This is it !");
7}
8function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
9  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
10  var spreadsheetId = spreadsheet.getId()  
11  var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;  
12  var url_base = spreadsheet.getUrl().replace(/edit$/,'');
13
14  var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
15
16      + (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId)) 
17      // following parameters are optional...
18      + '&size=A4'      // paper size
19      + '&portrait=true'    // orientation, false for landscape
20      + '&fitw=true'        // fit to width, false for actual size
21      + '&sheetnames=true&printtitle=false&pagenumbers=true'  //hide optional headers and footers
22      + '&gridlines=false'  // hide gridlines
23      + '&fzr=false';       // do not repeat row headers (frozen rows) on each page
24
25  var options = {
26    headers: {
27      'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
28    }
29  }
30
31  var response = UrlFetchApp.fetch(url_base + url_ext, options);
32  var blob = response.getBlob().setName(pdfName + '.pdf');
33  if (email) {
34    var mailOptions = {
35      attachments:blob, htmlBody:htmlbody
36    }
37MailApp.sendEmail(
38      email, 
39      subject+" (" + pdfName +")", 
40      "html content only", 
41      mailOptions);
42
43MailApp.sendEmail(
44      Session.getActiveUser().getEmail(), 
45      "FRWD "+subject+" (" + pdfName +")", 
46      "html content only", 
47      mailOptions);
48  }
49}