【发布时间】:2026-01-21 01:35:02
【问题描述】:
我有一个表格,其中包含一个问题,根据答案将引导您提出更多问题。我在谷歌表单中创建了一个部分。当我尝试使用应用脚本将 google 电子表格更新为表单时,我收到一条错误消息“Exception: Questions cannot have duplicate selection values” 谷歌表格中的所有列都与谷歌表单中使用的名称相同,并且都是谷歌表单中的下拉列表 我有如下应用脚本
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 !!");
};
【问题讨论】:
-
您介意分享您的示例 Google 表单以便更好地复制吗?当我最终测试您的脚本时,我可以更新测试表单的选择而不会出现错误。
标签: google-apps-script google-sheets google-forms