【问题标题】:Google AppScript - Update google Form using appscriptGoogle AppScript - 使用 appscript 更新谷歌表单
【发布时间】:2020-06-21 11:48:46
【问题描述】:

我有以下场景

我目前使用 appscript 更新我的表单,它工作正常但是我有这个问题,我的表单有问题选择一个国家(从国家的下拉列表中)并且每个选项都映射了每个部分,(比如如果我选择国家作为荷兰,在填写表格时,它会带我到下一部分在指定部分中选择荷兰的城镇),所以现在发生的情况是,只要在表格中添加了新国家,它就会通过下面的脚本在表格中更新,它工作正常,但它重置了为每个国家/地区设置的部分地图,因此我们必须再次重做基于部分的答案!想了解是否有任何方法可以避免重置已设置的基于部分的选项。

代码

var ssID = "sheet ID"; //Global Variable sheet ID

// This is start of function1 to update form Monthly 
function formUpdateCC() {
  // call your form and connect to the drop-down item
  var form = FormApp.openById("form ID");

  var country = form.getItemById("ItemID").asListItem();

  // identify the sheet where the data resides needed to populate the drop-down - Countrylist
  var countrysheet = SpreadsheetApp.openById(ssID).getSheetByName("Country");

  // grab the values in the first column of the country tab in sheet - use 2 to skip header row
  var countrylist = countrysheet.getRange(2, 1, countrysheet.getMaxRows() - 1).getValues();

  var data = [];

  // convert the array ignoring empty cells
  for (var i = 0; i < countrylist.length; i++)
    if (countrylist[i][0] != "")
      data[i] = countrylist[i][0];

  country.setChoiceValues(data);

}

【问题讨论】:

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


    【解决方案1】:

    您可以使用 setChoicescreateChoice(value, navigationItem) 而不是 setChoiceValues 来为选项添加导航。创建一个新列(在此示例中为 B 列),其中包含您要导航到该国家/地区的部分的部分 ID。然后使用下面的代码更新表单。

      var form = FormApp.openById("Form Id");
      var country = form.getItemById("List Id").asListItem();
    
      // identify the sheet where the data resides needed to populate the drop-down - Countrylist
      var countrysheet = SpreadsheetApp.openById(ssID).getSheetByName("Country");
    
      // create a new column next to the country name for its target pagebreak ID from the form
      // grab the values in the first & second columns of the country tab in sheet - use 2 to skip header row
      var countrylist = countrysheet.getRange(2, 1, countrysheet.getMaxRows() - 1).getValues();
      var targetlist = countrysheet.getRange(2, 2, countrysheet.getMaxRows() - 1).getValues();
    
      var data = [];
    
      // convert the array ignoring empty cells
      var s = 0;
      for (var i = 0; i < countrylist.length; i++)
        if (countrylist[i][0] != "") {
      // use createChoice(value, navigationItem) to add the section navigation
          data[s] = country.createChoice(countrylist[i][0], form.getItemById(targetlist[i][0]).asPageBreakItem());
          s++;
        }
    
      country.setChoices(data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 2014-09-13
      • 2020-03-11
      相关资源
      最近更新 更多