javascript - 当尝试在 Google Sheet 脚本中使用 e.value 时,我得到 TypeError : Cannot read property "Values" from undefined. (第 7 行,文件 "Code")

标签 javascript google-apps-script google-sheets google-forms gs-installable-triggers

这是一个由两部分组成的问题:

  1. 我有带有链接表单的 Google 表格。提交表单后,我希望将表单中的响应复制到另一个谷歌表格中,我打算在其中进行更改和重新格式化,然后通过电子邮件发送。下面的脚本是我当前编写的,它在FormSubmit 上设置了一个触发器。但是,我不断收到以下错误:

类型错误:无法从未定义中读取属性“值”。 (第 7 行,文件“代码”)

代码如下:

function formSubmitReply(e)
{    
var t = "1g-wIs6nGxu3mJYA1vKtPCxBLCsvh1upeVGbCokOOTIw"; 
var tName = "AggregationOutput";

//Get information from form and set as variables
  var email = e.Values[2];
  var name = e.Values[3];

// Get template, copy it as a new temp, and save the Doc’s id
var tcopyId = SpreadsheetApp.openById(t).copy(tName+' for '+name).getId();

// Open the temporary document & copy form responses into template copy response sheet
var copyt = SpreadsheetApp.openById (tcopyId);
var copyts = copyt.getSheetByName('Resp');  

// Transfers Data from Form Responses to Temporary file
copyts.getRange('A3').setValue(name);

//Sends copy of template in an email as an excel file
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + copyt.getId();  
var subject = 'Aggregaton Output for' + name;
var body = url   
MailApp.sendEmail(email, subject, body);

// Deletes temp file
DriveApp.getFileById(tcopyId).setTrashed(true);
}
  • 我的问题的第二部分,即使我可以让代码工作,当在表单中跳过问题时,您会建议什么 - 这不会改变 e.values 中的数组吗?使用最后一行作为问题是我希望人们返回并编辑表单上的响应,然后重新提交,这意味着最后一行并不总是使用的行。
  • 感谢任何和所有帮助。

    最佳答案

    对于第 1 部分,请尝试以下操作:

    function formSubmitReply(e)
    {    
    var t = "1g-wIs6nGxu3mJYA1vKtPCxBLCsvh1upeVGbCokOOTIw"; 
    var tName = "AggregationOutput";
    
    //Get information from form and set as variables
    var itemResponses = e.response.getItemResponses();
    
      var email = itemResponses[2].getResponse();
      var name = itemResponses[3].getResponse();
    
    // Get template, copy it as a new temp, and save the Doc’s id
    var tcopyId = SpreadsheetApp.openById(t).copy(tName+' for '+name).getId();
    
    // Open the temporary document & copy form responses into template copy response sheet
    var copyt = SpreadsheetApp.openById (tcopyId);
    var copyts = copyt.getSheetByName('Resp');  
    
    // Transfers Data from Form Responses to Temporary file
    copyts.getRange('A3').setValue(name);
    
    //Sends copy of template in an email as an excel file
    var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + copyt.getId();  
    var subject = 'Aggregaton Output for' + name;
    var body = url   
    MailApp.sendEmail(email, subject, body);
    
    // Deletes temp file
    DriveApp.getFileById(tcopyId).setTrashed(true);
    }
    

    关于javascript - 当尝试在 Google Sheet 脚本中使用 e.value 时,我得到 TypeError : Cannot read property "Values" from undefined. (第 7 行,文件 "Code"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42082032/

    相关文章:

    r - 授予 Simba ODBC 驱动程序或 bigrquery 访问 Google Drive 文件的权限

    google-sheets - Google 表格 - 使用日期范围作为标准的 SUMIFS 显示错误的值

    javascript - 使用html突出显示文本的一部分

    javascript - 为什么 this 根据放置括号的位置来引用窗口或对象?并返回未定义

    javascript - 谷歌应用程序脚本 : How to fix "content is not allowed in prolog"

    javascript - 如何在for循环中使用变量

    arrays - AppS 脚本 : 'number of columns in the data does not match the number of columns in the range.' setValues method not reading array correctly?

    javascript - 如何将循环重写为递归

    javascript - 按钮点击没有正确调用我的 Angular Directive(指令)

    javascript - 带有 svelte 的 Google 验证码