0

I'm currently using the script bellow to export and send a workbook as an email attachment. Is it possible to send multiple sheets as one attachment instead of the whole workbook?

function emailauto() { 
var date = Utilities.formatDate(new Date(), 'America/Santiago', 'yyyy-MM-dd HH:mm:ss')
var emaila = ('email1@test.com,email2@test.com')
var url = "https://docs.google.com/spreadsheets/d/ + "FileID" + /export?format=xlsx&access_token=" + ScriptApp.getOAuthToken();
var file = UrlFetchApp.fetch(url).getBlob().setName("Filename_" + date + ".xlsx");

GmailApp.sendEmail(emaila, 'Test', 'Test date: ' + date + '.\n\nRegards.', {
    attachments: [file],
    name: 'Tester'
});
}
Ghw-ghw
  • 3
  • 2
  • see https://stackoverflow.com/a/56261802/555121 https://stackoverflow.com/a/30492812/555121 – Kos Nov 08 '21 at 16:42
  • Ty for the answer, it's not was I was looking for but I got an idea from it. The first link shows how to attach each sheet as a separate file unless it's hidden and the second link only shows the option to send one sheet or all sheets. Thanks to the first link I realized that since I don't really care if the sheets are actually there or not, I can just hide them before sending the email. – Ghw-ghw Nov 08 '21 at 18:06

1 Answers1

0

Emails whatever sheets you wish from a spreadsheet as one multipage pdf

Returns sheets to their initial location.

function emailTheSheetsYouWishAsPDFAttachment() {
  const ss=SpreadsheetApp.openById("ssid");
  const incl=['Sheet2','Sheet1'];
  const pdfFolderId="Folder Id";
  const folder=DriveApp.getFolderById("pdffolderid");
  const sObj={shA:[]};
  const shts=ss.getSheets();
  shts.forEach(function(sh,i){
    sObj.shA.push(sh.getName());
    if(sh.isSheetHidden()) {
      sObj[sh.getName()]='hide';
    }else{
      sObj[sh.getName()]='show';
    }
    if(~incl.indexOf(sh.getName())) {
      sh.showSheet();
    }else{
      sh.hideSheet();
    }
  });
  const file=folder.createFile(ss.getBlob()).setName(ss.getName()).getAs(MimeType.PDF);
  const sh=ss.getSheetByName('Emails');//emails info sheet
  const rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
  const hA=sh.getRange(1,1,1,sh.getLastColumn()).getValues()[0];
  let hObj={};
  hA.forEach(function(e,i){if(e){hObj[e]=i;}})
  const vA=rg.getValues();
  vA.forEach(function(r,i){
    let recipient=r[hObj.recipient];
    let subject=r[hObj.subject];
    let message=r[hObj.message];
    GmailApp.sendEmail(recipient, subject, message, {attachments:[file]})
  })
  sObj.shA.forEach(function(name,i){
    if(sObj[name]=='hide') {
      ss.getSheetByName(name).hideSheet();
    }else{
      ss.getSheetByName(name).showSheet();
    }
  });
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Ty for the answer. Is converting the workbook to a PDF the only option or can it also work as a xlsx? – Ghw-ghw Nov 08 '21 at 18:10
  • Honestly I don't use this sort of thing too much myself. And I have no interest in Excel. If google sheets could run on my pc I wouldn't even have a copy of Excel – Cooper Nov 08 '21 at 18:20