发现有关事件对象 .namedValues() 的错误

Found error about event object .namedValues()

我只是 JavaScript 的新手,抱歉我的英语不好。我遵循某人的编码。我尝试了很多时间来理解但无法解决这个问题(在他的 post 和评论中也没有找到答案。)。这是原始代码。日志的说法

TypeError: Cannot read property 'namedValues' of undefined".

那我有2个问题

  1. 如何修复这个错误? (我试图创建这个功能) 第 3 行错误。)

  2. 可以在 google 电子表格中添加新行后自动修改此代码运行 (示例:我有 2 条记录,当我通过其他方式插入另一行(1 条新记录)时,通过 Web 应用程序(而非表单)输入时,此脚本会自动 运行 通过检测新行。)

修改后的第 2 季度 2021/04/06

function AfterFormSubmit (e) {
  
  const info = e.namedValues(info);
  const pdfFile = createPDF(info);
  const entryRow = e.range.getRow();
  const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet");
  ws.getRange(entryRow, 6).setValue(pdfFile.getUrl());
  ws.getRange(entryRow, 7).setValue(pdfFile.getName());

function createPDF(info) {

  const pdfFolder = DriveApp.getFolderById("XXXX");
  const tempFolder = DriveApp.getFolderById("XXXX");
  const templateDoc = DriveApp.getFileById("XXXX");

  const newTempFile = templateDoc.makeCopy(tempFolder);
  const openDoc = DocumentApp.openById(newTempFile.getId());
  const body = openDoc.getBody();

  body.replaceText("{fn}", info['First Name'][0]);
  body.replaceText("{ln}", info['Last Name'][0]);
  body.replaceText("{addr}", info['shipping address'][0]);
  body.replaceText("{qty}", info['Quantity Required'][0]);
  openDoc.saveAndClose();

  const blobPDF = newTempFile.getAs("application/pdf");
  const pdfFile = pdfFolder.createFile(blobPDF).setName(info['First Name'][0]);
  tempFolder.removeFile(newTempFile);
  return pdfFile;
}

来自问题:

Log's say

TypeError: Cannot read property 'namedValues' of undefined".

那我有2个问题

  1. 如何修复此错误?

当需要参数的函数(在本例中为 AfterFormSubmit 且所需参数为 e)从脚本编辑器执行时,会发生此错误。

如果您需要从脚本编辑器运行它,您必须在代码中使用它之前声明此变量。

function AfterFormSubmit (e){
  const e = {
    namedValues: // add here the appropriate litera, function or object
  }

  // add here the rest of the code

}

但您的代码显然还有其他问题。 Cooper 在对问题的评论中已经提到了其中之一:

I believe this const info = e.namedValues(info); should be this const info = e.namedValues["info"][0];

再次从问题

  1. Can this code autorun after New row added in google spreadsheet (This function i trying to create) error in line 3.

修复代码中的错误后,通过使用适当的触发器,它可能是“auto运行”。该问题没有包含足够的详细信息,无法让我们判断哪个是合适的。