Google表单提交自动填充Google文档代码问题排查
问题排查:Google表单提交后Google文档替换文本未生效
代码中的明显错误
- 方法名大小写错误:
copy.getID()是错误写法,Google Apps Script 中正确的方法是copy.getId()(驼峰式命名,小写d)。这个错误会导致无法打开复制后的文档,后续的文本替换逻辑根本不会执行。 - 变量与替换文本不匹配:
- 变量名是
oneWhatdowethinkthistellsus,但替换文本写的是{{oneWhatdowethinkittellsus}}(thistellsusvsittellsus) - 同理,
twoWhatdowethinkthistellsus对应的替换文本是{{twoWhatdowethinkittellsus}},同样存在拼写差异。这种不匹配会导致对应位置的文本无法被替换。
- 变量名是
修正后的代码示例
function Autofillgoogledocfromform(e) { var oneDate = e.values[1]; var oneName = e.values[2]; var oneALCI = e.values[3]; var oneIndoorOutdoor = e.values[4]; var oneObservationNote = e.values[5]; var oneWhatdowethinkthistellsus = e.values[6]; var oneEYFSBPMLinks = e.values[7]; var twoDate = e.values[8]; var twoName = e.values[9]; var twoALCI = e.values[10]; var twoIndoorOutdoor = e.values[11]; var twoObservationNote = e.values[12]; var twoWhatdowethinkthistellsus = e.values[13]; var twoEYFSBPMLinks = e.values[14]; var file = DriveApp.getFileById('1F2gDifE915_yF6lizwZL55reBnecg4EQ'); var folder = DriveApp.getFolderById('1qwg-4_HMl2tNiLKqXTyjoXSuWeKtj4ZO'); var copy = file.makeCopy(oneDate, folder); // 修正方法名:getId() 而非 getID() var doc = DocumentApp.openById(copy.getId()); var body = doc.getBody(); body.replaceText('{{Date}}', oneDate); body.replaceText('{{oneName}}', oneName); body.replaceText('{{oneALCI}}', oneALCI); body.replaceText('{{oneIndoorOutdoor}}', oneIndoorOutdoor); body.replaceText('{{oneObservationNote}}', oneObservationNote); // 修正替换文本,与变量名匹配(thistellsus) body.replaceText('{{oneWhatdowethinkthistellsus}}', oneWhatdowethinkthistellsus); body.replaceText('{{oneEYFSBPMLinks}}', oneEYFSBPMLinks); body.replaceText('{{twoDate}}', twoDate); body.replaceText('{{twoName}}', twoName); body.replaceText('{{twoALCI}}', twoALCI); body.replaceText('{{twoIndoorOutdoor}}', twoIndoorOutdoor); body.replaceText('{{twoObservationNote}}', twoObservationNote); // 修正替换文本,与变量名匹配(thistellsus) body.replaceText('{{twoWhatdowethinkthistellsus}}', twoWhatdowethinkthistellsus); body.replaceText('{{twoEYFSBPMLinks}}', twoEYFSBPMLinks); doc.saveAndClose(); }
额外检查项
- 确认Google文档模板中的占位符(如
{{oneWhatdowethinkthistellsus}})与修正后的替换文本完全一致,包括括号、拼写和大小写。 - 验证
e.values的索引是否正确:Google表单的e.values数组第一个元素是提交时间戳(索引0),后续依次对应表单字段,确保你取的索引和实际字段顺序匹配。
内容的提问来源于stack exchange,提问作者paddy253
相关产品推荐
相关产品推荐

