我有一個包含問題的表格,根據答案將指導您進一步提問。我在谷歌表單中創建了一個部分。當我嘗試使用應用程式腳本將 google 電子表格更新為表單時,我收到一條錯誤訊息,提示“例外:問題不能有重復的選擇值”
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 !!");
};
uj5u.com熱心網友回復:
從 的錯誤資訊來看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)可能包括重復的值。所以我提出了這個修改。這只是我的猜測。
參考:
- 放
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/358803.html
