英文:
How to remove a NamedRange in Google Sheet that contains specific text using Apps Script
问题
我想删除所有包含文本 "campaigns" 的命名区域。
我尝试了以下代码,它执行了但没有起作用。
function removeNamedRanges() {
var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getNamedRanges().map(x => x.getName()).filter(y => y.includes('campaigns'));
namedRanges.forEach(range => range.remove());
}
英文:
I want to remove all Named Ranges that contain the text "campaigns".
I am trying the below, it's executing but not working
function removeNamedRanges(){
var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getNamedRanges().map(x => x.getName()).filter(y => y.includes('campaigns'));
namedRanges.forEach(range=>range.remove());
}
答案1
得分: 1
我明白你要求只翻译代码部分。以下是代码的中文翻译:
// 从代码中我理解,您正在创建一个包含所有命名范围名称的数组,可以使用这个名称来调用 `removeNamedRange(name)` 方法来删除它:
var ss = SpreadsheetApp.getActiveSpreadsheet();
function removeNamedRanges() {
var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getNamedRanges().map(x => x.getName()).filter(y => y.includes('campaigns'));
namedRanges.forEach(range => ss.removeNamedRange(range));
}
// 这只删除与该范围相关的名称,您可以使用以下修改来删除其内容:
function removeNamedRanges() {
var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getNamedRanges();
for (const namedRange of namedRanges) {
if (namedRange.getName().includes("campaigns")) {
ss.removeNamedRange(namedRange.getName());
var range = namedRange.getRange();
range.clear();
}
}
}
<details>
<summary>英文:</summary>
From the code I understand that you're creating an Array that contains the name of all the named ranges, you can use that name to delete it by calling the [```removeNamedRange(name)```][1] method:
var ss = SpreadsheetApp.getActiveSpreadsheet();
function removeNamedRanges() {
var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getNamedRanges().map(x => x.getName()).filter(y => y.includes('campaigns'));
namedRanges.forEach(range => ss.removeNamedRange(range));
}
This only deletes the name associated to that range, you can use this modification to delete its contents:
var ss = SpreadsheetApp.getActiveSpreadsheet();
function removeNamedRanges() {
var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getNamedRanges();
for (const namedRange of namedRanges) {
if (namedRange.getName().includes("campaigns")) {
ss.removeNamedRange(namedRange.getName());
var range = namedRange.getRange();
range.clear();
}
}
}
[1]: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#removenamedrangename
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论