简体   繁体   English

应用程序脚本和谷歌表格,更改命名范围的正确方法?

[英]apps script and google sheets, proper way to change range of named range?

My core problem is I need to reference a changing range with a time based trigger.我的核心问题是我需要使用基于时间的触发器来引用变化范围。 Since the function is executed from time I decided to use a named range but I'm running into issues with setRange().由于 function 从我决定使用命名范围开始执行,但我遇到了 setRange() 的问题。

Currently my code is similar to this:目前我的代码与此类似:

var newRange = sheet.getRange(startRow, startCol, lenRow, lenCol);
SpreadsheetApp.getActive().getNamedRanges()
   .filter((range) => range.getName() == 'named_of_range')[0].setRange(newRange);

This works sometimes but other times newRange won't equal 'named_of_range' which is the named range I'd like to change.这有时有效,但有时 newRange 不等于“named_of_range”,这是我想更改的命名范围。 The height of the named range will be sometimes 1 more or 1 less than newRange.命名范围的高度有时会比 newRange 多 1 或少 1。 newRange is the correct range but the named range isn't updating properly. newRange 是正确的范围,但命名范围没有正确更新。

Hopefully my explanation makes sense, if not I'd be happy to share more of my code.希望我的解释是有道理的,否则我很乐意分享更多我的代码。 Thanks for any help or suggestions!感谢您的任何帮助或建议!

This seems to work:这似乎有效:

function changeNamedRangeName() {
  const ss = SpreadsheetApp.getActive();
  let nr = ss.getNamedRanges();
  nr.forEach(r => {
    if(r.getName() == "Original") {
      r.setName("New Name");
    }
  })
}

Not 100% sure but think it was just an error in another part of my code.不是 100% 肯定,但认为这只是我代码另一部分的错误。 Sorry for asking before testing more of my own program and thank you both for helping me out.很抱歉在测试更多我自己的程序之前询问并感谢你们帮助我。

The issue was that I was double upping an array with already existing items in the sheet while also adding to that same array.问题是我正在使用工作表中已经存在的项目加倍数组,同时还添加到同一个数组。 The length was used to determine the length of the named range which was making the issue.长度用于确定产生问题的命名范围的长度。 I'm still not sure why the range would sometimes be okay and other times not but at least it's working (for now).我仍然不确定为什么该范围有时可以,而其他时候则不行,但至少它正在工作(目前)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Sheets Apps 脚本,从命名范围返回随机项目 - Google Sheets Apps Script, return random item from named range 未找到范围,Google 表格的 Google Apps 脚本 - Range Not Found, Google Apps Script for Google Sheets 谷歌表格脚本:获取表格命名范围已打开 - Google Sheets Script: Get Sheet a Named Range is On 从命名范围Google表格脚本中选择范围 - Selecting a range from within a named range google sheets script 使用 Google Apps 脚本在不使用范围的情况下在 Google 表格中创建 EmbeddedChart - Create EmbeddedChart in Google Sheets without using Range, with Google Apps Script 范围宽度不正确。 Google Apps脚本和Google表格 - Incorrect range width. Google Apps Script & Google Sheets Google Apps 脚本 - 更改一系列 google 表格单元格中的值 - Google Apps Script - Changing values in a range of google sheets cells Google Apps脚本/ Google Sheet错误? 引用命名范围的复制/重新设置数据验证不适用于重复的图纸 - Google Apps Script/Google Sheet Bug? Copying/Re-Setup Data Validation refering to named range does not work on duplicated Sheets 如何使用脚本在谷歌表格中创建定义的命名范围 - How to create a defined named range in google sheets using script Google 表格脚本:获取命名范围中的第一行号 - Google Sheets Script: Get First Row Number in Named Range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM