我搜索了一种方法来删除谷歌应用程序脚本(绑定到表格的脚本)中的命名范围,但我找不到列出它们或删除它们的方法。
我正在努力避免与我在附加设置中创建的命名范围发生冲突。
或者我应该只容忍'脏'列表并依赖于使用我的设置功能重置命名范围?
答案 0 :(得分:1)
答案 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();
}