谷歌表单部分下拉列表从工作表填充

问题描述 投票:0回答:1

我有一个包含多个部分的谷歌表单,每个部分都有一个下拉列表。我希望从电子表格中提取具有匹配名称的下拉列表的数据。

执行日志 4:42:11PM 通知执行开始 4:42:18PM 错误
例外:问题不能有重复的选择值。 (匿名)@Code.gs:23 填充GoogleForms @ Code.gs:17

function getDataFromGoogleSheets() {
  const ss = SpreadsheetApp.getActiveSpreadsheet(`your text`);
  const sheet = ss.getSheetByName("FormList");
  const [hA, ...rows] = sheet.getDataRange().getDisplayValues();
  const cols = {};
  const col={};
  const idx={};
  hA.forEach((h, i) => { cols[h] = rows.map(r => r[i]).filter(e=>e);col[h]=i+1;idx[h]=i; });
  return cols;
}

function populateGoogleForms() {
  const GOOGLE_FORM_ID = `1cuPfuzzoEPEaIliiRKDqU`your text`vZ3dq4vawDPLERZYMUwpZQ`;
  const googleForm = FormApp.openById(GOOGLE_FORM_ID);
  const items = googleForm.getItems();
  const choices = getDataFromGoogleSheets();
  items.forEach(function (item) {
    const itemTitle = item.getTitle();
    if (itemTitle in choices) {
      const itemType = item.getType();
      switch (itemType) {
        case FormApp.ItemType.LIST:
          item.asListItem().setChoiceValues(choices[itemTitle]);
          break;
        case FormApp.ItemType.LIST:
          item.asListItem().setChoiceValues(choices[itemTitle]);
          break;
        case FormApp.ItemType.LIST:
          item.asListItem().setChoiceValues(choices[itemTitle]);
          break;
        default:
          Logger.log("Ignore question", itemTitle);
      }
    }
  });
}
google-sheets google-apps-script google-forms
1个回答
0
投票

我认为您当前出现

Exception: Questions cannot have duplicate choice values.
问题的原因可能是由于
const choices = getDataFromGoogleSheets();
的值。当每个数组的值有重复值时,会在
setChoiceValues
处出现这样的错误。为了去掉这个,当你的脚本修改的时候,作为一个简单的修改,下面的修改怎么样?请按如下方式修改您的
getDataFromGoogleSheets()

来自:

hA.forEach((h, i) => { cols[h] = rows.map(r => r[i]).filter(e=>e);col[h]=i+1;idx[h]=i; });

致:

hA.forEach((h, i) => { cols[h] = [...new Set(rows.map(r => r[i]).filter(e => e))]; col[h] = i + 1; idx[h] = i; });
  • 在此修改中,使用
    Set
    删除重复值。
© www.soinside.com 2019 - 2024. All rights reserved.