I am trying to create a macro in Google scripts that sends me an email of a worksheet I created. When I try to run this, the script gets stuck at the function GetSheetID(). (4th line of 2nd function)
"TypeError: Cannot read property 'getSheetId' of undefined (line 51, file "macros")"
I am open to other email techniques as well. My main goal is to take a range and send as a picture or PDF in an email.
function sendSheetToPdfwithA1MailAdress(){ // this is the function to call
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheets()[4]; // it will send sheet 0 which is the first sheet in the spreadsheet.
// if you change the number, change it also in the parameters below
var shName = 4 //sh.getName()
var shNum = 4
var shRng = 'A1:R35'
var pdfName = 'Automated Snapshot'
var email = 'email@gmail.com'
var subject = 'Daily Snapshot'
var htmlbody = ''
mailPdf(shNum,shRng,pdfName,email,subject,htmlbody);
}
function mailPdf(shNum,shRng,pdfName,email,subject,htmlbody) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = ss.getId();
var shId = shNum ? ss.getSheets()[shNum].getSheetId() : null;
var url_base = ss.getUrl().replace(/edit$/,'');
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ (shId ? ('&gid=' + shId) : ('&id=' + ssId))
+ (shRng ? ('&range=E1:L25') : null)
+ '&format=pdf' //export format
+ '&size=letter' //A3/A4/A5/B4/B5/letter/tabloid/legal/statement/executive/folio
//+ '&portrait=false' //true= Portrait / false= Landscape
//+ '&scale=1' //1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
//+ '&top_margin=0.00' //All four margins must be set!
//+ '&bottom_margin=0.00' //All four margins must be set!
//+ '&left_margin=0.00' //All four margins must be set!
//+ '&right_margin=0.00' //All four margins must be set!
+ '&gridlines=false' //true/false
//+ '&printnotes=false' //true/false
//+ '&pageorder=2' //1= Down, then over / 2= Over, then down
//+ '&horizontal_alignment=CENTER' //LEFT/CENTER/RIGHT
+ '&vertical_alignment=TOP' //TOP/MIDDLE/BOTTOM
//+ '&printtitle=false' //true/false
//+ '&sheetnames=false' //true/false
//+ '&fzr=false' //true/false frozen rows
//+ '&fzc=false' //true/false frozen cols
//+ '&attachment=false' //true/false
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
'muteHttpExceptions': true
}
}
var response = UrlFetchApp.fetch(url_base + url_ext, options);
var blob = response.getBlob().setName(pdfName + '.pdf');
if (email) {
var mailOptions = {
attachments:blob, htmlBody:htmlbody
}
MailApp.sendEmail(
// email + "," + Session.getActiveUser().getEmail() // use this to email self and others
email, // use this to only email users requested
subject+' (' + pdfName +')',
'html content only',
mailOptions);
}
}
Regarding
"TypeError: Cannot read property 'getSheetId' of undefined (line 51, file "macros")"
It's very likely that you are using base one index instead of a zero based index and that your spreadsheet has less than 5 sheets.
The fix depends on which sheet id do you want to assign to shId
of the following code line:
var shId = shNum ? ss.getSheets()[shNum].getSheetId() : null;
Reference
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.