Class Position

Position

特定の要素を基準とした、ドキュメント タブ内の場所への参照。ユーザーのカーソルは、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.');
}

メソッド

メソッド戻り値の型概要
getElement()Elementこの Position を含む要素を取得します。
getOffset()Integerこの Position の、それを含む要素内の相対位置を取得します。
getSurroundingText()TextPosition を直接または子要素のチェーンを介して含む Paragraph または ListItem のテキストと書式設定を表す人工的な Text 要素を作成します。
getSurroundingTextOffset()IntegergetSurroundingText() によって返された Text 要素内のこの Position のオフセットを取得します。
insertBookmark()Bookmarkこの Position に新しい Bookmark を作成して挿入します。
insertInlineImage(image)InlineImage|null指定された画像 blob から、この Position に新しい InlineImage を作成して挿入します。
insertText(text)Text|nullこの Position に指定されたテキストを挿入します。

詳細なドキュメント

getElement()

この Position を含む要素を取得します。これは、Text 要素または Paragraph などのコンテナ要素になります。どちらの場合も、要素内の相対位置は getOffset() で決定できます。

戻る

Element - この Position オブジェクトが配置されているコンテナまたは Text 要素


getOffset()

この Position の、それを含む要素内の相対位置を取得します。要素が Text 要素の場合、オフセットは Position の前の文字数(つまり、この Position の後の文字のインデックス)です。他の要素の場合、オフセットは同じコンテナ要素内のこの Position の前の子要素の数(つまり、Position の後の子要素のインデックス)です。

戻る

Integer - Text 要素の場合、この Position の前の文字数。他の要素の場合、同じコンテナ要素内のこの Position の前の子要素の数

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getSurroundingText()

Position を直接または子要素のチェーンを介して含む Paragraph または ListItem のテキストと書式設定を表す人工的な Text 要素を作成します。返された Text 要素内の Position のオフセットを特定するには、getSurroundingTextOffset() を使用します。

戻る

Text - Position を直接または子要素のチェーンを介して含む Paragraph または ListItemeditAsText() を呼び出した結果と同等の要素

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getSurroundingTextOffset()

getSurroundingText() によって返された Text 要素内のこの Position のオフセットを取得します。オフセットは Position の前の文字数です(つまり、この Position の後の文字のインデックスです)。

戻る

Integer - Position を直接または子要素のチェーンを介して含む Paragraph または ListItem 内のこの Position の前の文字数

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

insertBookmark()

この Position に新しい Bookmark を作成して挿入します。

戻る

Bookmark - 新しいブックマーク

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

insertInlineImage(image)

指定された画像 blob から、この Position に新しい InlineImage を作成して挿入します。

パラメータ

名前説明
imageBlobSourceこの Position に挿入する画像データ

戻る

InlineImage|null - 新しい画像要素。この Position が配置されている要素で画像の挿入が許可されていない場合は null

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

insertText(text)

この Position に指定したテキストを挿入します。このメソッドは、文字列が既存の Text 要素内に挿入された場合でも、新しい Text 要素を作成します。これにより、新しい要素のスタイル設定が容易になります。

パラメータ

名前説明
textStringこの Position に挿入する文字列

戻る

Text|null - 新しいテキスト要素。この Position が配置されている要素でテキストの挿入が許可されていない場合は null

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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