google-apps-script - 自动将数据从 Google 表格复制到 Google 文档模板
问题描述
每次有人提交 Google 表单时,我都需要自动填充 Google 文档模板。我有包含所有信息的 Google 表格,并且我已经设置了模板文档,其中包含我想要填充的区域,但我找不到任何地方可以显示如何做我需要做的事情。
请问有人可以分享知识吗?
解决方案
您正在寻找的是'Auto Fill Google Doc From Form'。您需要使用 onFormSubmit 触发器向工作表添加一个函数。在该功能中,您将
- 参考您的模板
- 从该模板创建一个新文档
- 生成参考号
- 使用信息更新新文档
它看起来像这样:
function autoFillGoogleDocFromForm(e) {
// Get your values from form
let timestamp = e.values[0];
let value1 = e.values[1];
let value2 = e.values[2];
// ... continue for however many values you are returning
// Get your template file
let templateFile = DriveApp.getFileById('your_file_id_here');
// Set the folder you want to create the new file in
let responseFolder = DriveApp.getFolderById('your_folder_id_for_new_doc');
// Create your reference number
let refNumber = createReferenceNumber(); // You'll need to write this
// Create your new file
let newDocName = createNewDocFileName(); // You'll need to write this
let copy = file.makeCopy(newDocName , responseFolder);
// Open the newly created document so it can be updated
let doc = DocumentApp.openById(copy.getId());
// Update the doc with your new data
let body = doc.getBody();
// The template should have placeholders {{ }} to replace the value with
body.replaceText('{{value1}}', value1);
body.replaceText('{{value2}}', value2);
body.replaceText('{{Reference Number}}', refNumber);
doc.saveAndClose();
}
当然,您需要编写一个脚本来创建参考号,以及一个用于生成文件名的脚本。(或者只是将它们在线添加,它们不必是单独的脚本。)您的问题提到您无法生成参考号。我建议创建一个新问题,其中包含您尝试过的代码以及您收到的任何错误。
推荐阅读
- asp.net-core - 在 SignalR 中间件中添加到上下文项
- sql - 提取测试运行历史记录的 TFS 数据仓库问题 - FactTestResult 表中的额外行(PointID、ChangeNumber)
- flutter - Flutter Forms:有没有办法根据某些条件更改输入文本的颜色?
- javascript - 我该怎么做才能让 Storage 中的图像知道它属于 Cloud Firestore 中的哪个文档?反应
- c - 是否将 for 循环中的一系列嵌套 if 语句统称为单个语句?
- postgresql - 尝试使用 psycopg2 undefinedTable 问题在 Heroku 上部署 Flask 应用程序
- opengl - 是否可以使用 GPU Grid 驱动程序在 Google Compute Engine 上将 OpenGL 升级到 4.3 版?
- node.js - Mongoose 查询中的嵌套 async/await
- erlang - 在 Zig 中实施 ERTS NIF 的工作流程是什么?
- fullpage.js - fullpage.js 动态添加部分:如何在部分内滚动