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

如何编写代码修改Google Form跳转逻辑,完成分类问卷后跳转至最后章节

修改Google Forms章节跳转逻辑至最终章节

核心思路是为每个分类章节的最后一道题目设置完成后跳转至指定的最终章节,替代默认的"下一章节"行为。以下是具体实现步骤和代码示例:

1. 确认基础代码结构

假设你的现有代码已完成以下操作:

  • 从Google Sheet读取分类及对应问卷题目
  • 创建初始章节(用于选择分类)
  • 为每个分类创建独立的问卷章节
  • 创建最终章节(如"感谢参与"页面)

2. 关键代码修改:设置章节跳转规则

在为每个分类创建完章节并添加所有题目后,定位该章节的最后一道题目,调用setGoToPage()方法指定跳转目标为最终章节。

完整代码示例

function createCategoryForm() {
  // 初始化表单与数据来源
  const form = FormApp.create("分类式问卷");
  const sheet = SpreadsheetApp.openById("你的Sheet ID").getSheetByName("题目数据");
  const data = sheet.getDataRange().getValues();
  const headers = data[0];
  const categoryCol = headers.indexOf("分类");
  const questionCol = headers.indexOf("题目");

  // 创建初始分类选择章节
  form.addSectionHeaderItem().setTitle("请选择问卷分类");
  const categoryList = [...new Set(data.slice(1).map(row => row[categoryCol]))];
  const categoryDropdown = form.addListItem().setTitle("选择分类").setChoices(
    categoryList.map(cat => FormApp.createChoice(cat))
  );

  // 创建最终章节(感谢页)
  const finalSection = form.addSectionHeaderItem().setTitle("感谢参与");
  form.addParagraphTextItem().setTitle("感谢您完成本次问卷!");
  const finalSectionIndex = form.getItems().indexOf(finalSection) + 1; // Form章节索引从1开始

  // 为每个分类创建章节并设置跳转
  categoryList.forEach(cat => {
    // 创建分类章节标题
    const catSection = form.addSectionHeaderItem().setTitle(`分类:${cat}`);
    const sectionStartIdx = form.getItems().indexOf(catSection);

    // 添加该分类下的所有题目(根据实际题目类型调整)
    const catQuestions = data.slice(1).filter(row => row[categoryCol] === cat);
    catQuestions.forEach(row => {
      form.addMultipleChoiceItem().setTitle(row[questionCol])
        .setChoices([FormApp.createChoice("是"), FormApp.createChoice("否")]);
    });

    // 找到该分类章节的最后一道题目
    let lastQuestion;
    const allItems = form.getItems();
    for (let i = sectionStartIdx + 1; i < allItems.length; i++) {
      if (allItems[i].getType() === FormApp.ItemType.SECTION_HEADER) {
        lastQuestion = allItems[i - 1];
        break;
      }
      if (i === allItems.length - 1) lastQuestion = allItems[i];
    }

    // 设置跳转至最终章节
    if (lastQuestion) {
      // 根据题目类型转换对象(需与实际题目类型匹配)
      lastQuestion.asMultipleChoiceItem().setGoToPage(finalSectionIndex);
      // 若为文本题则使用:lastQuestion.asParagraphTextItem().setGoToPage(finalSectionIndex);
    }

    // 设置初始分类选择的跳转逻辑(选分类后跳对应章节)
    const targetChoice = categoryDropdown.getChoices().find(c => c.getValue() === cat);
    targetChoice.setGoToPage(sectionStartIdx + 1);
  });

  // 设置初始章节的默认跳转(未选分类时直接到最终章节)
  categoryDropdown.setGoToPage(finalSectionIndex);
}

3. 关键细节说明

  • 章节索引规则:Google Form的章节索引从1开始,因此需要用indexOf(item) + 1转换为有效索引。
  • 题目类型匹配:不同类型的题目需转换为对应Item对象(如asMultipleChoiceItem()、asParagraphTextItem())才能调用跳转方法。
  • 定位章节最后一题:遍历表单元素,从分类章节的起始位置开始,直到遇到下一个章节标题或表单末尾,前一个元素即为该章节的最后一题。

4. 验证效果

运行代码后,打开生成的表单测试:

  1. 选择任意分类,将跳转到对应问卷章节
  2. 完成该章节题目后,会自动跳转至最终的感谢章节,而非下一个分类章节

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 21:19:51