简体   繁体   English

Google Forms脚本发送电子邮件故障

[英]Google Forms Script send email malfunction

I'm trying to make a google form of mine send me the contents of the form via email each time someone fills out the form. 我正在尝试制作我的Google表单,每次有人填写该表单时,都会通过电子邮件向我发送该表单的内容。 I'm using the instructions below, and I've followed them exactly. 我正在使用以下说明,并且已经严格按照它们进行操作。 For some reason, however, when I fill out a form, I get an error saying "TypeError: Cannot call method "getRange" of null. (line 7, file "Code")" 但是由于某种原因,当我填写表单时,出现错误消息“ TypeError:无法调用null的方法“ getRange”(第7行,文件“ Code”))

I look at the line of code right above it, in which "s" is defined and I'm thinking that the problem is that getActiveSpreadsheet is not working for some reason. 我看一下上面的代码行,其中定义了“ s”,并且我认为问题是getActiveSpreadsheet由于某种原因无法正常工作。 I think that maybe the script can't find the active sheet. 我认为脚本可能找不到活动工作表。 Below is the code snippet which is causing the problem, according to the error email I get from google. 根据我从Google收到的错误电子邮件,以下是导致问题的代码段。

  var s = SpreadsheetApp.getActiveSheet();
  var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];    

Whole piece of code I'm using; 我正在使用的整个代码;

function sendFormByEmail(e) 
{    
  // Remember to replace this email address with your own email address
  var email = "support@itjones.com"; 

  var s = SpreadsheetApp.getActiveSheet();
  var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];    
  var message = "";
  var subject = "Jones IT - New User Form";

  // The variable e holds all the form values in an array.
  // Loop through the array and append values to the body.

  for(var i in headers) 
    message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "\n\n";     

  // Insert variables from the spreadsheet into the subject.
  // In this case, I wanted the new hire's name and start date as part of the
  // email subject. These are the 3rd and 16th columns in my form.
  // This creates an email subject like "New Hire: Jane Doe - starts 4/23/2013"
  subject += e.namedValues[headers[1]].toString() + " - starts " + e.namedValues[headers[2]].toString();

  // Send the email
  MailApp.sendEmail(email, subject, message); 

}

Ok, thank you, that makes sense but when I changed my code to use that code snippet I get this error. 好的,谢谢,这很有意义,但是当我更改代码以使用该代码段时,出现此错误。

2/12/15 4:56 PM sendFormByEmail TypeError: Cannot call method "getRange" of null. 15/2/12 PM sendFormByEmail TypeError:无法调用null的方法“ getRange”。 (line 9, file "Code") formSubmit 2/12/15 4:56 PM (第9行,文件“代码”)提交表格15年2月12日下午4:56

Line 9 is the line with the function call for sheet.getRange(). 第9行是具有sheet.getRange()函数调用的行。

  var s = SpreadsheetApp.openById('1WYhDQct8vF-3rlca9dP6lYC2Z23vLLofwJt_DpQU--c');
  var sheet = s.getSheetByName('Jones IT - New User Form (Responses)');
  var headers = sheet.getRange(1,1,1,s.getLastColumn()).getValues()[0];

Is there any way you recommend me changing my code to get the desired result. 有什么方法可以建议我更改代码以获得所需的结果。 I don't really care what I have to change, I just want it to email me the contents of the forms when submitted. 我并不在乎我必须更改什么,我只希望它在提交时通过电子邮件将表格的内容发送给我。

The reason you are getting the error, is because the code is being run from a script project bound to the form, and not a spreadsheet. 出现错误的原因是因为代码是从绑定到表单的脚本项目而不是电子表格运行的。 The script bound to the form has no connection to the spreadsheet. 绑定到表单的脚本与电子表格没有任何关系。 It can't find the active sheet. 它找不到活动工作表。 There is no active sheet associated with the form. 没有与表单关联的活动工作表。 The form is associated with the spreadsheet as far as where to submit the data, but not in terms of the code. 该表单与电子表格相关联,涉及的是提交数据的位置,但与代码无关。 You can set a trigger in the spreadsheet to run when the form is submitted, but you might want to get the spreadsheet by ID. 您可以在电子表格中设置一个触发器,使其在提交表单时运行,但是您可能希望通过ID获取电子表格。

function test() {

  var s = SpreadsheetApp.openById('your SS ID');
  var sheet = s.getSheetByName('your sheet name');
  var headers = sheet.getRange(1,1,1,s.getLastColumn()).getValues()[0]; 

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM