Google表单下拉和循环部分

时间:2017-03-25 21:43:55

标签: javascript forms loops google-sheets add

所以我有这个项目,我正在处理我需要根据Google表格中的名称创建Google表单的地方。它基本上需要流动的方式是,工作表中的每个名称都应该是第一部分中的下拉选项,然后为每个名称创建一个部分。当该人在第一部分中选择他们的名字时,他们应该被发送到标题为他们名字的部分。例如,名称1应转到标题为名称1,名称2到名称2等的部分。下面的代码将创建一个Google表单并创建包含名称的部分。引起的问题是它不会将名称添加到下拉列表中(它将创建一个没有选项的下拉列表),也不会根据答案设置目标。有什么想法吗?



function RecommendationForm(){
  var form = FormApp.create('FANS Recommendation Form');
  form.setTitle('FANS Recommendation Form')
     .setConfirmationMessage('Thanks for responding!')//SET CONFIRMATION MESSAGE LATER
     .setAllowResponseEdits(false)
     .setAcceptingResponses(true);
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Teacher and Email List');
  var startRow = 2;
  var numRows = 200;
  var dataRange = sheet.getRange(startRow, 1, numRows, 4)
  var data = dataRange.getValues();
  var whoAreYou = form.addListItem();
  whoAreYou.setTitle('Who are you?')
  var nameBreak = []
  var nameChoice = []
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var name = row[0];
    var recNumber = row[2]
    if (name != ""){
    if (recNumber != 0){
    nameBreak[i] = form.addPageBreakItem().setTitle(name);
    nameChoice[i] = name
    }}};
  whoAreYou.setChoices([
    whoAreYou.createChoice(nameChoice,nameBreak) 
  ])
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你得到了setChoices函数的语法错误。您需要创建一系列选择:

var choices = []
choices[i] = whoAreYou.createChoice(name,form.addPageBreakItem().setTitle(name)) 

然后您可以在setChoices方法中使用此数组来设置所有选择。

 whoAreYou.setChoices(choices)

有关详细信息,请查看here

以下是包含这些更改的修改后的代码,

function RecommendationForm(){
  var form = FormApp.create('FANS Recommendation Form');
  //var form = FormApp.openByUrl("https://docs.google.com/forms/d/11oaebH_BeFLAkpGjqs5EykqtNxXXaabuVCTk3uuc12s/edit")
  form.setTitle('FANS Recommendation Form')
     .setConfirmationMessage('Thanks for responding!')//SET CONFIRMATION MESSAGE LATER
     .setAllowResponseEdits(false)
     .setAcceptingResponses(true);
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet9');
  var startRow = 2;
  var numRows = 10;
  var dataRange = sheet.getRange(startRow, 1, numRows, 4)
  var data = dataRange.getValues();
  var whoAreYou = form.addListItem();
  whoAreYou.setTitle('Who are you?')
  var nameBreak
  var nameChoice
  var choices = []
  var choiceCounter = 0
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var name = row[0];
    var recNumber = row[2]
    if (name != ""){
    if (recNumber != 0){
    nameBreak = form.addPageBreakItem().setTitle(name);
    nameChoice = name
    choices[choiceCounter] = whoAreYou.createChoice(nameChoice,nameBreak)  // Create a array of choices with the navigation item 
    choiceCounter++;                                        // This counter will be independent of i counter and will be have no breaks. 
    }}

  };
  whoAreYou.setChoices(choices)  //Set Choices with the choice array. 
  Logger.log(form.getEditUrl())
  Logger.log(form.getPublishedUrl())
}

希望这有帮助!