具有名稱和 ID 以供日後擷取的 Range
。名稱可以重複;同一份文件中的多個不同範圍可能會共用相同名稱,就像 HTML 中的類別一樣。相反地,文件內的 ID 是獨一無二的,例如 HTML 中的 ID。在文件中加入 NamedRange
後即無法修改,只能移除。
存取文件的任何指令碼都可存取 NamedRange
。為避免指令碼之間出現意外衝突,請考慮在範圍名稱前加上不重複的字串。
// Create a named range that includes every table in the document. var doc = DocumentApp.getActiveDocument(); var rangeBuilder = doc.newRange(); var tables = doc.getBody().getTables(); for (var i = 0; i < tables.length; i++) { rangeBuilder.addElement(tables[i]); } doc.addNamedRange('myUniquePrefix-tables', rangeBuilder.build());
方法
方法 | 傳回類型 | 簡短說明 |
---|---|---|
getId() | String | 取得此 NamedRange 的 ID。 |
getName() | String | 取得這個 NamedRange 的名稱。 |
getRange() | Range | 取得與這個 NamedRange 相關聯的元素範圍。 |
remove() | void | 從文件中移除這個 NamedRange 。 |
內容詳盡的說明文件
getId()
取得此 NamedRange
的 ID。文件內的 ID 不會重複。
回攻員
String
:範圍的 ID,文件中不會重複
getName()
取得這個 NamedRange
的名稱。名稱不一定是唯一的。
回攻員
String
:範圍名稱,不一定是唯一的
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getRange()
remove()
從文件中移除這個 NamedRange
。這個方法不會刪除範圍的內容,只會移除參照。在已移除的 NamedRange
上呼叫這個方法,並不會產生任何效果。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents