如何使用Google Apps Script删除包含特定文本的Google表格中的命名区域。

huangapple go评论65阅读模式
英文:

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&#39;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>



huangapple
  • 本文由 发表于 2023年4月10日 21:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定