Google Apps Script 如何在脚注编号之间实现光标前后跳转?
实现Google文档脚注上标顺序跳转的Apps Script方案
以下代码可以实现自定义菜单按钮点击,按顺序逐步跳转到文档所有脚注上标位置,遍历完成后自动循环回到第一个脚注。
完整代码
// 打开文档时自动创建自定义菜单 function onOpen() { const ui = DocumentApp.getUi(); ui.createMenu('脚注跳转工具') .addItem('跳转到下一个脚注上标', 'jumpToNextFootnote') .addToUi(); } // 跳转至下一个脚注上标位置的核心逻辑 function jumpToNextFootnote() { const activeDoc = DocumentApp.getActiveDocument(); const allFootnotes = activeDoc.getFootnotes(); const userProps = PropertiesService.getUserProperties(); // 无脚注时提示 if (allFootnotes.length === 0) { DocumentApp.getUi().alert('当前文档未检测到任何脚注'); return; } // 读取当前跳转进度,首次使用初始化为0 let currentFootnoteIndex = Number(userProps.getProperty('footnoteJumpIndex') || 0); // 获取当前脚注对应的正文上标引用 const footnoteSuperscript = allFootnotes[currentFootnoteIndex].getFootnoteReference(); // 将光标定位到上标位置 activeDoc.setCursor(activeDoc.newPosition(footnoteSuperscript, 0)); // 更新下一次跳转的索引,超出总数自动回到开头循环 currentFootnoteIndex = (currentFootnoteIndex + 1) % allFootnotes.length; userProps.setProperty('footnoteJumpIndex', currentFootnoteIndex.toString()); }
使用步骤
- 打开目标Google文档,点击顶部菜单栏「扩展程序」→「Apps Script」进入脚本编辑器
- 清空编辑器默认的示例代码,粘贴上述完整代码后点击保存,自定义项目名称后确认
- 回到Google文档页面刷新,等待数秒后顶部会出现新的菜单栏选项「脚注跳转工具」
- 点击「脚注跳转工具」→「跳转到下一个脚注上标」即可按顺序跳转,首次使用需要根据提示完成脚本授权
- 所有脚注遍历完成后,再次点击会自动回到第一个脚注上标位置循环跳转
内容的提问来源于stack exchange,提问作者Chris_toph
相关产品推荐
相关产品推荐

