从 Google 表单提交生成 PDF 的脚本

Script to Generate PDF from Google Form Submission

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

我得到的错误是 TypeError: Cannot read 属性 '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)]});
  } ```
  • Cannot read property 'range' of undefined 表示您正在尝试 运行 您的代码手动

  • 但是,const rg = e.range; 表示您正在检索范围,因为 event object - which can be retrieved only when a trigger 触发

总结:

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