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

Google Script无法生成PDF问题求助

问题:自定义参数后Google Script无法生成PDF

我跟着YouTube教程写了一段Google Script,用来从Google Sheets生成PDF。完全复刻教程内容时能正常生成PDF,但改成自定义参数后,PDF就生成不了了。作为新手我自己排查过,但没找到问题出在哪,相关代码如下:

function afterFormSubmit(e) {
  const info = e.namedValues;
  createPDF(info);

}


function createPDF(info){

   const pdfFolder = DriveApp.getFolderById("1TzHSBQVLBTFRMJLNc3y_ZfFSJCN8YN-u");
   const tempFolder = DriveApp.getFolderById("1dAdxsFtuowCSQgHOrU6GrkQvUH86YG_E");
   const templateDoc = DriveApp.getFileById("1lC50FzTsvwvO-5981OCob4-ideeqKN91VzC1WMO7dRw");

   const newTempFile = templateDoc.makeCopy(tempFolder);
   const openDoc = DocumentApp.openById(newTempFile.getId());
   const body = openDoc.getBody();

   body.replaceText("{{name}}", info['Employee name'][0]);
   body.replaceText("{{restaurant}}", info['Restaurant where employee works'][0]);
   body.replaceText("{{position}}", info['Employees position'][0]);
   body.replaceText("{{race}}", info['What is the employees race'][0]);
   body.replaceText("{{origin}}", info['What is the employees national orgin'][0]);
   body.replaceText("{{age}}", info['What is the employees exact age'][0]);
   body.replaceText("{{gender}}", info['What is the employees gender'][0]);
   body.replaceText("{{orientation}}", info['What is the employees sexual orientation if   known'][0]);
   body.replaceText("{{health}}", info['Has the employee ever disclosed that they have any   ongoing health issues to anyone in management'][0]);
   body.replaceText("{{leave}}", info['Has the employee recently taken a leave'][0]);
   body.replaceText("{{pregnant}}", info['Is the employee pregnant or was the employee   recently pregnant'][0]);
   body.replaceText("{{religion}}", info['Has the employee ever requested any religious accommodations'][0]);
   body.replaceText("{{complaint}}", info['Has the employee ever raised a complaint of harassment, discrimination, or sexual harassment to anyone in management'][0]);
   body.replaceText("{{information}}", info['Please provide any other relevant information about the employee'][0]);
   body.replaceText("{{reason}}", info['Reason for termination request'][0]);
   body.replaceText("{{answer}}", info['Has the employee ever been issued any written   disciplinary action form? If yes, please provide copies of all disciplinary warnings with this request'][0]);
   body.replaceText("{{manager}}", info['Name of manager completing this form'][0]);
   openDoc.saveAndClose();

   const blobPDF = newTempFile.getAs(MimeType.PDF);
   const pdfFile = pdfFolder.createFile(blobPDF).setName(info['Employee name'][0] + " " +       info['Restaurant where employee works'][0]);
   tempFolder.removeFile(newTempFile);
   }

}

排查建议

  • 核对字段名一致性:确保info对象中的键(如'Employee name')和Google Sheets表单的列标题完全匹配,包括空格、大小写,任何细微差异都会导致无法获取值,中断PDF生成流程。
  • 查看脚本错误日志:打开Google脚本编辑器,点击「查看」→「日志」,或在「执行」标签页查看最近的执行记录,里面会显示具体错误信息(比如字段不存在、权限不足),这是定位问题最直接的方式。
  • 检查模板占位符:确认Google Docs模板中的占位符(如{{name}})和代码里replaceText的第一个参数完全一致,没有多余空格或拼写错误。
  • 验证权限:确保脚本已获得Drive的读写权限,第一次运行时会弹出授权请求,必须完成全权限授权才能操作文件夹和文档。
  • 逐步测试替换逻辑:先注释掉大部分replaceText语句,只保留1-2个进行测试,确认能生成PDF后再逐个恢复,排查是哪个字段或替换语句导致的问题。
  • 处理空值场景:如果表单存在可选字段,可能返回空值,建议给这些字段添加默认值,比如info['字段名'][0] || '无',避免因空值导致替换失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 13:55:56