How to access Spreadsheet from a Google Forms submit trigger function

时间:2015-06-25 19:13:31

标签: javascript google-apps-script google-sheets google-spreadsheet-api

I have a function in Google Forms script to add a Unique ID to the row being added to the associated Sheet. This function is triggered on submission of the form. Here's one version of the script I've been playing around with:- function onFormSubmit(e) { // Get the active sheet var sheet = SpreadsheetApp.getActiveSheet(); // Get the active row var row = sheet.getActiveCell().getRowIndex(); // Get the next ID value. var id = sheet.getRange("Z4").getValue(); // Check of ID column is empty if (sheet.getRange(row, 1).getValue() == "") { // Set new ID value sheet.getRange(row, 1).setValue(id); sheet.getRange("Z4").setValue("Z4"+1) } } On debugging I get the message:- TypeError: Cannot call method "getActiveCell" of null. (line 5, file "CreateID") I've tried cropping the code right down to just a simple setValue, and I get the same issue - "cannot call method...", with pretty much every line except the getActiveSheet. The trigger of the function works ok, as I get notifications to say that the function itself had failed to execute successfully. I've looked online, tried a few things, but can't find a solution as yet. So what I'm really after is the correct method of accessing the spreadsheet that the form is posting to. If SpreadsheetApp.getActiveSheet() isn't the right method, what is? Totally new to this script, last programmed in PLI(!), sorry. Any pointers to existing solutions or other, would be appreciated.

2 个答案:

答案 0 :(得分:2)

提交表单时,触发器功能没有活动的电子表格,也没有活动的单元格。 (它与Sheets UI无关,因此这些概念毫无意义。)

但是,事件参数e将提供有关表单添加的行的信息。见Google Sheets events

screenshot

e.range包含一个Range对象,其中包含已触发您的函数的表单提交填充的单元格。您可以从那里回溯以获取表格。

sheet = e.range.getSheet();

你的功能变成这样:

function onFormSubmit(e) {
  // Get the active sheet
  var sheet = e.range.getSheet();
  // Get the active row
  var row = e.range.getRowIndex();
  // Get the next ID value.  
  var id = sheet.getRange("Z4").getValue();
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
    // Set new ID value
    sheet.getRange(row, 1).setValue(id);
    sheet.getRange("Z4").setValue("Z4"+1)
  }
}

现在,您还有其他问题要处理。

  • sheet.getRange(row, 1).getValue()没有必要;该值刚刚以e.values[0]传递给您。

  • 但是,在表单提交时,第一列包含时间戳,因此它不会是空的。如果你的" ID" value是表单上的第一个问题,然后它实际上在第2列,或e.values[1]

  • 单元格Z4可能会向您移动,因为表单提交会在表单中插入新行。从不同的工作表中提取该值会更好 - 或者更好地使用“属性服务”来管理它。

要使用该事件,您需要模拟它以进行测试。阅读How can I test a trigger function in GAS?

答案 1 :(得分:-2)

您需要检查getActiveSheet()是否成功。

function onFormSubmit(e) {
  // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  if (!sheet) { // No active sheet, nothing to do
    return;
  }
  // Get the active row
  var row = sheet.getActiveCell().getRowIndex();
  // Get the next ID value.  
  var id = sheet.getRange("Z4").getValue();
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
    // Set new ID value
    sheet.getRange(row, 1).setValue(id);
    sheet.getRange("Z4").setValue("Z4"+1)
  }
}