如何通过谷歌搜索包含多个部分的表单,每个部分都有一个下拉列表,从Google表格填充

问题描述 投票: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
    删除重复值。

  • 作为另一个修改,将

    getDataFromGoogleSheets()
    修改如下怎么样?

    function getDataFromGoogleSheets() {
      const ss = SpreadsheetApp.getActiveSpreadsheet();
      const sheet = ss.getSheetByName("FormList");
      const values = sheet.getDataRange().getDisplayValues();
      const cols = Object.fromEntries(values[0].map((_, col) => {
        const [h, ...v] = values.flatMap(row => row[col] || []);
        return [h, [...new Set(v)]];
      }));
      return cols;
    }
    
  • 顺便说一句,在您的脚本中,在

    const ss = SpreadsheetApp.getActiveSpreadsheet(
    您的文本
    );
    处,参数的使用方式类似于
    your text
    。在这种情况下,
    getActiveSpreadsheet
    没有参数。请注意这一点。

© www.soinside.com 2019 - 2024. All rights reserved.