文件分頁中相對於特定元素的參照位置。使用者游標會以 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 所含文字和格式的人工 Text 元素,無論是直接或透過一連串子項元素。ParagraphList |
get | Integer | 取得 get 傳回的 Text 元素中,這個 Position 的偏移。 |
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 前的子元素數
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getSurroundingText()
建立人工 Text 元素,代表包含 Position 的 Paragraph 或 ListItem 的文字和格式,無論是直接包含還是透過子項元素鏈結包含。如要判斷傳回的 Text 元素中 Position 的位移,請使用 getSurroundingTextOffset()。
回攻員
Text:相當於在包含 Position 的 Paragraph 或 ListItem 上呼叫 editAsText() 的結果,無論是直接或透過一連串子項元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getSurroundingTextOffset()
取得 getSurroundingText() 傳回的 Text 元素中,這個 Position 的偏移。位移是 Position 前的字元數 (也就是這個 Position 後的字元索引)。
回攻員
Integer:Position 在 Paragraph 或 ListItem 中 (直接或透過一連串子項元素) 之前的字元數Position
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
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
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents