【问题标题】:Dynamic Editing of Form Dropdown Creates Extra Column表单下拉列表的动态编辑创建额外的列
【发布时间】:2019-04-25 21:00:02
【问题描述】:

我对用于表格/表单的 Google 脚本相对较新,并且已编辑 this 答案以创建一个脚本,该脚本可在触发下拉问题时更新其选项。它工作得很好,但有一个小故障。我的代码如下:

var idColumn = 1; 

function getSheet() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ProjectList");
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  var lr = rows.getLastRow();

  var docId = sheet.getRange(2,idColumn,lr,1).getValues(); //gets the data from the last row in selected column

  fillFormData(docId);
}

//fills form with document ID
function fillFormData(docId) {
  var formId = 'form ID goes here'
  var form = FormApp.openById(formId);
  var items = form.getItems();
  var item = items[1];
  //loop variables
  var thisValue = "";
  var arrayOfItems = [];
  var newItem = "";

  for (var i=0;i<docId.length;i++) {
    thisValue =docId[i][0];
    Logger.log('thisValue: ' + thisValue);
    newItem = item.createChoice(thisValue);
    arrayOfItems.push(newItem);
  };

  item.setChoices(arrayOfItems)

}

我的希望是,通过使用 setChoices() 更新问题,响应将被放置在响应表的同一列中。相反,此脚本的每个触发器都会在响应表中创建名为“项目编号”的另一列,覆盖之前存在的列之一。这是预期的行为,还是有人知道确保我的回复进入同一列的方法?有没有办法将对此问题的回答定向到响应表中的特定列?

编辑:根据以下建议,我尝试调整宏以使用 .asListItems() ,但没有成功。这里有趣的行为是,新的重复列似乎覆盖了我的其他列之一,而不是创建一个新列。

这是我的项目列表,只有一列:

Project Number
Project A
Project B
Project C
Project D
Project E

该表格由 35 个问题组成,有多种类型,分为 3 个部分。

【问题讨论】:

  • Google 表格宏是一种特殊的脚本类型。请仅使用标记 google-sheets-macros 来解决有关此特殊脚本类型的问题。
  • 引用的答案使用 asListitem 但您的代码不使用。 AFAIK 我们应该使用一些 asSomethingItem 来确保 Apps 脚本为我们将更改的表单项获取正确的方法。
  • 你能分享一个版本的 ProjectList 以及你的表单是什么样的吗?
  • 不可重现。 new, duplicate column seems to overwrite one of my other columns 那么您可能引用了错误的项目。从顶部到下拉菜单的数量是多少?

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


【解决方案1】:

我认为您需要明确设置您尝试添加选项的问题,请参阅下面的工作脚本示例以向表单添加新项目。

function getSheet() {
   var sheet = SpreadsheetApp.openById('sheet id here');
}

var formId = "form id here";
var question = [{formFieldTitle:"question title here", worksheetName:"linked form sheet name here"}]

function newChoices(item, sheetName) {
  var data = (SpreadsheetApp.getActiveSpreadsheet()
              .getSheetByName(sheetName)
              .getDataRange()
              .getValues());
  var choices = [];
  for (var i = 1; i < data.length; i += 1){
    choices.push(item.createChoice(data[i][0])); 
  }
  item.setChoices(choices); 
}

function addNewChoices() {
  var form = FormApp.openById(formId);
  var items = form.getItems();
  for (var i = 0; i < items.length; i += 1) {
    for (var j = 0; j < question.length; j += 1) {
      var item = items[i];
      if (item.getTitle() === question[j].formFieldTitle) {
        addNewChoices(item.asCheckboxItem(), question[j].worksheetName);
        break; 
      } 
    } 
  } 
}

变量“问题”应该有问题标题和要从中添加表单项的工作表名称。

var question = [{formFieldTitle:"question title here", worksheetName:"sheet name here"}] 

此外,如果您的问题不是下一行中的复选框,请务必更改“asCheckboxItem”,请参阅Interface Item documentation 以确定使用什么。

addNewChoices(item.asCheckboxItem(), question[j].worksheetName);

【讨论】:

    【解决方案2】:

    您应该使用setChoiceValues(),而不是使用方法createChoice()setChoices(),这样它就不会创建新问题。通过使用setChoiceValues(),您可以有效地更新选择。

    请看下面的代码:

    function optWriteItem_CHECKBOX_(paramItem, paramItemData) {
      try {
        var thisItem = paramItem.asCheckboxItem();
        var listChoices;
        var n, i;
    
        n = paramItemData.length;
        if(paramItemData.indexOf('__OTHER__') > 5) {
          listChoices = paramItemData.slice(5, n-1);
          thisItem.showOtherOption(true);
        } else {
          listChoices = paramItemData.slice(5, n);
          thisItem.showOtherOption(false);
        }
    
    
        thisItem.setTitle(paramItemData[0]);
    
        if(paramItemData[3].toLowerCase() == 'yes') thisItem.setRequired(true);
        else thisItem.setRequired(false);
    
        thisItem.setHelpText(paramItemData[4]);
    
        if(listChoices.length == 0) listChoices = [ 'Option 1' ];
        thisItem.setChoiceValues(listChoices);
      } catch(err) {
        Logger.log('optWriteAsType/opt=CHECKBOX : ' + err.message);
        console.error("optWriteItem_CHECKBOX_()", err);
        return {s:false, m:err.message};
      }
    
      return {s:true};
    }
    

    Source-code

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-17
      • 2021-05-18
      • 1970-01-01
      • 2011-06-16
      • 2015-11-06
      • 1970-01-01
      • 2018-09-12
      • 2013-07-05
      相关资源
      最近更新 更多