0

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);
}```  

0

The problem is in the line

var labels = wsData.getRange(1, 1, 1, wsData.getLastColumn()).getValues()[0]

As it's returning Bobur CEO instead of the expected values.

Replace the above line by

var labels = wsData.getRange(1, 1, 1, wsData.getLastColumn()).getValues().map(function(row){
  return row[0];
});

.map(function(row){return row[0];}) will flatten the result of .getValues().

| improve this answer | |
  • As you can see there several columns with questions, and I wanted this script to apply all changes to the form question (all in dropdown format) when I add new answers to the existing questions and/or add new questions next to existing ones. #1 the problem after replacing the line You said the changes are being applied to only the first question. #2 2nd problem is that as you can see there is a RUN button on Questions#1 sheet. after making changes I use this button to run the script so as to apply changes. But the message Finished script appears whenever I click on it – Aroon Advisory Group Mar 21 at 23:28
  • then changes are not applied if I use this button. Please have a look on the Questions#1. the first row of each column(each column is a different question) is a separate question under which there are the answers to it. – Aroon Advisory Group Mar 21 at 23:34
  • @AroonAdvisoryGroup I think that I correctly answered your question. If you have follow-up questions, please post a new question. – Rubén Mar 22 at 0:35
  • but when i run the other two function it is sating there are still errors in the line 31 and 38 – Aroon Advisory Group Mar 22 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.