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错误。
修复步骤
- 直接复用之前创建的
copy文件对象,无需重新从文件夹查找,减少冗余操作 - 若必须从文件夹查找,需用
next()方法从迭代器中取出目标文件 - 转换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
相关产品推荐
相关产品推荐

