如何通过Google Apps Script将Google Form项目复制到新表单
Google Form 跨表单复制问题的Apps Script实现方案
核心结论
Google Apps Script 官方 Forms 服务未提供原生跨表单导入问题的接口,也不存在通用的addItem()方法可以直接导入完整条目,确实需要按项目类型逐个适配属性复制规则。不过社区已有成熟的可复用代码覆盖绝大多数常用场景,不需要从零开发。
可直接复用的实现代码
使用步骤
- 提取源表单和目标表单的ID:ID为表单URL中
d/和/edit之间的字符串 - 替换代码中对应的ID参数
- 运行
copyFormItems函数即可完成全量项目复制
function copyFormItems() { // 替换为实际的源表单ID和目标表单ID const SOURCE_FORM_ID = "替换为源表单ID"; const TARGET_FORM_ID = "替换为目标表单ID"; const sourceForm = FormApp.openById(SOURCE_FORM_ID); const targetForm = FormApp.openById(TARGET_FORM_ID); const allItems = sourceForm.getItems(); allItems.forEach(item => { const itemType = item.getType(); let newItem; switch(itemType) { // 文本题 case FormApp.ItemType.TEXT: const sourceTextItem = item.asTextItem(); newItem = targetForm.addTextItem(); newItem.setTitle(sourceTextItem.getTitle()) .setHelpText(sourceTextItem.getHelpText()) .setRequired(sourceTextItem.isRequired()) .setPoints(sourceTextItem.getPoints()); break; // 单选题 case FormApp.ItemType.MULTIPLE_CHOICE: const sourceMcItem = item.asMultipleChoiceItem(); newItem = targetForm.addMultipleChoiceItem(); newItem.setTitle(sourceMcItem.getTitle()) .setHelpText(sourceMcItem.getHelpText()) .setRequired(sourceMcItem.isRequired()) .setChoices(sourceMcItem.getChoices()) .setPoints(sourceMcItem.getPoints()) .setHasOtherOption(sourceMcItem.hasOtherOption()); break; // 多选题 case FormApp.ItemType.CHECKBOX: const sourceCbItem = item.asCheckboxItem(); newItem = targetForm.addCheckboxItem(); newItem.setTitle(sourceCbItem.getTitle()) .setHelpText(sourceCbItem.getHelpText()) .setRequired(sourceCbItem.isRequired()) .setChoices(sourceCbItem.getChoices()) .setPoints(sourceCbItem.getPoints()) .setHasOtherOption(sourceCbItem.hasOtherOption()); break; // 数值量表题 case FormApp.ItemType.SCALE: const sourceScaleItem = item.asScaleItem(); newItem = targetForm.addScaleItem(); newItem.setTitle(sourceScaleItem.getTitle()) .setHelpText(sourceScaleItem.getHelpText()) .setRequired(sourceScaleItem.isRequired()) .setBounds(sourceScaleItem.getLowerBound(), sourceScaleItem.getUpperBound()) .setLabels(sourceScaleItem.getLeftLabel(), sourceScaleItem.getRightLabel()) .setPoints(sourceScaleItem.getPoints()); break; // 网格题 case FormApp.ItemType.GRID: const sourceGridItem = item.asGridItem(); newItem = targetForm.addGridItem(); newItem.setTitle(sourceGridItem.getTitle()) .setHelpText(sourceGridItem.getHelpText()) .setRequired(sourceGridItem.isRequired()) .setRows(sourceGridItem.getRows()) .setColumns(sourceGridItem.getColumns()); break; // 图片内容 case FormApp.ItemType.IMAGE: const sourceImgItem = item.asImageItem(); newItem = targetForm.addImageItem(); newItem.setTitle(sourceImgItem.getTitle()) .setHelpText(sourceImgItem.getHelpText()) .setImage(sourceImgItem.getImage()); break; // 视频内容 case FormApp.ItemType.VIDEO: const sourceVideoItem = item.asVideoItem(); newItem = targetForm.addVideoItem(); newItem.setTitle(sourceVideoItem.getTitle()) .setHelpText(sourceVideoItem.getHelpText()) .setVideoUrl(sourceVideoItem.getVideoUrl()); break; // 分段标题 case FormApp.ItemType.SECTION_HEADER: const sourceSectionItem = item.asSectionHeaderItem(); newItem = targetForm.addSectionHeaderItem(); newItem.setTitle(sourceSectionItem.getTitle()) .setHelpText(sourceSectionItem.getHelpText()); break; // 如需适配更多类型,可在此处补充对应case逻辑 default: console.log(`未适配项目类型:${itemType},已跳过该项目`); } }); }
注意事项
- 代码已经覆盖绝大多数常用表单项目类型,如有特殊题型需求,可参照现有逻辑补充对应适配规则即可
- 测验类表单的分值设置、必填配置、其他选项开关、选项内容等属性均已纳入复制逻辑,不会遗漏配置
- 运行脚本的账号需要同时拥有源表单的查看权限和目标表单的编辑权限
内容的提问来源于stack exchange,提问作者Baron McDonald
相关产品推荐
相关产品推荐

