[英]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.