简体   繁体   English

Google AppScript - 使用 appscript 更新谷歌表单

[英]Google AppScript - Update google Form using appscript

I have the following scenario我有以下场景

I currently update my form using appscript which is working fine however I have this problem , my form has question to select a country (from drop down list of countries ) and each option has each section mapped , (say if I select country as Netherlands whilst filling the form , it will take me to next section to select the town in Netherlands in the specified section ), so what happens now is whenever there is new country added in the sheet its updated in the form via below script and it works fine but it resets the section map that is set for each country , so we have to redo the section based answer again !我目前使用 appscript 更新我的表单,它工作正常但是我有这个问题,我的表单有问题要选择一个国家(从国家下拉列表中),并且每个选项都映射了每个部分,(比如如果我选择国家作为荷兰,同时填写表格,它将带我到下一部分以在指定部分中选择荷兰的城镇),所以现在发生的情况是,只要在表格中添加了新的国家,它就会通过下面的脚本在表格中进行更新并且工作正常,但是它会重置为每个国家/地区设置的部分地图,因此我们必须再次重做基于部分的答案! wanted to understand is there any way that to avoid resetting the section based options thats set.想了解有什么方法可以避免重置设置的基于部分的选项。

Code代码

 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); }

You can use setChoices with createChoice(value, navigationItem) instead of setChoiceValues to add navigation to choices.您可以使用setChoicescreateChoice(value, navigationItem)而不是 setChoiceValues 来为选择添加导航。 Create a new column (in this example, it's column B) with the section ID for the section you'd like to navigate for that country.创建一个新列(在本例中为 B 列),其中包含您要为该国家/地区导航的部分的部分 ID。 Then use the code below to update the form.然后使用下面的代码更新表单。

  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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM