Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 编辑表单响应时,为工作表上更改的行创建Google文档_Google Apps Script - Fatal编程技术网

Google apps script 编辑表单响应时,为工作表上更改的行创建Google文档

Google apps script 编辑表单响应时,为工作表上更改的行创建Google文档,google-apps-script,Google Apps Script,以下是构建项目的链接: 目标 初始提交表单时创建文档。然后,在使用表单编辑响应时,为工作表“表单响应1”上每个受影响的行创建一个新文档 当前结果 每次脚本运行时,都会为工作表上的所有行创建文档。单个文档包含正确的数据,但也会为未更改值的行创建文档 问题 我对脚本缺乏经验,无法找到仅在行中的值发生更改时创建文档的方法 我尝试了一种不同于下面脚本的方法,使用“On form submit”作为触发器,但编辑响应会导致创建忽略任何未编辑响应的文档。换句话说,如果表单收集了第一季度和第二季度的数据

以下是构建项目的链接:


目标 初始提交表单时创建文档。然后,在使用表单编辑响应时,为工作表“表单响应1”上每个受影响的行创建一个新文档


当前结果 每次脚本运行时,都会为工作表上的所有行创建文档。单个文档包含正确的数据,但也会为未更改值的行创建文档


问题 我对脚本缺乏经验,无法找到仅在行中的值发生更改时创建文档的方法

我尝试了一种不同于下面脚本的方法,使用“On form submit”作为触发器,但编辑响应会导致创建忽略任何未编辑响应的文档。换句话说,如果表单收集了第一季度和第二季度的数据,那么创建的第一个文档将包括表单中输入的所有内容,正如预期的那样。但是,如果我编辑了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(); 
}

我只是想提供一种存储上次配置的方法,这样您就可以确定触发器之间是否进行了更改,从而知道是否要生成新文档

SpreadsheetApp.getActive().getSheetByName('ConfigSheet').appendRow([tid,fid,date,email,q1,q2,q3,q4,q5]); 
我会这样做,因为获取最后一次配置非常简单

var sh=SpreadsheetApp.getActive().getSheetByName('ConfigSheet'); 
sh.getRange(sh.getLastRow(),1,1,sh.getLastColumn()).getValues()[0];` 

现在,您在平面阵列中拥有了所有最新的配置设置。但是我不知道什么样的更改值得创建一个新文档。

当您使用
onFormSubmit(e)
时,您的方式是正确的,是的,e对象为您提供了编辑过的单元格,但没有提供其他值,我解决这个问题的方法是使用带e对象的range来定位编辑的行,然后使用
e.range.getRow()
来查找它,其余的操作将按照您的操作进行

SpreadsheetApp.getActive().getSheetByName('ConfigSheet').appendRow([tid,fid,date,email,q1,q2,q3,q4,q5]); 
// 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);
}
如您所见,我对您的代码做了一些更改,您可以这样使用它,也可以按照您希望的方式进行修改。请记住,以后您可以检查
数据
数组中的值,以验证它们是否为空

文件 有关更多信息,请查看这些选项,并小心Instabble触发器限制:


您能提供一个带有一些值的示例表吗?这将有助于更好地画出<代码> WS>代码中的值。我会考虑使用电子表格来存储最后一个配置,将它附加到一个电子表格中,比如“代码> var SPARESETAPAP.GETAccvIe().GETSHITEBYNAMY(“配置文件”)。AppEdRead([TID,FID,Dead,Email,Q1,Q2,Q3,Q4,Q5])< /C>我之所以这样做,是因为获取最后一次配置非常简单,如
var sh=SpreadsheetApp.getActive().getSheetByName('ConfigSheet');sh.getRange(sh.getLastRow(),1,1,sh.getLastColumn()).getValues()[0]现在您在平面阵列中拥有了所有最新的配置设置。我从您的代码中猜测,您在检查更改时不会遇到问题。@albertovielma我已在查询的顶部添加了一个指向该项目的链接。@Cooper,我不完全确定您的意思。。。你远远超出了我的知识水平。你能进一步解释一下你的建议是如何起作用的吗?或者你能把它包含在一个即插即用的脚本中让我试试吗?