0

keep getting this annoying error, tried making a new sheet and new form and issue is repeating itself

Nov 8, 2022, 1:08:30 PM Error   Exception: Questions cannot have duplicate choice values.
    at [unknown function](Code:21:27)
    at [unknown function](Code:11:30)
    at populateQuestions(Code:10:14)
    at openForm(Code:3:3)

The script used from YouTube link

function openForm(e)
{
  populateQuestions();
}

function populateQuestions() {
  var form = FormApp.getActiveForm();
  var googleSheetsQuestions = getQuestionValues();
  var itemsArray = form.getItems();
  itemsArray.forEach(function(item){
    googleSheetsQuestions[0].forEach(function(header_value, header_index) {
      if(header_value == item.getTitle())
      {
        var choiceArray = [];
        for(j = 1; j < googleSheetsQuestions.length; j++)
        {
          (googleSheetsQuestions[j][header_index] != '') ? choiceArray.push(googleSheetsQuestions[j][header_index]) : null;
        }
        //item.asMultipleChoiceItem().setChoiceValues(choiceArray);
        // If using Dropdown Questions use line below instead of line above.
        item.asListItem().setChoiceValues(choiceArray);
      }
    });     
  });
}

function getQuestionValues() {
  var ss= SpreadsheetApp.openById('1jVxeFFi8LxHGUJ2L3suzjf9iokZN2RI5Vu1hcGCOWrc');
  var questionSheet = ss.getSheetByName('Static');
  var returnData = questionSheet.getDataRange().getValues();
  return returnData;
}

tried to follow the tutorial https://www.youtube.com/watch?v=PRJ4bKk9JE8&t=268s

1 Answers1

1

This answer should have the technical information and background needed to resolve the duplicate choice value error: Remove duplicate values from JS array

Basically, this line will not accept any duplicates from the spreadsheet of options that you are trying to populate in the form. Likely due to whatever constraints google has placed on the questionnaire type.

item.asListItem().setChoiceValues(choiceArray);

You would need to remove any duplicate values within the 'choiceArray', either by updating your input spreadsheet manually, or by removing duplicates within each dropdown menu with the code from georg above (be sure to then replace choiceArray with uniqueArray):

uniqueArray = [...new Set(choiceArray)];
item.asListItem().setChoiceValues(uniqueArray);
han
  • 11
  • 1