GAS - Google Docs API - getActiveDocument().getCursor().getOffset() 在行尾为光标返回意外的“1”

问题描述

我使用 CLASP,因此我的虚拟代码显示为 TypeScript。

如果我将 "test" 写入我的 google 文档并让光标位于我期望的字符串索引 4 处,Apps Script API 将返回 1 调用时:

const activeDocument = DocumentApp.getActiveDocument();
const cursor = activeDocument.getCursor();
const cursorOffset = cursor.getoffset();

这对我来说是一个真正的标题,因为当我将光标停留在字符串 "test" 的索引 1 处时,相同的代码返回预期的索引 >1

如何知道光标位于行尾或光标SurroundingText的{​​{1}}的实际索引1当偏移量相同时?


解决方案:

通过使用 Position 而不是 getSurroundingTextOffset() 解决了这个问题。

getoffset()

注意:这在使用 const activeDocument = DocumentApp.getActiveDocument(); const cursor = activeDocument.getCursor(); const cursorOffset = cursor.getSurroundingTextOffset(); 的情况下是有意义的。

在文档中阅读更多内容https://developers.google.com/apps-script/reference/document/position#getSurroundingTextOffset()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)