Class NamedRange
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Range ที่มีชื่อ
Range
ที่มีชื่อและรหัสเพื่อให้ดึงข้อมูลในภายหลังได้ ชื่อไม่จำเป็นต้องไม่ซ้ำกัน หลายช่วงในแท็บเดียวกันอาจมีชื่อเดียวกันได้ คล้ายกับคลาสใน HTML ในทางตรงกันข้าม รหัสจะซ้ำกันไม่ได้ภายในแท็บ เช่น รหัสใน HTML เมื่อเพิ่ม NamedRange
ลงในแท็บแล้ว คุณจะแก้ไขไม่ได้ ทำได้เพียงนำออกเท่านั้น
NamedRange
สามารถเข้าถึงได้โดยสคริปต์ใดก็ตามที่เข้าถึงแท็บ ลองนำสตริงที่ไม่ซ้ำกันไว้หน้าชื่อช่วงเพื่อหลีกเลี่ยงความขัดแย้งที่ไม่ตั้งใจระหว่างสคริปต์
// Create a named range that includes every table in the active tab.
const documentTab =
DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
const rangeBuilder = documentTab.newRange();
const tables = documentTab.getBody().getTables();
for (let i = 0; i < tables.length; i++) {
rangeBuilder.addElement(tables[i]);
}
documentTab.addNamedRange('myUniquePrefix-tables', rangeBuilder.build());
เมธอด
วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
getId() | String | รับรหัสของ NamedRange นี้ |
getName() | String | รับชื่อของ NamedRange นี้ |
getRange() | Range | รับช่วงขององค์ประกอบที่เชื่อมโยงกับ NamedRange นี้ |
remove() | void | นำ NamedRange นี้ออกจากแท็บ |
เอกสารประกอบโดยละเอียด
getId()
รับรหัสของ NamedRange
นี้ รหัสนี้จะไม่ซ้ำกันภายในแท็บ
รีเทิร์น
String
— รหัสของช่วง ซึ่งไม่ซ้ำกันภายในแท็บ
getName()
รับชื่อของ NamedRange
นี้ ชื่อไม่จำเป็นต้องไม่ซ้ำกัน
รีเทิร์น
String
— ชื่อของช่วง ซึ่งไม่จำเป็นต้องไม่ซ้ำกัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getRange()
รับช่วงขององค์ประกอบที่เชื่อมโยงกับ NamedRange
นี้
รีเทิร์น
Range
— ช่วงขององค์ประกอบที่เชื่อมโยงกับชื่อและรหัส
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
remove()
นำ NamedRange
นี้ออกจากแท็บ วิธีนี้จะไม่ลบเนื้อหาของช่วง แต่จะนําการอ้างอิงออกเท่านั้น การเรียกใช้เมธอดนี้กับ NamedRange
ที่นําออกแล้วจะไม่มีผล
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eA \u003ccode\u003eNamedRange\u003c/code\u003e is a named area within a Google Doc tab, retrievable using its name or unique ID, similar to HTML elements with classes and IDs respectively.\u003c/p\u003e\n"],["\u003cp\u003eWhile multiple ranges can share the same name, IDs are unique within a tab, and once a \u003ccode\u003eNamedRange\u003c/code\u003e is added, it can only be removed, not modified.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eNamedRange\u003c/code\u003e provides methods like \u003ccode\u003egetId()\u003c/code\u003e, \u003ccode\u003egetName()\u003c/code\u003e, \u003ccode\u003egetRange()\u003c/code\u003e, and \u003ccode\u003eremove()\u003c/code\u003e for managing and accessing the named range and its associated elements.\u003c/p\u003e\n"],["\u003cp\u003eScripts can access \u003ccode\u003eNamedRange\u003c/code\u003e within a tab, and using unique prefixes for range names helps avoid conflicts between different scripts.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eNamedRange\u003c/code\u003e methods might require specific authorization scopes for accessing and manipulating document content.\u003c/p\u003e\n"]]],[],null,["# Class NamedRange\n\nNamedRange\n\nA [Range](/apps-script/reference/document/range) that has a name and ID to allow later retrieval. Names are not\nnecessarily unique; several different ranges in the same tab may share the same name, much like a\nclass in HTML. By contrast, IDs are unique within the tab, like an ID in HTML. Once a `Named``Range` has been added to a tab, it cannot be modified, only removed.\n\nA `Named``Range` can be accessed by any script that accesses the tab. To avoid unintended\nconflicts between scripts, consider prefixing range names with a unique string.\n\n```javascript\n// Create a named range that includes every table in the active tab.\nconst documentTab =\n DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();\nconst rangeBuilder = documentTab.newRange();\nconst tables = documentTab.getBody().getTables();\nfor (let i = 0; i \u003c tables.length; i++) {\n rangeBuilder.addElement(tables[i]);\n}\ndocumentTab.addNamedRange('myUniquePrefix-tables', rangeBuilder.build());\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|---------------------------|------------------------------------------------|-----------------------------------------------------------------|\n| [getId()](#getId()) | `String` | Gets the ID of this `Named``Range`. |\n| [getName()](#getName()) | `String` | Gets the name of this `Named``Range`. |\n| [getRange()](#getRange()) | [Range](/apps-script/reference/document/range) | Gets the range of elements associated with this `Named``Range`. |\n| [remove()](#remove()) | `void` | Removes this `Named``Range` from the tab. |\n\nDetailed documentation\n----------------------\n\n### `get``Id()`\n\nGets the ID of this `Named``Range`. The ID is unique within the tab.\n\n#### Return\n\n\n`String` --- The range's ID, which is unique within the tab.\n\n*** ** * ** ***\n\n### `get``Name()`\n\nGets the name of this `Named``Range`. The name is not necessarily unique.\n\n#### Return\n\n\n`String` --- The range's name, which is not necessarily unique.\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``Range()`\n\nGets the range of elements associated with this `Named``Range`.\n\n#### Return\n\n\n[Range](/apps-script/reference/document/range) --- The range of elements associated with the name and ID.\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### `remove()`\n\nRemoves this `Named``Range` from the tab. This method doesn't delete the contents of the\nrange; it merely the removes the reference. Calling this method on a `Named``Range` that\nhas already been removed has no effect.\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`"]]