如何使用Google Apps脚本重置Google表单计数器值

时间:2013-02-22 09:05:17

标签: google-apps-script google-sheets

我有几个Google表单经常被重复使用,每次提交大约300到400个表单。

到目前为止,我有两种方法可以手动重置表单:

  1. 在保持表单编辑器打开的同时删除表单。
  2. 选择包含表单提交的行,右键单击并选择“删除...行”。
  3. 使用脚本,我没有找到像form.reset()这样的方法。 方法.DeleteRows(fromRow, to Row)只删除行,但不影响计数器。

    那么,我如何使用Google应用脚本自动执行“重置表单”任务?
    或者,如何模仿脚本中的第二种方式?

    谢谢!

2 个答案:

答案 0 :(得分:2)

Google Forms已经创建了一个新的表单编辑器(几周前发布)。这个新的表单编辑器有一个“删除所有响应”的选项。因此,一种选择是在新表单编辑器中重新创建表单。 (从新电子表格中,点击工具>创建表单)。

注意:此新的表单编辑器尚未供Google Apps用户使用。

答案 1 :(得分:1)

我有一个使用deleteRows()的工作解决方案。我不确定为什么这对你不起作用,看到你的代码会很有趣。

新的Forms产品(2013年2月)与传统表单完全不同 - 此解决方案仅适用于旧版表单

无论如何,这是我拥有的,以及之前和之后截图后。可以通过传入要删除的行数来调用tidy()函数,也可以从菜单中调用它,如此处所示。此脚本的更完整版本为available as a gist

/**
 * Standard onOpen provided in script template.
 */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{name : "Read Data",functionName : "readRows"},
                 {name : "Tidy Form Responses", functionName : "tidy"}];
  sheet.addMenu("Script Center Menu", entries);
};

/**
 * Prompt user for the number of form response rows to remove, then delete them.
 * Assumes that the form responses are in the active sheet, that there is one
 * row of 'headers' followed by responses (in row 2).
 *
 * @param {number} thisMany (Optional) The number of rows to remove. If not
 *                          specified, user will be prompted for input.
 */
function tidy(thisMany) {
  if (tidy.arguments.length == 0) {
    thisMany = Browser.inputBox("How many responses should be tidied?");
  }

  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.deleteRows(2, thisMany);
}

之前

Spreadsheet with form responses and custom menu

Script operation, inputBox

Spreadsheet with 2 fewer form responses