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

Google Apps Script预填表单多选框Other字段填充失效求助

解决Google Apps Script中表单Other字段预填问题

问题原因

Google Apps Script的createResponse()方法仅支持为带“Other”选项的单选/多选框传入预设选项的值;直接传入自定义Other文本时,表单无法识别该值属于Other选项,导致预填失效。

解决方案

表单预填URL中,Other选项的自定义值需要以Other.自定义文本的格式传递(需URL编码)。我们可以先生成带预设选项的预填URL,再手动替换参数值为符合要求的格式。

示例1:单选框(MultipleChoiceItem)带Other预填

function makePrefillFormWithOther() {
  const FORM = FormApp.openById("17F_6R7QbxCNrhf3X2ednKIxuKi9rB3YfGhgcpImFEwQ");
  let formResponse = FORM.createResponse();

  let items = FORM.getItems();
  let mcItem = items[0].asMultipleChoiceItem();
  
  // 先用一个预设选项生成临时响应(确保能正常生成预填URL)
  let tempResponse = mcItem.createResponse("DEFAULT");
  formResponse.withItemResponse(tempResponse);
  let prefillUrl = formResponse.toPrefilledUrl();

  // 自定义Other内容
  const otherValue = "DOT4";
  // 获取当前表单项的entry ID
  const entryId = mcItem.getId();
  // 编码符合要求的Other值格式
  const encodedOtherValue = encodeURIComponent(`Other.${otherValue}`);

  // 替换URL中的对应参数
  prefillUrl = prefillUrl.replace(
    new RegExp(`entry.${entryId}=.*?(&|$)`),
    `entry.${entryId}=${encodedOtherValue}$1`
  );

  Logger.log(prefillUrl); 
}

示例2:多选框(CheckboxItem)带Other预填

如果是你问题中的多选框场景,只需将类型转换改为asCheckboxItem(),其余逻辑类似:

function makePrefillCheckboxWithOther() {
  const FORM = FormApp.openById("你的表单ID");
  let formResponse = FORM.createResponse();

  let items = FORM.getItems();
  let checkboxItem = items[0].asCheckboxItem();
  
  // 用一个预设选项生成临时响应
  let tempResponse = checkboxItem.createResponse(["预设选项"]);
  formResponse.withItemResponse(tempResponse);
  let prefillUrl = formResponse.toPrefilledUrl();

  const otherValue = "DOT4";
  const entryId = checkboxItem.getId();
  const encodedOtherValue = encodeURIComponent(`Other.${otherValue}`);

  prefillUrl = prefillUrl.replace(
    new RegExp(`entry.${entryId}=.*?(&|$)`),
    `entry.${entryId}=${encodedOtherValue}$1`
  );

  Logger.log(prefillUrl); 
}

说明

  • 确保目标表单项已开启“Other”选项,否则此方法无效。
  • 如果需要同时预填预设选项和Other内容,可将参数值改为编码后的预设选项1,编码后的Other.自定义文本(用逗号拼接)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 05:14:55