Change Named Range with Script on multiple sheets

644 views
Skip to first unread message

John

unread,
May 11, 2022, 4:59:55 AM5/11/22
to Google Apps Script Community
Good day to all.

Can someone assist with an issue I am facing for changing named ranges on multiple google sheets.

Is the easiest way to remove the named range and and apply a new named range in the correct cell
or, to modify the existing named range i.e. change the cell reference?

The scrip is not directly associated to any sheet, but is linked to a master list of google URLS.

I am trying to adapt a sample script from here:

// The code below creates a new named range "foo", and then remove it. var ss = SpreadsheetApp.getActiveSpreadsheet(); ss.setNamedRange("foo", ss.getActiveRange()); ss.removeNamedRange("foo");

However, I get an error "Cannot function SetNamedRange in object sheet"

any help is gratefully received

Chanel Greco

unread,
May 11, 2022, 9:24:32 AM5/11/22
to Google Apps Script Community
Hey there

Here a couple of tips.

Make sure that the code from the sample is within a function that you write. Something like this: 
function nameRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setNamedRange("foo", ss.getActiveRange());
ss.removeNamedRange("foo");
}

The function name you define can't be the same as the Google Apps Script function setNamedRange.

Take into account that the sample script is creating a named range and then deleting it on the last row (ss.removeNamedRange("foo"). So if you only want to set a new named range make sure to use the setNamedRange("whatever the name you want to give it", ss.getActiveRange()).

Hope this shed some light into the dark.
Chanel
Reply all
Reply to author
Forward
0 new messages