我有一个带脚本按钮的Google电子表格,可以将PDF格式的工作表通过电子邮件发送给B5中的电子邮件。这已经运行了将近一年,没有更改任何代码,但是今天突然脚本停止运行,并且出现了一个错误:
“异常:参数(String,String,String,String,(class))与MailApp.sendEmail的方法签名不匹配。”
似乎可能对MailApp.sendEmail调用进行了更新,但我检查了他们的页面,但似乎找不到更新或它们与我的旧代码之间的区别或区别;所以我不确定如何正确更新它。我在此处阅读了关于StackFlow和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 wich 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);
}
}
答案 0 :(得分:0)
您正在为sendEmail
提供五个参数。但是在所有不同的sendEmail
方法中,没有实现接受五个参数:
这就是您得到错误的原因。
我不确定您确切想做什么,但是我猜想您想使用此方法:sendEmail(recipient, subject, body, options),并为{{1 }}:recipient
和email
。
在这种情况下,您应该选择要成为其中一个的收件人,然后删除另一个。
如果要根据是否提供参数Session.getActiveUser().getEmail()
进行选择,可以使用ternary operator:
email