Class RangeElement
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Range 元素包含可能的起始和結束偏移的 Element
包裝函式。這些偏移量可讓 Text
元素中的字元範圍,在搜尋結果、文件選取範圍和命名範圍中顯示。
內容詳盡的說明文件
getElement()
取得與此 RangeElement
對應的 Element
。
const rangeElement = DocumentApp.getActiveDocument().getSelection().getRangeElements()[0];
// Log debug information about a range element.
Logger.log(`Element type: ${rangeElement.getElement().getType()}`);
if (rangeElement.isPartial()) {
Logger.log(`The character range begins at ${rangeElement.getStartOffset()}`);
Logger.log(
`The character range ends at ${rangeElement.getEndOffsetInclusive()}`,
);
} else {
Logger.log('The entire range element is included.');
}
回攻員
Element
:與此 RangeElement
對應的元素
授權
使用這個方法的腳本需要具備下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getEndOffsetInclusive()
取得範圍元素中部分範圍的結束位置。如果元素是 Text
元素,且 isPartial()
傳回 true
,則偏移量是範圍中最後一個字元之前的字元數量 (也就是範圍中最後一個字元的索引);在其他情況下,這個方法會傳回 -1
。
回攻員
Integer
:針對 Text
元素,範圍中最後一個字元之前的字元數量;針對其他元素,-1
授權
使用這個方法的腳本需要具備下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getStartOffset()
取得範圍元素內部分範圍的起始位置。如果元素是 Text
元素,且 isPartial()
傳回 true
,則偏移量為範圍起始之前的字元數量 (也就是範圍中第一個字元的索引);在其他情況下,這個方法會傳回 -1
。
回攻員
Integer
:如果是 Text
元素,則為範圍起始前的字元數;如果是其他元素,則為 -1
授權
使用這個方法的腳本需要具備下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
isPartial()
判斷此範圍元素是否涵蓋整個元素,或是元素字元部分選取範圍。部分範圍元素的範圍起點和結尾分別可使用 getStartOffset()
和 getEndOffsetInclusive()
判斷。
回攻員
Boolean
:如果元素部分納入,則為 true
;如果未納入,則為 false
授權
使用這個方法的腳本需要具備下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003eRangeElement\u003c/code\u003e is a wrapper around an \u003ccode\u003eElement\u003c/code\u003e that can represent a range of characters within a \u003ccode\u003eText\u003c/code\u003e element.\u003c/p\u003e\n"],["\u003cp\u003eIt is used in search results, document selections, and named ranges, allowing for partial or full element selection.\u003c/p\u003e\n"],["\u003cp\u003eYou can get the corresponding \u003ccode\u003eElement\u003c/code\u003e, determine if the range is partial, and find the start and end positions of partial ranges using its methods.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eRangeElement\u003c/code\u003e methods require authorization with specific scopes, like \u003ccode\u003ehttps://www.googleapis.com/auth/documents\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Class RangeElement\n\nRangeElement\n\nA wrapper around an [Element](/apps-script/reference/document/element) with a possible start and end offset. These offsets allow a\nrange of characters within a [Text](/apps-script/reference/document/text)\nelement to be represented in search results, document selections, and named ranges. \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------------|----------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|\n| [getElement()](#getElement()) | [Element](/apps-script/reference/document/element) | Gets the [Element](/apps-script/reference/document/element) that corresponds to this `Range``Element`. |\n| [getEndOffsetInclusive()](#getEndOffsetInclusive()) | `Integer` | Gets the position of the end of a partial range within the range element. |\n| [getStartOffset()](#getStartOffset()) | `Integer` | Gets the position of the start of a partial range within the range element. |\n| [isPartial()](#isPartial()) | `Boolean` | Determines whether this range element covers the entire element or a partial selection of the element's characters. |\n\nDetailed documentation\n----------------------\n\n### `get``Element()`\n\nGets the [Element](/apps-script/reference/document/element) that corresponds to this `Range``Element`.\n\n```javascript\nconst rangeElement = DocumentApp.getActiveDocument().getSelection().getRangeElements()[0];\n\n// Log debug information about a range element.\nLogger.log(`Element type: ${rangeElement.getElement().getType()}`);\nif (rangeElement.isPartial()) {\n Logger.log(`The character range begins at ${rangeElement.getStartOffset()}`);\n Logger.log(\n `The character range ends at ${rangeElement.getEndOffsetInclusive()}`,\n );\n} else {\n Logger.log('The entire range element is included.');\n}\n```\n\n#### Return\n\n\n[Element](/apps-script/reference/document/element) --- the element that corresponds to this `Range``Element`\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/documents.currentonly`\n- `https://www.googleapis.com/auth/documents`\n\n*** ** * ** ***\n\n### `get``End``Offset``Inclusive()`\n\nGets the position of the end of a partial range within the range element. If the element is a\n[Text](/apps-script/reference/document/text) element and [isPartial()](#isPartial()) returns `true`, the offset is the number of characters before the last\ncharacter in the range (that is, the index of the last character in the range); in any other\ncase, this method returns `-1`.\n\n#### Return\n\n\n`Integer` --- for [Text](/apps-script/reference/document/text) elements, the\nnumber of character before the last character in the range; for other elements, `-1`\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/documents.currentonly`\n- `https://www.googleapis.com/auth/documents`\n\n*** ** * ** ***\n\n### `get``Start``Offset()`\n\nGets the position of the start of a partial range within the range element. If the element is a\n[Text](/apps-script/reference/document/text) element and [isPartial()](#isPartial()) returns `true`, the offset is the number of characters before the start of\nthe range (that is, the index of the first character in the range); in any other case, this\nmethod returns `-1`.\n\n#### Return\n\n\n`Integer` --- for [Text](/apps-script/reference/document/text) elements, the\nnumber of characters before the start of the range; for other elements, `-1`\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/documents.currentonly`\n- `https://www.googleapis.com/auth/documents`\n\n*** ** * ** ***\n\n### `is``Partial()`\n\nDetermines whether this range element covers the entire element or a partial selection of the\nelement's characters. The beginning and end of a partial range element's range can be\ndetermined using [getStartOffset()](#getStartOffset()) and [getEndOffsetInclusive()](#getEndOffsetInclusive()), respectively.\n\n#### Return\n\n\n`Boolean` --- `true` if the element is partially included; `false` if not\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/documents.currentonly`\n- `https://www.googleapis.com/auth/documents`"]]