I'm working on making a Google Sheet update relevant Google Form questions from a list of cells if the header matches a question. The code runs fine, but for some reason, I continue to run into an error when using a certain set of data that is known to not have duplicates, having run it through =Unique() and used the duplicate removal data tool in 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. I've removed the imports and changed data for privacy, but the error is still replicated in this data/structure. Thank you for any insight you may have, I'm hoping someone with a bit more experience can steer me in the right direction. I'm a total novice with coding, so any insight on what I did wrong would be great as well.
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);
}
CodePudding user response:
Room #135 has 2 occurences! You have then to remove one of them.
To prevent further occurence, you can change this
updateRoomNumberUsingTitle(label, options.join().split(',').filter(onlyUnique));
and add
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}