Google Apps 脚本错误异常:问题不能有重复的选择值。 GSheet > GForms 更新器

Google Apps Script Error Exception: Questions cannot have duplicate choice values. GSheet > GForms updater

我正在努力制作 Google Sheet 更新相关的 Google 如果 header 匹配问题,则从单元格列表中形成问题。代码 运行 很好,但由于某种原因,我在使用一组已知没有重复的数据时继续 运行 出错,通过 = 运行 Unique() 并使用了 GSheets 中的去重数据工具。 Here is a link to the GSheet for the source data. The 'Room Number' dataset is the one that is throwing the error when attempting to upload it to the Google Form, here. 为了保护隐私,我已经删除了导入和更改数据,但此 data/structure 中仍然存在错误。感谢您提供的任何见解,我希望有更多经验的人可以引导我朝着正确的方向前进。我是一个完全的编码新手,所以任何关于我做错了什么的见解也会很棒。

var ssID = "1EoTHkLlqXuZ8wf-L2rEm1PQ6NUFkO_KTMu7BrzvN-30";
var formID = "1r-mcbZCd4EDHRC-8_gpOJZw8Oaj6IzekzNIxSegEwuA";

var wsData = SpreadsheetApp.openById(ssID).getSheetByName("QTR Inspection Data");
var form = FormApp.openById(formID);


function main(){

  var labels = wsData.getRange(1,1,1,wsData.getLastColumn()).getValues()[0];
  
  labels.forEach(function(label,i){
    //Logger.log(label);
    //Logger.log(i)
    var options = wsData.getRange(2, i + 1,wsData.getLastRow()-1,1).getValues().map(function(o){ return o[0] }).filter(function(o){ return o !== ""});
    //Logger.log(options);
    updateRoomNumberUsingTitle(label,options);
  });
  //Logger.log(labels);

}



function updateRoomNumberUsingTitle(title,values) {

  var items = form.getItems();
  var titles = items.map(function(item){
    return item.getTitle();
  });

  var pos = titles.indexOf(title);
  if(pos !== -1){
    var item = items[pos];
    var itemID = item.getId();
    updateRoomNumber(itemID,values);
  }
}

function updateRoomNumber(id,values) {
    var item = form.getItemById(id);
  item.asListItem().setChoiceValues(values);
}

135 号房间出现了 2 次!然后您必须删除其中之一。

为防止再次发生,您可以更改此设置

updateRoomNumberUsingTitle(label, options.join().split(',').filter(onlyUnique));

并添加

function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}