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().
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. The height of the named range will be sometimes 1 more or 1 less than newRange. newRange is the correct range but the named range isn't updating properly.
Hopefully my explanation makes sense, if not I'd be happy to share more of my code. Thanks for any help or suggestions!
CodePudding user response:
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");
}
})
}
CodePudding user response:
Not 100% sure but think it was just an error in another part of my code. 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).