Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 表单下拉列表的动态编辑会创建额外的列_Google Apps Script_Google Sheets_Google Forms - Fatal编程技术网

Google apps script 表单下拉列表的动态编辑会创建额外的列

Google apps script 表单下拉列表的动态编辑会创建额外的列,google-apps-script,google-sheets,google-forms,Google Apps Script,Google Sheets,Google Forms,我对谷歌表单脚本比较陌生,我已经编辑了答案,创建了一个脚本,每当触发下拉式问题时,它都会更新选项。它工作得非常好,但有一个小故障。我的代码如下: var idColumn = 1; function getSheet() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ProjectList"); var rows = sheet.getDataRange(); var numRows = r

我对谷歌表单脚本比较陌生,我已经编辑了答案,创建了一个脚本,每当触发下拉式问题时,它都会更新选项。它工作得非常好,但有一个小故障。我的代码如下:

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)

}

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

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

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; 
      } 
    } 
  } 
}
此外,如果您的问题不是下一行中的复选框,请确保更改“asCheckboxItem”,以确定使用什么

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

不要使用方法
createChoice()
setChoices()
,而应该使用
setChoiceValues()
,这样它就不会产生新问题。通过使用
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};
}

Google Sheets宏是一种特殊的脚本类型。对于有关此特殊脚本类型的问题,请仅使用标记google sheets宏。引用的答案使用asListitem,但您的代码不使用。好的,我们应该使用一些
asSomethingItem
,以确保应用程序脚本为我们将要更改的表单项获得正确的方法。能否共享ProjectList的版本以及表单的外观?不可复制<代码>新建的重复列似乎覆盖了我的其他列之一您可能引用了错误的项。顶部下拉项的数量是多少?
addNewChoices(item.asCheckboxItem(), question[j].worksheetName);
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};
}