删除谷歌应用脚​​本中的命名范围

时间:2014-08-27 05:42:54

标签: google-apps-script

我搜索了一种方法来删除谷歌应用程序脚本(绑定到表格的脚本)中的命名范围,但我找不到列出它们或删除它们的方法。

我正在努力避免与我在附加设置中创建的命名范围发生冲突。

或者我应该只容忍'脏'列表并依赖于使用我的设置功能重置命名范围?

2 个答案:

答案 0 :(得分:1)

ss.removeNamedRange(name);

Docs

无法更改它们 - issue 1721

答案 1 :(得分:0)

要列出命名范围:

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getnamedranges

const namedRanges: GoogleAppsScript.Spreadsheet.NamedRange[] = SpreadsheetApp.getActiveSpreadsheet().getNamedRanges();
Logger.log("List of named ranges in SpreadSheet:");
for (const namedRange of namedRanges) {
    Logger.log(namedRange.getName());
}

删除/移除命名范围:

https://developers.google.com/apps-script/reference/spreadsheet/named-range#remove

const namedRanges: GoogleAppsScript.Spreadsheet.NamedRange[] = SpreadsheetApp.getActiveSpreadsheet().getNamedRanges();
for (const namedRange of namedRanges) {
    Logger.log(`Removing named range: ${namedRange.getName()}`);
    namedRange.remove();

    // OR
    // SpreadsheetApp.getActiveSpreadsheet().removeNamedRange(namedRange.getName());
}

注意

“删除”命名范围不会删除该范围的内容。它只是删除与范围关联的名称。

也删除内容:

https://developers.google.com/apps-script/reference/spreadsheet/range#clear

const namedRanges: GoogleAppsScript.Spreadsheet.NamedRange[] = SpreadsheetApp.getActiveSpreadsheet().getNamedRanges();
for (const namedRange of namedRanges) {
    SpreadsheetApp.getActiveSpreadsheet().removeNamedRange(namedRange.getName());
    const range = namedRange.getRange();
    // clear the range content
    range.clear();
}