如何编写脚本实现Google Form文本的批量查找替换?
Google Form 批量查找替换文本脚本实现
完全可以通过Google Apps Script实现批量查找替换,以下是具体方案:
操作步骤
- 打开目标Google Form,点击右上角的「脚本编辑器」(代码方块图标)
- 清空编辑器默认代码,粘贴下方脚本
- 修改脚本开头的
oldText和newText变量,替换为你需要的旧文本和新文本 - 点击顶部「运行」按钮,首次运行需按提示完成权限授权
批量替换脚本
function batchReplaceFormText() { const oldText = "Central High School"; const newText = "North Middle School"; const form = FormApp.getActiveForm(); // 替换表单标题与描述 form.setTitle(form.getTitle().replaceAll(oldText, newText)); form.setDescription(form.getDescription().replaceAll(oldText, newText)); // 替换所有章节(分页)的标题与帮助文本 const sections = form.getItems(FormApp.ItemType.PAGE_BREAK); sections.forEach(section => { const pageBreak = section.asPageBreakItem(); pageBreak.setTitle(pageBreak.getTitle().replaceAll(oldText, newText)); pageBreak.setHelpText(pageBreak.getHelpText().replaceAll(oldText, newText)); }); // 替换所有问题的标题、帮助文本,及选择题选项 const allItems = form.getItems(); allItems.forEach(item => { item.setTitle(item.getTitle().replaceAll(oldText, newText)); item.setHelpText(item.getHelpText().replaceAll(oldText, newText)); // 处理选择题、复选框、下拉框的选项文本 if ([FormApp.ItemType.MULTIPLE_CHOICE, FormApp.ItemType.CHECKBOX, FormApp.ItemType.DROPDOWN].includes(item.getType())) { const choiceItem = item.asChoiceItem(); const updatedChoices = choiceItem.getChoices().map(choice => { return FormApp.createChoice(choice.getValue().replaceAll(oldText, newText)); }); choiceItem.setChoices(updatedChoices); } }); // 替换提交后的确认页面文本 const confirmationMsg = form.getConfirmationMessage(); form.setConfirmationMessage(confirmationMsg.replaceAll(oldText, newText)); Logger.log("批量替换完成"); }
注意事项
- 运行前建议复制表单备份,避免意外修改无法恢复
- 脚本覆盖范围:表单标题/描述、所有章节的标题/帮助文本、所有问题的标题/帮助文本、选择题/复选框/下拉框的选项、提交确认信息
- 如需区分大小写替换,可将
replaceAll改为正则替换,示例:replace(new RegExp(oldText, 'g'), newText)(旧文本含正则特殊字符时需提前转义)
内容的提问来源于stack exchange,提问作者tsb
相关产品推荐
相关产品推荐

