我有一个带有脚本按钮的 Google 电子表格,可以将表单以 PDF 格式通过电子邮件发送到 B5 中的电子邮件。这已经运行了将近一年,没有更改代码,然后今天突然脚本停止工作,我们遇到了错误:
“例外:参数(字符串、字符串、字符串、字符串、(类))与 MailApp.sendEmail 的方法签名不匹配。”
似乎 MailApp.sendEmail 调用可能已经更新,但我检查了他们的页面,我似乎无法找到他们需要的内容和我的旧代码之间的更新或区别;所以我不确定如何正确更新它。我阅读了 Stack Overflow 和 Reddit 上的几篇帖子,其中人们遇到了类似的问题,但略有不同,我无法找到解决方案。有人可以帮帮我吗?
链接到 MailApp.sendEmail 调用: https ://developers.google.com/apps-script/reference/mail/mail-app
这是我的代码:
//EMAIL SHEET AS PDF
//Type First Last name into B4, must be spelled correctly for the query to pull records into the sheet.
//Enter Email Address into B5 separated by a comma
//Select Email Button
function emailPDFofTraining(){ // this is the function to call
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheets()[0]; // 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 = sh.getName()
sendSpreadsheetToPdf(0, shName, sh.getRange('B4').getValue(),"Mountain Training");
}
function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId()
var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;
var url_base = spreadsheet.getUrl().replace(/edit$/,'');
var name = SpreadsheetApp.getActiveSheet().getRange('B3').getValue();
var date = SpreadsheetApp.getActiveSheet().getRange('B6').getValue();
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId))
// following parameters are optional...
+ '&size=A4' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=true&printtitle=false&pagenumbers=true' //hide optional headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false'; // do not repeat row headers (frozen rows) on each page
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
}
}
var response = UrlFetchApp.fetch(url_base + url_ext, options);
var blob = response.getBlob().setName(pdfName + '-' + name + '.pdf');
if (email) {
var mailOptions = {
attachments:blob, htmlBody:htmlbody
}
MailApp.sendEmail(
email, Session.getActiveUser().getEmail(),
subject+ pdfName + '-' + name + ".pdf')",
"Mountain Training Certification is attached for "+name+". .or support, contact the Mountain Project Manager or reply to this email.",
mailOptions);
}
}