Class Range
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
范围
文档中的元素范围。用户的选择会以 Range
的形式表示,以及其他用途。脚本只能访问运行脚本的用户的选择,并且只有在脚本绑定到文档的情况下才能访问。
// Bold all selected text.
const selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
const elements = selection.getRangeElements();
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
// Only modify elements that can be edited as text; skip images and other
// non-text elements.
if (element.getElement().editAsText) {
const text = element.getElement().editAsText();
// Bold the selected part of the element, or the full element if it's
// completely selected.
if (element.isPartial()) {
text.setBold(
element.getStartOffset(),
element.getEndOffsetInclusive(),
true,
);
} else {
text.setBold(true);
}
}
}
}
详细文档
getRangeElements()
获取此 Range
中的所有元素,包括任何部分 Text
元素(例如,如果选择仅包含 Text
元素的一部分)。如需确定 Text
元素是否仅部分包含在范围内,请参阅 RangeElement.isPartial()
。
返回
RangeElement[]
- 元素数组,按文档中显示的顺序
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
已弃用的方法
getSelectedElements()
已弃用。已重命名为 getRangeElements()
。
获取用户在文档的打开实例中选择的所有元素,包括任何部分选中的 Text
元素。
返回
RangeElement[]
- 一个包含已选择或部分选择的元素的数组,按元素在文档中显示的顺序排列
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eA \u003ccode\u003eRange\u003c/code\u003e represents a range of elements in a Google Doc, often used to represent the user's selection.\u003c/p\u003e\n"],["\u003cp\u003eScripts can only access the selection of the user who is running them within the bound document.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egetRangeElements()\u003c/code\u003e method retrieves all elements within the range, including partially selected text elements.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egetSelectedElements()\u003c/code\u003e method is deprecated and has been replaced by \u003ccode\u003egetRangeElements()\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Class Range\n\nRange\n\nA range of elements in a document. The user's selection is represented as a `Range`, among\nother uses. Scripts can only access the selection of the user who is running the script, and only\nif the script is [bound](/apps-script/scripts_containers) to the document.\n\n```javascript\n// Bold all selected text.\nconst selection = DocumentApp.getActiveDocument().getSelection();\nif (selection) {\n const elements = selection.getRangeElements();\n for (let i = 0; i \u003c elements.length; i++) {\n const element = elements[i];\n\n // Only modify elements that can be edited as text; skip images and other\n // non-text elements.\n if (element.getElement().editAsText) {\n const text = element.getElement().editAsText();\n\n // Bold the selected part of the element, or the full element if it's\n // completely selected.\n if (element.isPartial()) {\n text.setBold(\n element.getStartOffset(),\n element.getEndOffsetInclusive(),\n true,\n );\n } else {\n text.setBold(true);\n }\n }\n }\n}\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------|-----------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [getRangeElements()](#getRangeElements()) | [RangeElement[]](/apps-script/reference/document/range-element) | Gets all elements in this `Range`, including any partial [Text](/apps-script/reference/document/text) elements (for example, in the case of a selection that includes only part of a `Text` element). |\n\n### Deprecated methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------------|-----------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [getSelectedElements()](#getSelectedElements()) | [RangeElement[]](/apps-script/reference/document/range-element) | Gets all elements that the user has selected in the open instance of the document, including any partially selected [Text](/apps-script/reference/document/text) elements. |\n\nDetailed documentation\n----------------------\n\n### `get``Range``Elements()`\n\nGets all elements in this `Range`, including any partial [Text](/apps-script/reference/document/text) elements (for example, in the case\nof a selection that includes only part of a `Text` element). To determine whether a\n`Text` element is only partially included in the range, see [RangeElement.isPartial()](/apps-script/reference/document/range-element#isPartial()).\n\n#### Return\n\n\n[RangeElement[]](/apps-script/reference/document/range-element) --- an array of elements, in the order they appear in the document\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\nDeprecated methods\n------------------\n\n### `get``Selected``Elements()`\n\n\n**Deprecated.** Renamed to [getRangeElements()](#getRangeElements()).\n\nGets all elements that the user has selected in the open instance of the document, including\nany partially selected [Text](/apps-script/reference/document/text)\nelements.\n\n#### Return\n\n\n[RangeElement[]](/apps-script/reference/document/range-element) --- an array of selected or partially selected elements, in the order they appear in the\ndocument\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`"]]