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

Google Apps Script更新Google Form选项不生效问题求助

Google Apps Script更新Google Form选项不生效问题

我正在开发一个Google Apps Script,用于根据关联Google Sheet中的提交响应动态更新Google Form的选项。脚本运行无错误,执行日志显示结果正确(例如4 PM场次已满,仅应保留7 PM场次),但运行脚本并刷新表单后,4 PM选项仍在Google Form中可见。

我想咨询:

  • 是否有人遇到过脚本运行后表单未反映更新选项的问题?
  • 是否存在缓存、权限或Google Apps Script的setChoices方法相关的潜在问题?
  • 有哪些额外的调试步骤或确保表单正确更新的建议?

我的脚本

function autoLimitBySession() {
  const FORM_ID = "1Gab3Obs0xooR6tmnlE0tPDOWgK5iY1jeQHDjCb6373M"; // Replace with your Google Form ID
  const LIMIT_PER_SESSION = 1; // Limit per session
  const SESSION_COLUMN = 7; // Column G: "Choose 1 Music Party Session"
  const SHEET_NAME = "Form Responses 1"; // Replace with your sheet name

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME);
  const data = sheet.getDataRange().getValues();

  let count4PM = 0;
  let count7PM = 0;

  // Count responses
  for (let i = 1; i < data.length; i++) {
    const session = data[i][SESSION_COLUMN - 1];
    if (session.includes("4pm (Limited to 10 Kids)")) count4PM++;
    if (session.includes("7pm (Limited to 10 Kids)")) count7PM++;
  }

  Logger.log(`Current counts: 4 PM = ${count4PM}, 7 PM = ${count7PM}`);
  Logger.log(`Remaining slots: 4 PM = ${LIMIT_PER_SESSION - count4PM}, 7 PM = ${LIMIT_PER_SESSION - count7PM}`);

  const form = FormApp.openById(FORM_ID);

  if (count4PM >= LIMIT_PER_SESSION && count7PM >= LIMIT_PER_SESSION) {
    form.setAcceptingResponses(false);
    form.setCustomClosedFormMessage("All sessions are fully booked. Thank you!");
    Logger.log("Form closed: All sessions are full.");
    return;
  }

  const items = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE);
  items.forEach(item => {
    const title = item.getTitle();
    if (title.includes("Choose 1 Music Party Session")) {
      const sessionItem = item.asMultipleChoiceItem();
      const choices = [];

      if (count4PM < LIMIT_PER_SESSION) {
        choices.push(sessionItem.createChoice("4pm (Limited to 10 Kids)"));
      }
      if (count7PM < LIMIT_PER_SESSION) {
        choices.push(sessionItem.createChoice("7pm (Limited to 10 Kids)"));
      }

      if (choices.length === 0) {
        choices.push(sessionItem.createChoice("All sessions are fully booked"));
      }

      Logger.log(`Updated choices: ${choices.map(choice => choice.getValue())}`);
      sessionItem.setChoices(choices);
    }
  });
}

已尝试的操作

  • 手动运行脚本并刷新表单(包括Ctrl + F5强制刷新和隐身模式访问)
  • 验证FORM_ID正确,与目标Google Form完全匹配
  • 检查Google Form的问题标题,确保与脚本中的判断逻辑一致
  • 添加详细日志,确认脚本正确处理数据并生成预期的更新选项

预期效果

  • 若4pm场次被选择提交至少1次,从Google Form中移除该选项
  • 若7pm场次被选择提交至少1次,从Google Form中移除该选项
  • 若两场次均达上限,关闭Google Form

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 23:53:14