gpt4 book ai didi

google-apps-script - 在 onFormSubmit 触发器中获取 TypeError?

转载 作者:行者123 更新时间:2023-12-03 20:51:58 32 4
gpt4 key购买 nike

我使用 Google 表单教程来调整表单数据以合并为 PDF,然后发送到电子邮件。当我尝试运行脚本时收到以下错误消息:

TypeError: Cannot read property "values" from undefined. (line 11, file "Code")



我不知道如何解决这个问题。我已经在网上搜索了答案。这是脚本的副本。我标记了脚本出错的 2 行:
var docTemplate = "1ZSqmId2BBjtz6PmgQEmusjnkHGsFKD1CBSq0rrQk6Kc";  
var docName = "TestCertificate";

// When Form Gets submitted

function onFormSubmit(e) {

//Get information from form and set our variables

var email_address = "EMAIL@example.com";

//**(THIS IS WHERE THE ERROR IS OCCURRING ON THESE 2 LINES BELOW!)**

var full_name = e.values[2];
var Activity = e.values[3];

// Get document template, copy it as a new temp doc, and save the Doc’s id

var copyId = DocsList.getFileById(docTemplate)
.makeCopy(docName+' for '+full_name)
.getId();

// Open the temporary document

var copyDoc = DocumentApp.openById(copyId);

// Get the document’s body section

var copyBody = copyDoc.getActiveSection();

// Replace place holder keys,in our google doc template

copyBody.replaceText('keyFullName', full_name);
copyBody.replaceText('keyActivity', Activity);



// Save and close the temporary document

copyDoc.saveAndClose();

// Convert document to PDF

var pdf = DocsList.getFileById(copyId).getAs("application/pdf");

// Attach PDF and send the email

var subject = "Report";
var body = "Here is the form for " + full_name + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});

// Delete temp file

DocsList.getFileById(copyId).setTrashed(true);
}

这是我正在测试的表格和证书的链接。
  • Form/Spreadsheet
  • Document Template
  • 最佳答案

    您看到的错误是因为您正在脚本编辑器中运行触发器函数。执行此操作时,事件参数 e未定义 - 这就是错误消息所说的。

    更多背景请见How can I test a trigger function in GAS?

    这是一个测试函数,它将运行您的 onFormSubmit()使用电子表格中已有的数据多次运行。它读取工作表的每一行,生成一个对象来模拟提交表单时您将获得的事件,然后调用触发器函数。如果在 onFormSubmit() 内放置断点,或依赖 Logger.log() ,此技术将允许您测试触发功能。

    function test_onFormSubmit() {
    var dataRange = SpreadsheetApp.getActiveSheet().getDataRange()
    var data = dataRange.getValues();
    var headers = data[0];
    // Start at row 1, skipping headers in row 0
    for (var row=1; row < data.length; row++) {
    var e = {};
    e.values = data[row];
    e.range = dataRange.offset(row,0,1,data[0].length);
    e.namedValues = {};
    // Loop through headers to create namedValues object
    for (var col=0; col<headers.length; col++) {
    e.namedValues[headers[col]] = e.values[col];
    }
    // Pass the simulated event to onFormSubmit
    onFormSubmit(e);
    }
    }

    我没有对您的原始函数进行其他调试……但这消除了该错误消息,因此您可以继续测试。

    关于google-apps-script - 在 onFormSubmit 触发器中获取 TypeError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17984230/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com