【问题标题】:Editing checkbox choices in an existing Google form编辑现有 Google 表单中的复选框选项
【发布时间】:2013-12-11 10:56:23
【问题描述】:

我正在尝试使用具有动态复选框答案的 Google 表单和电子表格创建调查,这样如果参与者 1 在“其他”文本框中填写了答案,那么该答案将作为复选框提供给后续响应者.

我正在使用以下代码:

//a function to update Question 2's answer options
function updateChoices() {

    //open the existing survey form  
    var form = FormApp.openById('1DEcjGr6x9KrlxapgIkFxreW1F-2Vlj_yDDzLQUhcmgk');

    //retrieve existing Question 2
    var items = form.getItems();
    var question2 = items[1];

    //retrieve previously submitted responses from response spreadsheet
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    SpreadsheetApp.setActiveSheet(ss.getSheets()[1]);
    var numRows = ss.getLastRow()-1;
    var values = SpreadsheetApp.getActiveSheet().getRange(2,16,numRows,16).getValues();

    //declare a 1D array for existing survey answers
    var answersSoFar = new Array(numRows);

    //Pass the 2D array into the 1D array.
    for (i=0; i < numRows; i++){
    answersSoFar[i] = values[i][0];     
    }

    //update the choices for the question
    question2.setChoiceValues(answersSoFar);

}

我收到以下错误消息:

TypeError: Cannot find function setChoiceValues in object Item. (line 57, file "Code")

即使setChoiceValues 方法列在类项中(第 57 行是最后一行代码)。

我还尝试在 for 循环中使用 setChoicescreateChoice 单独设置选项。

【问题讨论】:

    标签: google-apps-script google-sheets google-forms


    【解决方案1】:

    Class Item 是一个通用对象,需要在调用特定于项目的方法之前将其转换为特定的项目类型。请参阅 preamble in the documentation 获取类项目。你可以这样做:

    question2.asCheckboxItem().setChoiceValues(answersSoFar);
              ^^^^^^^^^^^^^^^^
    

    剧透警告:您的函数中还有一些其他问题。如果您想独自解决这些问题,请立即停止阅读!

    无需调用SpreadsheetApp.setActiveSheet(),因为它对这个特定脚本没有任何好处。

    使用ss.getSheets()[1] 获取特定工作表的句柄是不可靠的,因为索引取决于工作表的顺序,而这可能会发生变化。如果您需要特定的工作表,最好按名称引用它。 (如果您使用的方法采用 gridId 参数,则按 ID。)

    getRange(2,16,numRows,16).getValues(); 中的值 16 的用途是什么?也就是说,范围从P2 开始,向下延伸numRows,向右延伸16 列。否则,您似乎打算生成所有先前对问题 2 的回答的列表,应该在第 3 列或C(允许A..B 中的时间戳和问题 1 )。

    这一行,var answersSoFar = new Array(numRows);,创建了一个数组,其中有一个元素,一个数字,它等于工作表中的行数。它不会创建一个包含numRows 元素的数组。参考:JavaScript Arrays.

    遍历行以检索单元格值是有效的,但会变得丑陋且难以维护。考虑一下,如果我们转置二维数组,对问题的所有回答都将出现在同一行中……回想一下高中矩阵数学。来自Google Spreadsheet Script - How to Transpose / Rotate Multi-dimensional Array?transpose 函数将在这里为我们提供帮助。

    代码

    //a function to update Question 2's answer options
    function updateChoices() {
      //open the existing survey form  
      var form = FormApp.openById('1oaL64gVahL1wpIBeLJeRKclOHnso8Ai8sOVW9akxNhs');
    
      //retrieve existing Question 2
      var items = form.getItems();
      //var type = items[1].getType() == FormApp.ItemType.CHECKBOX;  // If you want to check, uncomment this
      var question2 = items[1].asCheckboxItem();  // Specify item type
    
      //retrieve previously submitted responses from response spreadsheet
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName("Form Responses 1");
    
      // Get all the answers to question 2, so far...
      var values = sheet.getDataRange().getValues().slice(1);  // All responses, headers sliced off
      var answersSoFar = transpose(values)[2];  // Question 2 responses
    
      //update the choices for the question
      question2.setChoiceValues(answersSoFar);
    }
    
    // See https://stackoverflow.com/a/16705104/1677912
    function transpose(a)
    {
      return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
    }
    

    【讨论】:

    • 如果您不想看到我的投票,那么现在就停止阅读(并最终关闭您的计算机)...太好了 :-) +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多