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 = ('[email protected],[email protected]')
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'
});
}
CodePudding user response:
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();
}
});
}