You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script报错:问题不可包含重复选项值求助

问题根源

错误Exception: Questions cannot have duplicate choice values的核心原因是:从Google Sheets读取的选项列表中存在重复值,而Google表单的下拉/多选问题不允许选项重复。

解决方案

1. 先排查Sheet数据

检查Sheet中Static工作表对应列的选项:

  • 直接可见的重复文本
  • 因前后空格导致的“伪重复”(比如"选项A"和" 选项A")
  • 空值以外的重复内容

2. 修改代码自动去重+清理空格

在生成选项数组后,添加去重逻辑,同时对每个选项做空格清理,避免伪重复。修改后的populateQuestions函数如下:

function populateQuestions() {
  var form = FormApp.getActiveForm();
  var googleSheetsQuestions = getQuestionValues();
  var itemsArray = form.getItems();
  itemsArray.forEach(function(item){
    googleSheetsQuestions[0].forEach(function(header_value, header_index) {
      if(header_value == item.getTitle())
      {
        var choiceArray = [];
        for(j = 1; j < googleSheetsQuestions.length; j++)
        {
          const value = googleSheetsQuestions[j][header_index].trim(); // 清理前后空格
          (value != '') ? choiceArray.push(value) : null;
        }
        // 去重并保留原始顺序
        const uniqueChoices = choiceArray.filter((val, idx, arr) => arr.indexOf(val) === idx);
        // If using Dropdown Questions use line below instead of line above.
        item.asListItem().setChoiceValues(uniqueChoices);
      }
    });     
  });
}

代码说明

  • trim():清理每个选项的前后空格,解决因空格导致的伪重复问题
  • filter(...):过滤掉数组中的重复值,同时保留选项的原始顺序(如果不需要顺序,也可以用Array.from(new Set(choiceArray)),但Set会打乱顺序)

内容的提问来源于stack exchange,提问作者Skymike

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 22:50:26