【问题标题】:TypeError: Cannot read property 'namedValues' of undefined [duplicate]TypeError:无法读取未定义的属性“namedValues”[重复]
【发布时间】:2020-12-22 12:27:15
【问题描述】:

请帮我解决以下错误。

TypeError:无法读取未定义的属性“namedValues”

因为我是 Google Apps 脚本的新手。我想生成 PDF 并通过他的电子邮件 ID 发送给请求者。请在下面查看我的脚本并提出建议。

function afterformsubmit(e){

const info = e.namedValues;
createPDF(info);
}

function createPDF(info){

const pdfFolder = DriveApp.getFolderById("1nV1zSSJfBND0ao9NufmIOEa_ocO5DhQf");
const tempFolder = DriveApp.getFolderById("1u3vbM1hxqKThVMsnjLeLWqLn3CWdTMDO");
const templateDoc   = DriveApp.getFileById("1qBtaAfcsHmxU9Y4wp6uQRdFkR95XhVNqpr6tDQ2xH7Q");
const newTempFile = templateDoc.makeCopy(tempFolder);
const openDoc = DocumentApp.openById(newTempFile.getId());
const body = openDoc.getBody();
body.replaceText("{dt}", info['PO Date'][0]);
body.replaceText("{dpt}", info['Department'][0]);
body.replaceText("{rname}", info['Requestor Name'][0]);
body.replaceText("{item}", info['Item 1'][0]);
body.replaceText("{qty}", info['Qty1'][0]);
body.replaceText("{price}", info['Unit price1'][0]);
openDoc.saveAndClose();
const blobPDF = newTempFile.getAs(MimeType.PDF);
const pdfFile = pdfFolder.createFile(blobPDF).setName(info['Requestor Name'][0] + " " + info['Department'][0]);
tempFolder.removeFile(newTempFile);
return pdfFile;
}

【问题讨论】:

  • 请同时贴出调用函数afterformsubmit的代码。我猜你传递了错误的event object 或者属性namedValues 确实不存在,就像错误显示的那样。

标签: google-apps-script google-sheets triggers google-forms


【解决方案1】:

您需要在您的项目中设置必要的触发器,以便在您的代码中获取和使用事件对象 e。要设置“on form submit”触发器,请按照以下步骤操作:

1.) 在脚本编辑器中,转到编辑 -> 当前项目的触发器。

2.) 在出现的新选项卡上,您将看到您手动添加的触发器列表(如果有)。要添加“on form submit”触发器,请点击“+ Add Trigger”按钮。

3.) 在弹出窗口中,选择触发器将运行的功能,填写其他字段,然后选择“提交表单”作为事件类型。点击保存。

这应该会在您的项目中设置可安装触发器 “提交表单时”,并且应该允许您在脚本中使用 namedValues。

参考

Event Objects and Triggers

Installable Triggers

【讨论】:

    猜你喜欢
    • 2021-03-18
    • 2014-09-07
    • 2017-12-15
    • 2019-08-19
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多