根据用户输入自动填充Google表单

时间:2017-02-23 17:05:30

标签: google-apps-script

好朋友,

我正在使用谷歌脚本开展我的第一个项目,到目前为止它非常有趣。我的项目是创建一个数据输入表单,可以接受ID号并填写其余字段,或者让用户填写整个表单。基本上我填写其他字段的方法只是在第二张表上有一个查找表。当用户提交表单时,脚本运行,查找最后一行的ID,扫描引用表中的ID,然后填写详细信息。

认为我遇到的问题是假设当脚本运行时表单中的数据已经在表单中。我注意到的问题是脚本有时无法填补空白。我尝试在具有相同ID的循环中创建表单提交,并且它们的功能有些不规律但似乎最后的总结仍然有效,如果脚本执行与表单提交不匹配则有意义。这是脚本供参考:

function fillGaps() {

  // First take in the appropriate spreadsheet objects and get the sheets from it
  var ss = SpreadsheetApp.openById(id);
  var sheet = ss.getSheets()[0];
  var refSheet = ss.getSheets()[1];

  // Here's the last rows' index
  var lastRow = sheet.getLastRow();
  var lastRowRef = refSheet.getLastRow();

  // now this is an array of values for the last row and the student ID entered
  var response = sheet.getRange(lastRow, 1, 1, 7).getValues();
  var enteredID = response[0][1];

  // Next we're going to try to load up the lookup table and scan for the ID
  var stuIDs = refSheet.getRange(2, 4, refSheet.getLastRow()).getValues();
  var row = 0;
  while(enteredID != stuIDs[row] && row <= lastRowRef){
    row++;
  }

  // Okay at this point the row variable is actually -2 from what the sheet index
  // is that I'm thinking of. This is because we didn't load the first row (names)
  // and the way arrays are indexed starts with 0.
  row++;
  row++;

  // now assuming that it found a match we'll fill in the values
  if(row < refSheet.getLastRow()){

    // Alright now we need to wrangle that row and format the data
    var matchedRow = refSheet.getRange(row, 1, 1, 6).getValues();
    // modify the response
    var replacement = [response[0][0],enteredID, matchedRow[0][1],matchedRow[0][0],matchedRow[0][2],matchedRow[0][4],matchedRow[0][5]];
    sheet.getRange(lastRow, 1, 1, 7).setValues([replacement]) ;
  }

}

所以我想知道:

  • 这看起来是正确的诊断吗?
  • 如果是这样,最好的补救方法是什么?我想在脚本中添加一点延迟以及尝试捕获提交时间戳(不知道该怎么做)

非常感谢!

0 个答案:

没有答案