【问题标题】:Script to Generate PDF from Google Form Submission从 Google 表单提交生成 PDF 的脚本
【发布时间】:2021-11-13 20:44:11
【问题描述】:

我之前为 google 表格编写了一个脚本,该脚本在其相关的 google 表单提交表单时触发。提交表单后,将生成 PDF 并通过电子邮件发送给指定人员。我创建了一个新版本来自动生成数字千年版权法案通知,但脚本似乎不再适用(原版也不再适用),我不知道如何修复它。

我得到的错误是 TypeError: Cannot read property 'range' of undefined (line 2, file "Code")


9/28/2021 我按照说明将console.log(rg.getA1Notation()) 添加到代码中并提交了表单。它的执行日志向我显示了以下内容 -

代码如下 -

  const rg = e.range;
  console.log(rg.getA1Notation());
  const sh = rg.getSheet();
  
  //Get all the form submitted data
  const Email= e.namedValues['Email Address'][0];
  const LinkOrig = e.namedValues['Link(s) for where the original work appears'][0];
  const AttachOrig = e.namedValues['Copies of the original copyrighted work'][0];
  const Domain = e.namedValues['Infringing Domain'][0];
  const LinkInfring = e.namedValues['Link(s) for where infringing image appears online'][0];
  const Contact = e.namedValues['Contact Information'][0];
  const WHOIS = e.namedValues['WHOIS Search results'][0];
  const Date = e.namedValues['Date'][0];
  const Location = e.namedValues['Where are you based?'][0];
  
  //Build a new DMCA Form from the template
  //Folder ID (save destination) and file IDs (template ID + new doc ID)
  const DMCAFolderID = 'googledrivefolderidhere';
  const DMCALibFolder = DriveApp.getFolderById(DMCAFolderID);
  
  const TemplateFileID = 'googledrivetemplateidhere';
  const newFilename = 'DMCA Notice -' + TemplateFileID + 'Domain';
  
  //Make a copy of the template file
  const newTemplateFileID = DriveApp.getFileById(TemplateFileID).makeCopy(newFilename, DMCALibFolder).getId();;
  
  //Get the DMCA Notice body into a variable
  var document = DocumentApp.openById(newTemplateFileID);
  var body = document.getBody();
  
  //Replace all the {{ }} text in the BlinkLib body
  body.replaceText('{{LinkOrig}}', LinkOrig);
 // body.replaceText('{{AttachOrig}}', AttachOrig);
  body.replaceText('{{LinkInfring}}', LinkInfring);
  body.replaceText('{{ContactInfo}}', Contact);
  body.replaceText('{{WHOISResults}}', WHOIS);
  body.replaceText('{{date}}', Date);
  body.replaceText('{{location}}', Location);
  
  document.saveAndClose();

// define email variables
var subject = 'DMCA Notice - ' + Domain;
var msgHtml = 
"Hi " + Name + "," + "<br/>" + "<br/>" +
"Please find your DMCA Notice attached." + "<br/>" + "<br/>" +
"Sincerely," + "<br/>" +
"Your Bada** Self" + "<br/>" 
;
var attachment = DriveApp.getFileById(newTemplateFileID);

//send email with the file
GmailApp.sendEmail(Email, subject, msgHtml, {htmlBody: msgHtml, attachments: [attachment.getAs(MimeType.PDF)]});
  } ```

【问题讨论】:

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


    【解决方案1】:
    • Cannot read property 'range' of undefined 表示您正在尝试手动运行代码

    • 但是,const rg = e.range; 表示您正在以 event object 的形式检索范围 - 只有当 trigger 触发时才能检索该范围

    总结:

    当使用像e.range这样的事件对象时,你不能手动运行你的代码,你需要让执行被自动触发。这意味着您的功能在触发器上被调用 - 例如onEdit 触发器。

    【讨论】:

    • 你的意思是,它需要从表单提交中运行?我已经以这种方式运行它,但它仍然无法以这种方式运行。我在执行日志中看到的错误说同样... TypeError: Cannot read property 'range' of undefined at onSubmit(Code:2:16)
    • 那么您的函数是否与 formSubmit 触发器相关联?脚本是绑定到电子表格还是表单?
    • 该函数与表单提交触发器相关联,是的。该脚本已绑定到电子表格。
    • 如果在提交表单响应时自动触发代码,您应该不会收到此类错误。仔细检查错误的时间戳是否与表单提交事件匹配。在您的代码中实现一些日志,例如console.log(RG.getA1Notation())` 并将这一行放在const rg = e.range; 之后。然后,在提交表单响应后继续 View-&gt;Executions 并使用与表单提交事件匹配的时间戳扩展执行。
    • 哦,我没有意识到这就是 Code: 47:9 的意思。哦,天哪,我觉得自己好傻。我为此倾注了好几天,对为什么它不起作用感到困惑。今天终于第一次成功了,我很兴奋 :) :) :) 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多