Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function spreadsheetToPdf() {
- const ss = SpreadsheetApp.getActiveSpreadsheet();
- const subject = `PDF Generado por ${ss.getName()}`;
- const body = "Enviado con [Email SpreadSheet] Reporte de Notas";
- const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId());
- var contacts = ss.getSheetByName("contacto");
- var numRows = contacts.getLastRow();
- const email = contacts.getRange(1, 1, numRows, 1).getValues();
- const exportOptions =
- 'exportFormat=pdf&format=pdf' + // export as pdf / csv / xls / xlsx
- '&size=letter' + // paper size legal / letter / A4
- '&portrait=false' + // orientation, false for landscape
- '&fitw=true&source=tuto' + // fit to page width, false for actual size
- '&sheetnames=false&printtitle=false' + // hide optional headers and footers
- '&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
- '&fzr=false' + // do not repeat row headers (frozen rows) on each page
- '&gid='; // the sheet's Id
- const token = ScriptApp.getOAuthToken();
- const sheets = ss.getSheets();
- const blobs = [];
- for (let i = 0; i < sheets.length; i += 1) {
- const response = UrlFetchApp.fetch(url + exportOptions + sheets[i].getSheetId(), {
- headers: {
- Authorization: `Bearer ${token}`
- }
- });
- blobs[i] = response.getBlob().setName(`${sheets[i].getName()}.pdf`);
- }
- const zipBlob = Utilities.zip(blobs).setName(`${ss.getName()}.zip`);
- DriveApp.createFile(zipBlob);
- Logger.log(`Storage Space used: ${DriveApp.getStorageUsed()}`);
- if (MailApp.getRemainingDailyQuota() > 0)
- GmailApp.sendEmail(email, subject, body, {
- htmlBody: body,
- attachments: [zipBlob]
- });
- }
Add Comment
Please, Sign In to add comment