对文档标签页中相对于特定元素的位置的引用。用户的光标以 Position 表示,还有其他用途。脚本只能访问运行该脚本的用户的光标,并且只有当脚本绑定到文档时才能访问。
// Insert some text at the cursor position and make it bold. const cursor = DocumentApp.getActiveDocument().getCursor(); if (cursor) { // Attempt to insert text at the cursor position. If the insertion returns // null, the cursor's containing element doesn't allow insertions, so show the // user an error message. const element = cursor.insertText('ಠ‿ಠ'); if (element) { element.setBold(true); } else { DocumentApp.getUi().alert('Cannot insert text here.'); } } else { DocumentApp.getUi().alert('Cannot find a cursor.'); }
方法
| 方法 | 返回类型 | 简介 |
|---|---|---|
get | Element | 获取包含相应 Position 的元素。 |
get | Integer | 获取此 Position 在包含它的元素中的相对位置。 |
get | Text | 创建一个人工 Text 元素,用于表示包含 Position 的 Paragraph 或 List 的文本和格式,无论是直接包含还是通过一系列子元素包含。 |
get | Integer | 获取此 Position 在 get 返回的 Text 元素中的偏移量。 |
insert | Bookmark | 在此 Position 处创建并插入新的 Bookmark。 |
insert | Inline | 根据指定的图片 blob 在此 Position 处创建并插入新的 Inline。 |
insert | Text|null | 在此 Position 处插入指定文本。 |
详细文档
get Element()
获取包含相应 Position 的元素。这将是 Text 元素或 Paragraph 等容器元素。无论哪种情况,都可以使用 get 确定元素内的相对位置。
返回
get Offset()
获取此 Position 在包含它的元素中的相对位置。如果元素是 Text 元素,则偏移量是 Position 之前的字符数(即此 Position 之后的字符的索引);对于任何其他元素,偏移量是同一容器元素内此 Position 之前的子元素数(即 Position 之后的子元素的索引)。
返回
Integer - 对于 Text 元素,此 Position 之前的字符数;对于其他元素,同一容器元素内此 Position 之前的子元素数
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Surrounding Text()
创建一个人工 Text 元素,用于表示包含 Position 的 Paragraph 或 List 的文本和格式,无论是直接包含还是通过一系列子元素包含。如需确定返回的 Text 元素中 Position 的偏移量,请使用 get。
返回
Text - 相当于对包含 Position 的 Paragraph 或 List(直接或通过子元素链)调用 edit 所得到的结果的元素
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Surrounding Text Offset()
获取此 Position 在 get 返回的 Text 元素中的偏移量。偏移量是指 Position 之前的字符数(即此 Position 之后的字符的索引)。
返回
Integer - 包含相应 Position 的 Paragraph 或 List 中此 Position 之前的字符数,直接包含或通过子元素链包含
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Bookmark()
insert Inline Image(image)
根据指定的图片 blob 在此 Position 处创建并插入新的 Inline。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
image | Blob | 要在此 Position 处插入的图片数据 |
返回
Inline - 新的图片元素,或者如果此 Position 所在的元素不允许插入图片,则为 null
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Text(text)
在此 Position 处插入指定文本。此方法会创建一个新的 Text 元素,即使该字符串插入到现有 Text 元素中也是如此,以便轻松设置新元素的样式。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
text | String | 要在此 Position 处插入的字符串 |
返回
Text|null - 新文本元素,或者如果此 Position 所在的元素不允许插入文本,则为 null
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents