Class Position

位置

对文档中某个位置的引用,相对于特定元素。用户游标表示为 Position,有其他用途。只有在脚本已绑定到文档的情况下,脚本才能访问运行脚本的用户的游标。

// Insert some text at the cursor position and make it bold.
var 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.
  var element = cursor.insertText('ಠ‿ಠ');
  if (element) {
    element.setBold(true);
  } else {
    DocumentApp.getUi().alert('Cannot insert text here.');
  }
} else {
  DocumentApp.getUi().alert('Cannot find a cursor.');
}

方法

方法返回类型简介
getElement()Element获取包含此 Position 的元素。
getOffset()Integer获取此 Position 在包含它的元素中的相对位置。
getSurroundingText()Text创建一个人工 Text 元素,该元素直接或通过子元素链表示包含 PositionParagraphListItem 的文本和格式。
getSurroundingTextOffset()Integer获取 getSurroundingText() 返回的 Text 元素中此 Position 的偏移量。
insertBookmark()Bookmark在此 Position 创建并插入新的 Bookmark
insertInlineImage(image)InlineImage从指定的图像 blob 在此 Position 创建并插入新的 InlineImage
insertText(text)Text在此 Position 处插入指定文本。

详细文档

getElement()

获取包含此 Position 的元素。这将是一个 Text 元素或一个容器元素(如 Paragraph)。无论是哪种情况,元素中的相对位置都可以通过 getOffset() 确定。

弃踢回攻

Element - 此 Position 对象所在的容器或 Text 元素


getOffset()

获取此 Position 在包含它的元素中的相对位置。如果该元素是 Text 元素,则偏移量为 Position 前面的字符数(即此 Position 后面的字符的索引);对于任何其他元素,偏移量为同一容器元素内此 Position 前面的子元素的数量(即 Position 之后的子元素的索引)。

弃踢回攻

Integer - 对于 Text 元素,表示此 Position 前面的字符数;对于其他元素,表示同一容器元素内此 Position 之前的子元素的数量

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getSurroundingText()

创建一个人工 Text 元素,该元素直接或通过子元素链表示包含 PositionParagraphListItem 的文本和格式。如需确定 Position 在返回的 Text 元素中的偏移量,请使用 getSurroundingTextOffset()

弃踢回攻

Text - 一个元素,相当于对包含 PositionParagraphListItem 调用 editAsText()(直接或通过子元素链)的结果

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getSurroundingTextOffset()

获取 getSurroundingText() 返回的 Text 元素中此 Position 的偏移量。偏移量是 Position 之前的字符数(即此 Position 后面的字符的索引)。

弃踢回攻

Integer - 包含 PositionParagraphListItem 中此 Position 前面的字符数(直接或通过子元素链)

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertBookmark()

在此 Position 创建并插入新的 Bookmark

弃踢回攻

Bookmark - 新书签

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertInlineImage(image)

从指定的图像 blob 在此 Position 创建并插入新的 InlineImage

参数

名称类型说明
imageBlobSource要在此 Position 中插入的图片数据

弃踢回攻

InlineImage - 新的图片元素,或 null(如果此 Position 所在的元素不允许插入图片)

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertText(text)

在此 Position 处插入指定文本。即使将字符串插入现有 Text 元素中,此方法也会创建新的 Text 元素,以便轻松设置新元素的样式。

参数

名称类型说明
textString要在此 Position 插入的字符串

弃踢回攻

Text - 新的文本元素,或 null(如果此 Position 所在的元素不允许插入文本)

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents