Home > OS >  Questions cannot have duplicate choice values Google Sheet to Google Form error
Questions cannot have duplicate choice values Google Sheet to Google Form error

Time:11-16

I have a form that has a Question that depending upon an answer will direct you to further questions. I have created a section in the google form. When I am trying to update the google spreadsheet to the form using app script I am getting an error saying "Exception: Questions cannot have duplicate choice values" The sheet which has the column which is same as from the google form All of the column in the google sheet are same name used in the google form and all are dropdown in the google form I have the app script as below

const populateGoogleForms = () => {
  const GOOGLE_SHEET_NAME = "Form Data";
  const GOOGLE_FORM_ID = "1bl179GkeU58rq6jNj_aiSFlORGB8j7e36CtKE3drIk4";

  const ss = SpreadsheetApp.getActiveSpreadsheet();

  const [header, ...data] = ss.getSheetByName(GOOGLE_SHEET_NAME).getDataRange().getDisplayValues();

  const choices = {};
  header.forEach((title, i) => {choices[title] = data.map((d) => d[i]).filter((e) => e);});

  FormApp.openById(GOOGLE_FORM_ID).getItems().map((item) => ({item,values: choices[item.getTitle()],})).filter(({ values }) => values).forEach(({ item, values }) => {
      switch (item.getType()) {
        case FormApp.ItemType.CHECKBOX:
          item.asCheckboxItem().setChoiceValues(values);
          break;
        case FormApp.ItemType.LIST:
          item.asListItem().setChoiceValues(values);
          break;
        case FormApp.ItemType.MULTIPLE_CHOICE:
          item.asMultipleChoiceItem().setChoiceValues(values);
          break;
        default:
        // ignore item
      }
    });
  ss.toast("Google Form Updated !!");
};
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

From the error message of Exception: Questions cannot have duplicate choice values, for example, how about the following modification?

From:

header.forEach((title, i) => {choices[title] = data.map((d) => d[i]).filter((e) => e);});

To:

header.forEach((title, i) => {
  choices[title] = [...new Set(data.map((d) => d[i]).filter((e) => e))];
});
  • I thought that the values of data.map((d) => d[i]).filter((e) => e) might include the duplicated values. So I proposed this modification. This is just my guess.

Reference:

  • Related