mrnavaone

6

May 9th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function spreadsheetToPdf() {
  3.  
  4.   const ss = SpreadsheetApp.getActiveSpreadsheet();
  5.   const subject = `PDF Generado por ${ss.getName()}`;
  6.   const body = "Enviado con [Email SpreadSheet] Reporte de Notas";
  7.   const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId());
  8.   var contacts = ss.getSheetByName("contacto");
  9.   var numRows = contacts.getLastRow();
  10.   const email = contacts.getRange(1, 1, numRows, 1).getValues();
  11.   const exportOptions =
  12.     'exportFormat=pdf&format=pdf' + // export as pdf / csv / xls / xlsx
  13.     '&size=letter' + // paper size legal / letter / A4
  14.     '&portrait=false' + // orientation, false for landscape
  15.     '&fitw=true&source=tuto' + // fit to page width, false for actual size
  16.     '&sheetnames=false&printtitle=false' + // hide optional headers and footers
  17.     '&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
  18.     '&fzr=false' + // do not repeat row headers (frozen rows) on each page
  19.     '&gid='; // the sheet's Id
  20.  
  21.   const token = ScriptApp.getOAuthToken();
  22.   const sheets = ss.getSheets();
  23.   const blobs = [];
  24.  
  25.   for (let i = 0; i < sheets.length; i += 1) {
  26.     const response = UrlFetchApp.fetch(url + exportOptions + sheets[i].getSheetId(), {
  27.       headers: {
  28.         Authorization: `Bearer ${token}`
  29.       }
  30.     });
  31.  
  32.  
  33.     blobs[i] = response.getBlob().setName(`${sheets[i].getName()}.pdf`);
  34.   }
  35.  
  36.  
  37.   const zipBlob = Utilities.zip(blobs).setName(`${ss.getName()}.zip`);
  38.  
  39.  
  40.   DriveApp.createFile(zipBlob);
  41.  
  42.   Logger.log(`Storage Space used: ${DriveApp.getStorageUsed()}`);
  43.  
  44.   if (MailApp.getRemainingDailyQuota() > 0)
  45.     GmailApp.sendEmail(email, subject, body, {
  46.       htmlBody: body,
  47.       attachments: [zipBlob]
  48.     });
  49. }
Add Comment
Please, Sign In to add comment