我有一个工作流,用户可以在其中填写Google表单,然后相关的Google表格信息填充Google文档并发送电子邮件。 该脚本在我所有的测试中都能正常运行,但是当我尝试设置可安装触发器时,它会一次又一次地失败。
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,因此我一直通过反复试验将其拼凑在一起,因此我为混乱的代码表示歉意。
我是否可以使用不会引起此问题的替代触发方法?
任何帮助将不胜感激!
答案 0 :(得分:1)
我在谈论这条线:
var replaceTextToImage = function(body, searchText, fileId) {
,在对它们执行任何操作之前,我需要了解它们的定义。因此,您将不得不花费更多的精力来查找和共享这些声明。
我们需要在这里解析未定义的变量。请参阅我的评论。
function CreatePro2() {
var sleepINT = 1500
var templateid = "XXXXXXXXXXXXXXXXXX";
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var data = sheet.getRange(2,1,1,23).getValues();
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();
for (var i=0;i<data.length;i++) {
var replaceTextToImage = function(body, searchText, fileId) {//body,searchText and fileId undefined
var width = 590;
var blob = DriveApp.getFileById(fileId).getBlob();//fileId is undefined
var r = body.findText(searchText).getElement();//searchText is undefined
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();//Who is making the selection?
var Image = row[10].split("=")[1];
var selectBody = doc.getBody();
body.replaceText("%NAME%", row[0]);//body is undefined
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();
var file = DriveApp.getFileById(doc.getId());
var newfolder = DriveApp.getFolderById("YYYYYYYYYYYYYYYYYYYY");
var oldfolder = DriveApp.getFolderById("ZZZZZZZZZZZZZZZZZZZZ");
newfolder.addFile(file);
oldfolder.removeFile(file);
var usernamefordoctitle = sheet.getRange(2, 1, 1, 1).getValues();
var name = doc.getName();
doc.setName(newInvNumber + ' - Invoice for ' + usernamefordoctitle);
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);
var email_status = sheet.getRange("C4").getValue();
if (email_status == "YES" ) {
var pdfEMAIL = DriveApp.getFileById(doc.getId()).getAs('application/pdf').getBytes();
var emailName = sheet.getRange("A11").getValue()
var emailToName = emailName;
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;
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]});
} else {
ss.toast("No email sent");
}
}
}
您不是程序员,也不了解JavaScript,这可能会要求您雇用某人来帮助您,但我无法解决我无法生成的问题,也无法提出我不声明的问题无法访问,因此您必须这样做。
答案 1 :(得分:0)
所以我注意到代码正在使用活动电子表格。根据您的触发条件,活动电子表格可能不是您想要的电子表格吗?
从脚本编辑器运行脚本时,通常会从与其关联的电子表格中打开它,这意味着活动电子表格始终是正确的电子表格。
我建议直接打开电子表格。这样一来,您一定会拥有正确的电子表格。
您可以通过将var ss = SpreadsheetApp.getActiveSpreadsheet();
更改为var ss = SpreadsheetApp.openById(id);
doc
或使用网址:var ss = SpreadsheetApp.openByUrl(url);
doc
如果更改,您也可以像稍后在代码中一样搜索文件,并使用以下命令打开文件:var ss = SpreadsheetApp.open(file);
doc
[编辑]
我注意到的另一件事,但这应该在运行脚本时给您一个错误,而不仅仅是使用已安装的触发器...这行是这里:
//sheet.getRange("F7").setValue('=IMAGE("https://s-media-cache-ak0.pinimg.com/564x/58/5d/8f/585d8f802867c25df8f1ecc0cf7cadc8.jpg",1)')
如果获取Image的fileId的单元格内容相似,那将是一个问题。
由于您将图像定义为var Image = row[10].split("=")[1];
之类,这意味着ID是'='之后的内容,而'IMAGE(“ https://s-media-cache-ak0.pinimg.com/564x/58/5d/8f /585d8f802867c25df8f1ecc0cf7cadc8.jpg",1)'不是ID。
您能告诉我们第[10]行的内容是什么吗?
如果这不是为什么获取错误的Image id的原因,则需要进行一些调试,并检查变量是否与您想像的一样:
您可以使用Logger.log(data)
记录要检查的变量。只需在定义该变量后确保已完成即可。 doc
如果您这样做,请在您的问题中分享结果。