I would like a Function in the Script attached to the Spreadsheet, to iterate through all the Named Ranges, and enter them in cells in a "Data" sheet. This sheet has lots of lists etc, which are used by my main sheet ("Analysis").
Ideally, this would put them starting at Row 1001, with col A being the Name, and col D being the Range. (Cols B & C left blank to allow for Overflow from col A - don't want to resize col A)
To be effective, any existing data in the rows previously entered by the Function should first be deleted, before updating with the new data.
Although I'm getting the hang of the Script language, I'm struggling with this, so if anyone could provide an example function, it would be appreciated! MTIA.
[edit] Apologies if my original question was inadequate. I had some code, but it was rubbish, I knew it, so I scrapped it. Almost there now (I think) but one more prob to sort, getting an exception:
function namedRangeToSpreadsheet() {
// Author: Cooper
// Link: https://stackoverflow.com/a/64841828/190925
// Purpose: Record all NamedRanges by both Name and Range, as a check that
// public copy is NOT missing any!
const ss=SpreadsheetApp.getActive();
const nrA=ss.getNamedRanges();
var startRow = 1001; // MH insert from this row
var endRow = 1200; // MH clear|insert to a max. of this
// var targetSh = "Data"; // MH revert to Data ss when tests OK
var targetSh = "Named Ranges";
// MH clear previous data
let sh=ss.getSheetByName(targetSh);
var range = sh.getRange(targetSh + "!A" + startRow + ":D" + endRow);
range.clear();
//sh.getRange(sh.getLastRow()+1,1,rowA.length,4).clear();
let rowA=[];
for(let i=0;i<nrA.length;i++) {
let name=nrA[i].getName();
let range=nrA[i].getRange().getA1Notation();
rowA.push([name,'','',range]);
}
rowA.sort(); // MH
/* Exception: The number of rows in the data does not match the number of rows in the range.
The data has 32 but the range has 200. (line 348, file "Code") */
//sh.getRange(sh.getLastRow()+1,1,rowA.length,4).setValues(rowA);
range.setValues(rowA);
}
function namedRangeToSpreadsheet() {
const ss=SpreadsheetApp.getActive();
const nrA=ss.getNamedRanges();
let rowA=[];
for(let i=0;i<nrA.length;i++) {
let name=nrA[i].getName();
let range=nrA[i].getRange().getA1Notation();
rowA.push([name,'','',range]);
}
let sh=ss.getSheetByName('Sheet1');
sh.getRange(sh.getLastRow()+1,1,rowA.length,4).setValues(rowA);
}