【发布时间】:2020-07-01 20:16:08
【问题描述】:
我有一个工作流程,用户可以在其中填写 Google 表单,然后关联的 Google 表格信息填充 Google Doc 并发送电子邮件。 该脚本在我的所有测试中都可以正常运行,但是当我尝试设置可安装触发器时,它会一次又一次地失败。
function CreatePro2() {
// specify doc template and get values from spread
var sleepINT = 1500
var templateid = "XXXXXXXXXXXXXXXXXX"; // template file id
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.toast("ENGINE INITIALIZING & Feeding the unicorns");
Utilities.sleep(sleepINT);
var sheet = ss.getActiveSheet();
var data = sheet.getRange(2,1,1,23).getValues(); // starting with row 2 and column 1 as our upper-left most column,
// get values from cells from 1 row down, and 15 columns along - hence (2,1,1,15)
//sheet.getRange("F7").setValue('=IMAGE("https://s-media-cache-ak0.pinimg.com/564x/58/5d/8f/585d8f802867c25df8f1ecc0cf7cadc8.jpg",1)');
ss.toast("10%: data captured");
Utilities.sleep(sleepINT);
//Create and set a counter for the invoice number, then grab the new invoice number
var oldInvoiceNumber = sheet.getRange("G2").getValue();
oldInvoiceNumber += 1;
sheet.getRange("G2").clear();
sheet.getRange("G2").setValue(oldInvoiceNumber);
sheet.getRange("G2").setBackgroundColor("#cecece");
var newInvNumber = sheet.getRange("G2").getValue();
ss.toast("20%: invoice number updated");
Utilities.sleep(sleepINT);
// Make a copy of the invoice template, then Fill up it up with the data from the spreadsheet.
//NOTE: body.replace method does not have to be in any specific order.
for (var i in data) {
var replaceTextToImage = function(body, searchText, fileId) {
var width = 590; // Please set this.
var blob = DriveApp.getFileById(fileId).getBlob();
var r = body.findText(searchText).getElement();
r.asText().setText("");
var img = r.getParent().asParagraph().insertInlineImage(0, blob);
var w = img.getWidth();
var h = img.getHeight();
img.setWidth(width);
img.setHeight(width * h / w);}
var row = data[i];
var docid = DriveApp.getFileById(templateid).makeCopy().getId();
var doc = DocumentApp.openById(docid);
var body = doc.getActiveSection();
var Image = row[10].split("=")[1];
var selectBody = doc.getBody();
body.replaceText("%NAME%", row[0]);
body.replaceText("%ADD_LN1%", row[1]);
body.replaceText("%EMAIL%", row[2]);
body.replaceText("%PHONE%", row[3]);
body.replaceText("%DATE%", row[4]);
body.replaceText("%EXPDATE%", row[5]);
body.replaceText("%INV_NUM%", row[6] +=1);
body.replaceText("%PTOTAL%", row[7]);
body.replaceText("%DESC1%", row[8]);
body.replaceText("%FEE1%", row[9]);
replaceTextToImage(body, '%IMAGE%', Image);
doc.saveAndClose();
ss.toast("30%: template data replaced");
Utilities.sleep(sleepINT);
//copy the modified template to the specified folder, then delete the first copy we made (to modify it)
var file = DriveApp.getFileById(doc.getId());
var newfolder = DriveApp.getFolderById("YYYYYYYYYYYYYYYYYYYY");
var oldfolder = DriveApp.getFolderById("ZZZZZZZZZZZZZZZZZZZZ");
newfolder.addFile(file);
oldfolder.removeFile(file);
ss.toast("40%: invoice has been put in correct folder");
Utilities.sleep(sleepINT);
//customize the title for the invoice
var usernamefordoctitle = sheet.getRange(2, 1, 1, 1).getValues() // this is grabbing the customer name field (A2)
var name = doc.getName();
doc.setName(newInvNumber + ' - Invoice for ' + usernamefordoctitle);
ss.toast("50%: named new invoice");
Utilities.sleep(sleepINT);
//create and organize pdf version
var pdffolder = DriveApp.getFolderById("***************");
var pdfFILE = DriveApp.getFileById(doc.getId()).getAs('application/pdf');
pdfFILE.setName(doc.getName() + ".pdf");
var theFolder = pdffolder;
var theFile = DriveApp.createFile(pdfFILE);
theFolder.addFile(theFile);
ss.toast("60%: PDF generated");
Utilities.sleep(sleepINT);
var email_status = sheet.getRange("C4").getValue();
if (email_status == "YES" ) {
//send a pdf copy to customer
var pdfEMAIL = DriveApp.getFileById(doc.getId()).getAs('application/pdf').getBytes();
var emailName = sheet.getRange("A11").getValue()
var emailToName = emailName; // add designer name here
var message = "Hi " + emailToName + "!, please find " + usernamefordoctitle + "'s project proposal attached. Reply to this email if you run into any issues.";
var emailAdd = sheet.getRange("A10").getValue()
var emailTo = emailAdd; // add designer email here
var subject = "Proposal for " + usernamefordoctitle + " by" + emailToName + " - Proposal No: " + newInvNumber;
var attach = {fileName:"PROPOSAL " + newInvNumber + " " + usernamefordoctitle + '.pdf',content:pdfEMAIL, mimeType:'application/pdf'};
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
ss.toast("70%: emailed customer");
Utilities.sleep(sleepINT);
}
else {
ss.toast("No email sent");
}
}
Utilities.sleep(sleepINT);
ss.toast("90%: feeding the unicorns some more")
Utilities.sleep(sleepINT);
ss.toast("100%: high-fiving the neighbour")
Utilities.sleep(sleepINT);
ss.toast("SUCCESS Invoice " + newInvNumber + " has been created. It's in a new doc with the ID " + docid);
Utilities.sleep(10000);
}
当我运行这个脚本时,一切都按预期工作并且电子邮件被发送。 当我尝试使用已安装的触发器时,它给了我以下错误:
Exception: Invalid argument: id at replaceTextToImage(Code:35:25) at CreatePro2(Code:62:5)
我不懂 javascript,所以我只是通过反复试验拼凑起来,所以我为混乱的代码道歉。
我可以使用不会导致此问题的替代触发方法吗?
任何帮助将不胜感激!
【问题讨论】:
-
你能用 Logger.log()/console.log() var Image 吗?因为错误告诉你它不是一个无效的 id。
-
我猜测 body、searchtext 和 fileId 是全局变量,并且在触发器上运行时它们可能不会被初始化。尝试将它们放入函数中。另外我会用标准的循环语法替换 (var i in data) 你应该只使用该语法来通过对象中的键进行交互。
-
你好@AFischer!除了@Salix 和@Cooper 的要求,您能否查看
replaceTextToImage(body, '%IMAGE%', Image);行?代码可能需要Image变量中的图像标识符,但提供的标识符可能有问题。为防止出现这种情况,请尝试对经过验证的 id 进行硬编码。 -
感谢您的回复! @Salix 我该如何实现那个记录器?
-
感谢@Cooper!我将如何将它们放入函数中?就像我说的,我一般不懂 javascript 或编程,所以把这些放在一起一直让我很紧张。
标签: google-apps-script google-sheets google-forms