特定の要素を基準とした、ドキュメント タブ内の場所への参照。ユーザーのカーソルは、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 | Position を直接または子要素のチェーンを介して含む Paragraph または List のテキストと書式設定を表す人工的な Text 要素を作成します。 |
get | Integer | get によって返された Text 要素内のこの Position のオフセットを取得します。 |
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 の前の子要素の数
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Surrounding Text()
Position を直接または子要素のチェーンを介して含む Paragraph または List のテキストと書式設定を表す人工的な Text 要素を作成します。返された Text 要素内の Position のオフセットを特定するには、get を使用します。
戻る
Text - Position を直接または子要素のチェーンを介して含む Paragraph または List で edit を呼び出した結果と同等の要素
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Surrounding Text Offset()
get によって返された Text 要素内のこの Position のオフセットを取得します。オフセットは Position の前の文字数です(つまり、この Position の後の文字のインデックスです)。
戻る
Integer - Position を直接または子要素のチェーンを介して含む Paragraph または List 内のこの Position の前の文字数
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
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
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
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
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents