问题不能有重复的选择值 Google Sheet 到 Google 格式错误

Questions cannot have duplicate choice values Google Sheet to Google Form error

我有一个表格,其中有一个问题,根据答案会引导您进一步提问。我在 google 表格中创建了一个部分。当我尝试使用应用程序脚本将 google spreadsheet 更新为表单时,我收到一条错误消息“异常:问题不能有重复的选择值 google sheet 中的所有列都与 google 表单中使用的名称相同,并且都是 google 表单中的下拉列表 我有如下的应用程序脚本

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 !!");
};

Exception: Questions cannot have duplicate choice values的错误信息来看,比如下面的修改如何?

发件人:

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

收件人:

header.forEach((title, i) => {
  choices[title] = [...new Set(data.map((d) => d[i]).filter((e) => e))];
});
  • 我认为 data.map((d) => d[i]).filter((e) => e) 的值可能包含重复值。所以我提出了这个修改。这只是我的猜测。

参考: