代码之家  ›  专栏  ›  技术社区  ›  Sam Murdock

我在谷歌应用程序脚本中的事件对象有什么问题?

  •  0
  • Sam Murdock  · 技术社区  · 5 月前

    也许我误解了事件对象的工作原理,但我一直在学习如何让谷歌表单在每次提交表单时发送pdf。这让我找到了onFormSubmit(e){}函数,然后拉取e.namedValues,但它一直给我提供

    TypeError:无法读取undefined的属性(读取“namedValues”) onFormSubmit@Code.gs:2

    我不明白是什么导致我做错了这件事?我已经设置了触发器,当表单被子限制在FormSubit上运行时,但我无法让它使用事件对象。

    function onFormSubmit(e) {
      const info = e.namedValues;
      createPDF(info);
    }
    
    function createPDF(info ){
    
    const pdfFolder = DriveApp.getFolderById("1g58GUQLPjPonsHtxj5LlxyoDgXs5wj2R");
    const tempFolder = DriveApp.getFolderById("1Fkzf0xeZcedfq7BF2k3V4mn4Pz_LsXsv");
    const templateDoc = DriveApp.getFileById("1eOqom8SqhuDUpIqYEVum-EvQ09cVz2d_XCLcRNAz8jE");
    
    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("{bd}", info['Birthday'][0]);
    body.replaceText("{em}", info['Email'][0]);
    body.replaceText("{pn}", info['Phone Number'][0]);
    body.replaceText("{pv}", info['Province'][0]);
    body.replaceText("{cm}", info['Contact Method'][0]);
    body.replaceText("{lg}", info['Language'][0]);
    body.replaceText("{ts}", info['Type Of Service'][0]); //error TypeError: Cannot read properties of undefined (reading '0')
    body.replaceText("{cn}", info['Child Name'][0]);
    body.replaceText("{cbd}", info['Child Birthday'][0]);
    body.replaceText("{sr}", info['Services Required'][0]);
    body.replaceText("{stf}", info['Staff Requested'][0]);
    body.replaceText("{pri}", info['Priority'][0]);
    body.replaceText("{ref}", info['Referral'][0]);
    body.replaceText("{jc}", info['Jane Consent'][0]);
    
    openDoc.saveAndClose();
    
    const blobPDF = newTempFile.getAs(MimeType.PDF);
    const pdfFile = pdfFolder.createFile(blobPDF).setName(info['First Name'][0] + ' ' + (info['Last Name'][0] ));
    tempFolder.removeFile(newTempFile);
    
    }
    
    2 回复  |  直到 5 月前
        1
  •  1
  •   doubleunary    5 月前

    错误表明 e 未定义,这意味着您可能在脚本编辑器中运行了该函数。不要通过以下方式运行代码 ·快跑 脚本编辑器中的按钮。如果这样做,事件参数 e 将不会填充,从而导致您提到的错误。

    相反,当您提交新的表单响应时,让触发器运行该函数。

    当脚本项目是 bound 当“表单提交”触发器触发时,该函数运行, e.namedValues 将出席。

    有关调试,请参阅 How can I test a trigger function in GAS?

        2
  •  0
  •   leylou    5 月前

    @双元说得对。此外,为了改进代码,我建议使用这段代码将所有函数组合成一个。在最后一部分,我改变了 tempFolder.removeFile(newTempFile); tempFolder.getFilesByName(newTempFile).next().setTrashed(true); 因为 removeFile 方法已弃用。我把它改成了 setTrashed ,这是现代的等价物。

    代码:

    function onFormSubmit(e) {
      const info = e.namedValues;
    
      const pdfFolder = DriveApp.getFolderById("1g58GUQLPjPonsHtxj5LlxyoDgXs5wj2R");
      const tempFolder = DriveApp.getFolderById("1Fkzf0xeZcedfq7BF2k3V4mn4Pz_LsXsv");
      const templateDoc = DriveApp.getFileById("1eOqom8SqhuDUpIqYEVum-EvQ09cVz2d_XCLcRNAz8jE");
    
      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("{bd}", info['Birthday'][0]);
      body.replaceText("{em}", info['Email'][0]);
      body.replaceText("{pn}", info['Phone Number'][0]);
      body.replaceText("{pv}", info['Province'][0]);
      body.replaceText("{cm}", info['Contact Method'][0]);
      body.replaceText("{lg}", info['Language'][0]);
      body.replaceText("{ts}", info['Type Of Service'][0]);
      body.replaceText("{cn}", info['Child Name'][0]);
      body.replaceText("{cbd}", info['Child Birthday'][0]);
      body.replaceText("{sr}", info['Services Required'][0]);
      body.replaceText("{stf}", info['Staff Requested'][0]);
      body.replaceText("{pri}", info['Priority'][0]);
      body.replaceText("{ref}", info['Referral'][0]);
      body.replaceText("{jc}", info['Jane Consent'][0]);
    
      openDoc.saveAndClose();
    
      const blobPDF = newTempFile.getAs(MimeType.PDF);
      pdfFolder.createFile(blobPDF).setName(info['First Name'][0] + ' ' + (info['Last Name'][0]));
      // tempFolder.removeFile(newTempFile);
      tempFolder.getFilesByName(newTempFile).next().setTrashed(true);
    }
    

    参考: setTrashed(trashed)