I have written this script to my spreadsheet which is connected to a google form too, and connected dropdown function to a button on spreadsheet so as be easier to run the script. first it worked well but now it is saying TypeError: Cannot read property 'getId' of undefined (line 34, file "Code")
(this line was 34 -->var itemID = item.getId();
)
I do not understand what is going wrong in this script.
how can I fix this?
Thank you for your help in advance!!!
var ssID = "1Z7uUBnzS5zKnxGswKKNAgn2XbRunqIzQZMATDl7skoE";
var formID = "1LxORhL2WgU6teJ11ax7_HsFxhbpXbjtFXiuPH5qfEfA";
var wsData = SpreadsheetApp.openById(ssID).getSheetByName("Questions#1");
var form = FormApp.openById(formID);
function dropdown() {
var labels = wsData.getRange(1, 1, 1, wsData.getLastColumn()).getValues()[0];
labels.forEach(function(label,i){
var options = wsData
.getRange(2, i+1,wsData.getLastRow()-1,1)
.getValues()
.map(function(o){ return o[0] })
.filter(function(o){ return o !== "" });
updateDropdownUsingTitle(label,options);
});
}
function updateDropdownUsingTitle(title,values) {
var items = form.getItems();
var titles = items.map(function(item){
return item.getTitle();
});
var pos = titles.indexOf(title);
var item = items[pos];
var itemID = item.getId(pos);
updateDropdown(itemID,values);
}
function updateDropdown(id,values) {
var item = form.getItemById(id);
item.asListItem().setChoiceValues(values);
}```