这是构建项目的链接:https://drive.google.com/open?id=1IbmEWv_y60n-IefIrjmTcND-E6Rv35vb
目标
最初提交表单时创建文档。然后,在使用表单编辑回复时,为“表单回复 1”工作表上的每个受影响的行创建一个新文档。
当前结果
每次脚本运行时都会为工作表上的所有行创建文档。各个文档包含正确的数据,但也会为未更改值的行创建文档。
问题
我对脚本缺乏经验,无法找到仅当行中的值发生更改时才创建文档的方法。
我尝试了使用“在表单提交时”作为触发器的脚本以外的其他方法,但编辑响应导致创建的文档忽略了任何未编辑的响应。换句话说,如果表单收集了 Q1 和 Q2 的数据,则创建的第一个文档将包括表单上输入的所有内容,如预期的那样。但是,如果我编辑 Q1 的表单响应,新创建的文档将完全忽略 Q2,只显示 Q1。
我还尝试使用“编辑时”作为触发器。使用表格时这根本不起作用。它需要直接编辑“Form Responses 1”表单,所以不能满足我的需求。
帮忙?
当前触发器和脚本
触发器:表单提交时
function setUp() {
//IDs to get and open
var tid = "1Us7qyNjFTeTrrA2Xt_Yigks1Yn-n0KGre9rUn3UV5yc";
var fid = "1HVy7J7EsgKfX-BjgzOGgXlcmYDuLs5C_";
var sid = "1FrJpfE9L2ZRE76ABDWA0gfYaZd6RFkvGQkSipHFCZCU";
var sname = "Form Responses 1";
//Get the template doc
var template = DriveApp.getFileById(tid);
//Get the output folder
var folder = DriveApp.getFolderById(fid);
//Open the form response worksheet
var ws = SpreadsheetApp.openById(sid).getSheetByName(sname);
//Get the worksheet range
//starting row #, starting column #, last row, # of columns
var range = ws.getRange(2,1,ws.getLastRow()-1,7).getValues();
range.forEach(function(r){
//Name the data
var ts = r[0];
var email = r[1];
var q1 = r[2];
var q2 = r[3];
var q3 = r[4];
var q4 = r[5];
var q5 = r[6];
//Format the timestamp
var date = Utilities.formatDate(new Date(ts), "GMT-5", "yyyy-MM-dd");
createDoc(template,folder,date,email,q1,q2,q3,q4,q5);
});
}
function createDoc(template,folder,date,email,q1,q2,q3,q4,q5) {
//Copy the template, name the doc, and put it in the folder
var copy = template.makeCopy(q1 + ' - ' + date, folder);
//Open the new document
var doc = DocumentApp.openById(copy.getId());
//Get and update the body of the document
var body = doc.getBody();
body.replaceText('##Date##', date);
body.replaceText('##Email##', email);
body.replaceText('##Question1##', q1);
body.replaceText('##Question2##', q2);
body.replaceText('##Question3##', q3);
body.replaceText('##Question4##', q4);
body.replaceText('##Question5##', q5);
//Save and close the document to persist our changes
doc.saveAndClose();
}
最佳答案
当您使用onFormSubmit(e)
时,您的方法是正确的,是的,e对象为您提供了编辑后的单元格,但不是其他值,我为解决该问题所做的就是使用带来 e 的 range> 对象来定位编辑的行,然后使用 e.range.getRow()
找到它,其余的将按照您的操作完成。
// Create a new document with the submitted data in the sheet
function createDoc(data){
//Get the output folder
var folder = DriveApp.getFolderById("your folder ID");
//Get the template doc
var template = DriveApp.getFileById("your file ID");
//Copy the template, name the doc, and put it in the folder
var copy = template.makeCopy(data[0][2] + ' - ' + data[0][0], folder);
//Open the new document
var doc = DocumentApp.openById(copy.getId());
//Get and update the body of the document
var body = doc.getBody();
body.replaceText('##Date##', data[0][0]);
body.replaceText('##Email##', data[0][1]);
body.replaceText('##Question1##', data[0][2]);
body.replaceText('##Question2##', data[0][3]);
body.replaceText('##Question3##', data[0][4]);
body.replaceText('##Question4##', data[0][5]);
body.replaceText('##Question5##', data[0][6]);
//Save and close the document to persist our changes
doc.saveAndClose();
}
// This will be trigger every time the form is submitted
function onFormSubmit(e) {
// Get active sheet in the active spreadsheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var numberCols = 7;
// Get values in the row that was edited
var editedRow = sheet.getRange(e.range.getRow(), 1, 1, numberCols).getValues();
// Create a new doc
createDoc(editedRow);
}
正如您所看到的,我对您的代码进行了一些更改,您可以像这样使用它或按照您想要的方式调整它。请记住,将来您可以检查 data
数组内的值来验证它们是否为空。
文档
有关详细信息,您可以检查这些内容并注意 Instabble Triggers 限制:
关于google-apps-script - 编辑表单响应时为工作表上更改的行创建 Google 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58956934/