You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google表单自动填充Doc并发送PDF报错:TypeError: waiver.getAs is not a function

问题分析与修复

错误原因

你调用DriveApp.getFolderById(...).getFilesByName(firstName)得到的是文件迭代器(FileIterator),而非单个文件对象。getAs()是文件(File)对象的专属方法,所以调用迭代器的getAs()会触发TypeError: waiver.getAs is not a function错误。

修复步骤

  1. 直接复用之前创建的copy文件对象,无需重新从文件夹查找,减少冗余操作
  2. 若必须从文件夹查找,需用next()方法从迭代器中取出目标文件
  3. 转换PDF时,使用正确的MIME类型:可直接用内置枚举MimeType.PDF(无需加引号),或字符串'application/pdf'

修复后的完整代码

function autoFillGoogleDocFromForm(e) {
  // 提取表单提交值
  var timestamp = e.values[0];
  var firstName = e.values[1];
  var email = e.values[2];
  var note = e.values[3];

  // 获取模板文件与目标文件夹
  var tempfile = DriveApp.getFileById('1GfUEmx07Ev1YFubbuhpJOjYY0OHtMGoojUK92c0q_So'); 
  var answerfolder = DriveApp.getFolderById('1x5EhuzXP9EgcJZBqsl_8uaniRDjz2hS1');

  // 创建模板副本并填充内容
  var copy = tempfile.makeCopy(firstName, answerfolder); 
  var doc = DocumentApp.openById(copy.getId());
  var body = doc.getBody(); 

  body.replaceText('{{firstname}}', firstName); 
  body.replaceText('{{email}}', email);  
  body.replaceText('{{note}}', note); 
  doc.saveAndClose(); 

  // 发送邮件(直接复用copy文件对象转换PDF)
  var subject = "thank you " + firstName;
  var messageBody = "Hi " + firstName ;
  var pdfFile = copy.getAs(MimeType.PDF);
 
  MailApp.sendEmail({
    to: email,
    subject: subject,
    htmlBody: messageBody,
    attachments: [pdfFile]
  });
}

内容的提问来源于stack exchange,提问作者umar joseph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 01:40:50