onFormSubmit触发器无法在提交表单时发送电子邮件

时间:2015-07-15 09:09:12

标签: javascript forms google-apps-script triggers eventtrigger

我在这里有一段代码,在表单提交时应该:

使用表单回复在电子表格中执行一些计算,创建包含用户结果的pdf文件,并通过电子邮件发送给他们。

我设置了一个运行“onFormSubmit”的触发器,其中包含事件“From Spreadsheet”,“onFormSubmit”,这是我的代码:

//Set out global variables
var docTemplate = "1Ff3SfcXQyGeCe8-Y24l4EUMU7P9TsgREsAYO9W6RE2o";
var docName="Calculations";

//createOnFormSubmitTrigger();
function onFormSubmit(e){

  //Variables

  var ssID = '1dMmihZoJqfLoZs9e7YMoeUb_IobW4k6BbOuMDOTTLGk';
  var ss = SpreadsheetApp.openById(ssID);
  ss.setActiveSheet(ss.getSheetByName("Sheet3"));
  var totalOutstandingPrincipalDebt = SpreadsheetApp.getActiveSheet().getRange("G25").getValue();
  var totalOutstandingInterest = SpreadsheetApp.getActiveSheet().getRange("H25").getValue();
  var totalOutstandingCompensation = SpreadsheetApp.getActiveSheet().getRange("I25").getValue();
  var dailyInterestRate = SpreadsheetApp.getActiveSheet().getRange("J25").getValue();
  var grandTotal = SpreadsheetApp.getActiveSheet().getRange("K25").getValue();
  var userEmail = SpreadsheetApp.getActiveSheet().getRange("H24").getValue();

  //Template Info

  var copyId=DriveApp.getFileById(docTemplate).makeCopy(docName+' for '+userEmail).getId();
  var copyDoc = DocumentApp.openById(copyId);
  var copyBody = copyDoc.getActiveSection();

  //Putting the data into the file
  copyBody.insertParagraph(1,'Total Outstanding Principal Debt: £' + totalOutstandingPrincipalDebt);
  copyBody.insertParagraph(2,'Total Outstanding Interest: £'+ totalOutstandingInterest );
  copyBody.insertParagraph(3,'Total Outstanding Compensation: £'+ totalOutstandingCompensation);
  copyBody.insertParagraph(4,'Grand Total: £' + grandTotal);
  copyBody.insertParagraph(5,'Daily Interest Rate: £'+ dailyInterestRate);
  copyDoc.saveAndClose();

 //email pdf document as attachment 
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
  var subject = "Calculations";
  var body = "Thank you very much for using our online calculator. Please find your results attached.";

  MailApp.sendEmail(userEmail, subject, body, {htmlBody: body, attachments: pdf});

  //Deletes temporary Document
  DriveApp.getFileById(copyId).setTrashed(true);
}

当我在脚本编辑器中时,脚本有时会正常运行(并不总是?!),但是当我提交表单时,收到以下错误通知:“无法发送电子邮件:没有收件人(行) 40,文件“代码”)“,其中第40行是行:

MailApp.sendEmail(userEmail, subject, body, {htmlBody: body, attachments: pdf});

我已尝试使用getNote()而不是getValue()作为userEmail变量,但这也不起作用!我还确保电子表格上的单元格引用格式为纯文本而不是数字,但我不确定现在还要尝试什么!任何建议将不胜感激!

事先非常感谢:)

1 个答案:

答案 0 :(得分:0)

自从我改变后,它现在正在运作:

var ssID = '1dMmihZoJqfLoZs9e7Y.............';
var ss = SpreadsheetApp.openById(ssID);
ss.setActiveSheet(ss.getSheetByName("Sheet3"));
var totalOutstandingPrincipalDebt = SpreadsheetApp.getActiveSheet().getRange("G25").getValue();
var totalOutstandingInterest = SpreadsheetApp.getActiveSheet().getRange("H25").getValue();
var totalOutstandingCompensation = SpreadsheetApp.getActiveSheet().getRange("I25").getValue();
var dailyInterestRate = SpreadsheetApp.getActiveSheet().getRange("J25").getValue();
var grandTotal = SpreadsheetApp.getActiveSheet().getRange("K25").getValue();
var userEmail = SpreadsheetApp.getActiveSheet().getRange("H24").getValue();

为:

 var ssID = '1dMmihZ.................';
var ss = SpreadsheetApp.openById(ssID);
var sheet = SpreadsheetApp.setActiveSheet(ss.getSheets()[0]);
var totalOutstandingPrincipalDebt = sheet.getRange("G25").getValue();
var totalOutstandingInterest = sheet.getRange("H25").getValue();
var totalOutstandingCompensation = sheet.getRange("I25").getValue();
var dailyInterestRate = sheet.getRange("J25").getValue();
var grandTotal = sheet.getRange("K25").getValue();
var userEmail = sheet.getRange("H24").getValue();