特定の要素を基準とした、ドキュメント タブ内の位置への参照。ユーザーのカーソルは、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 | get によって返されたこの Position の Text 要素内のオフセットを取得します。 |
insert | Bookmark | この Position に新しい Bookmark を作成して挿入します。 |
insert | Inline | 指定された画像
blob から、この Position に新しい Inline を作成して挿入します。 |
insert | Text|null | この Position に指定したテキストを挿入します。 |
詳細なドキュメント
getElement()
この Position を含む要素を取得します。これは、Text 要素または Paragraph などのコンテナ要素になります。どちらの場合でも、要素内の相対位置は getOffset() で確認できます。
戻る
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's のオフセットを確認するには、getSurroundingTextOffset() を使用します。
戻る
Text - Position を直接または子要素のチェーンを介して含む Paragraph または ListItem で editAsText()
を呼び出した結果と同等の要素
承認
このメソッドを使用するスクリプトには、次の 1 つ以上の スコープでの承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getSurroundingTextOffset()
この Position の Text 要素内のオフセットを getSurroundingText() によって取得します。オフセットは Position の前の文字数(つまり、この Position の後の文字のインデックス)です。
戻る
Integer - Position を直接または子要素のチェーンを介して含む Paragraph または ListItem 内のこの Position の前の文字数
承認
このメソッドを使用するスクリプトには、次の 1 つ以上の スコープでの承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insertBookmark()
insertInlineImage(image)
指定された画像 blob から、この Position に新しい InlineImage を作成して挿入します。
パラメータ
| 名前 | タイプ | 説明 |
|---|---|---|
image | Blob | この Position に挿入する画像データ |
戻る
InlineImage|null - 新しい画像要素。この Position が存在する要素で画像の挿入が許可されていない場合は null
承認
このメソッドを使用するスクリプトには、次の 1 つ以上の スコープでの承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insertText(text)
この Position に指定したテキストを挿入します。このメソッドは、文字列が既存の Text 要素内に挿入される場合でも、新しい Text
要素を作成します。これにより、新しい要素のスタイルを簡単に設定できます。
パラメータ
| 名前 | タイプ | 説明 |
|---|---|---|
text | String | この Position に挿入する文字列 |
戻る
Text|null - 新しいテキスト要素。この Position が存在する要素でテキストの挿入が許可されていない場合は null
承認
このメソッドを使用するスクリプトには、次の 1 つ以上の スコープでの承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents