我正在为 Google 表单创建一个 Apps 脚本,提交后将生成 Google 文档,并将 Google 文档的 URL 链接发送给收件人以进行编辑。 触发器起作用了。当它创建文档并发送包含 URL 的电子邮件时,它会执行预期的操作。
问题是它继续遍历谷歌工作表并转到最后一个空的单元格并创建一个空白的谷歌文档和错误消息:
错误异常:无效的电子邮件: 在未知函数 在 onFormSubmit(代码:12:8)
这会导致整个过程停止。我知道出现错误是因为 Google 表格的最后一行没有电子邮件,因为它都是空的。
这是代码:
const SHEETID = '1dplvx-LF5AhL4P-DgMGyqeW3z6VqeZJZmtyK-S_Uy6E'
const DOCID = '1oDFRGcajSJ66lZPYJ0EjxS3d9spS7TCPiRRVUciHvk0'
const FOLDERID = '1CjMmQ8p4q4O5aWjiAMyfe4VTCD2wJwZv'
function onFormSubmit(e) {
const sheet = SpreadsheetApp.openById(SHEETID).getSheetByName('Reference');
const temp = DriveApp.getFileById(DOCID);
const folder = DriveApp.getFolderById(FOLDERID);
const data = sheet.getRange("A:P").getValues();
const rows = data.slice(2);
rows.forEach((row,index)=>{
if(row[15] == ''){
const file = temp.makeCopy(folder);
const doc = DocumentApp.openById(file.getId());
const body = doc.getBody();
data[0].forEach((heading,i)=>{
const header1 = heading.toUpperCase();
body.replaceText(`{${header1}}`,row[i]);
})
doc.setName(row[0]+' '+'-'+' '+row[3]);
const docUrl = doc.getUrl();
doc.saveAndClose();
const email = row[2];
const email2 = row[9];
const subject = row[0]+' '+'-'+' '+row[3]+' '+'created';
const messageBody = `Hi, ${row[7]}, <p> ${row[0]+' '+'-'+' '+row[3]} has been created. <p> Here is the link: ${docUrl} <p> Please wait for Approval Signature before you serve the file to the employee. <p> Thank you!`;
MailApp.sendEmail({
to:email+','+email2,
subject:subject,
htmlBody:messageBody,
});
const tempo = sheet.getRange(index+3,16,1,1)
tempo.setValue(new Date());
Logger.log(row);
}
})
}
虽然我已经设定了
if(row[15] == ''){
第 13 行和
const tempo = sheet.getRange(index+3,16,1,1)
tempo.setValue(new Date());
Logger.log(row);
在第 35 行,它仍然继续为下一个空行生成 google 文档。
有没有办法只获取最后提交的响应来生成文档并将其作为电子邮件发送?
我认为它将消除 if(row[15] == ''){ 和 const tempo 但我已经有一段时间没有找到答案了。
任何帮助都是无价的。
rows.forEach((row,index)=>{
由于您在提交表单响应时单独处理表单响应,因此无需迭代
Reference
选项卡中的行。
使用事件对象来发现用户提交的
values
,如下所示:
// rows.forEach((row,index)=>{ <-- commented out
const row = e.values;
if (row[15] == '') {
const file = temp.makeCopy(folder);
const doc = DocumentApp.openById(file.getId());
const body = doc.getBody();
data[0].forEach((heading, i) => {
const header1 = heading.toUpperCase();
body.replaceText(`{${header1}}`, row[i]);
})
// ...