Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
How to load Named Ranges into array?

I have defined a couple of named ranges in my sheet following the naming schema fieldName1, fieldName2, fieldName3 etc. They are located in different places in my sheet.

I am trying to load those fieldName(s) into an array now, but I encounter an issue, as getRange("fieldName1").getValue() is expecting a "String" of fieldName rather than a getRange(fieldName[c]).getValue() code.

Here is what I have so far:

  const noFields = wsS.getRange("noFields").getValue()
  var fieldName = []
    for (var c = 1; c <= noFields; ++c) {
    fieldName[c] = wsS.getRange(fieldName[c]).getValue()
  }

noFields is a variable of how many fieldNameX named ranges I have to load, as I didn't think of a better way to load them still.

Please help, thank you.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

So you want all the named ranges that start with "fieldName", and want to get all their values as an array.

You can get all the named ranges from the spreadsheet using .getActive(). However, they will not be sorted as expected.

If you simply want an array with all the values from named ranges, do:

var ss = SpreadsheetApp.getActive()
var values = ss.getNamedRanges()
    .map(namedRange => namedRange.getRange().getValue())

If you only want the values from those ranges following the naming schema, do:

const PREFIX = "fieldName" // case sensitive
var values = ss.getNamedRanges()
    .filter(namedRange => namedRange.getName().startsWith(PREFIX))
    .map(namedRange => namedRange.getRange().getValue())

You might also want to sort the named ranges by the number after "fieldName", just in case they were entered in different order, right? But that complicates things a little since the names are strings.

One way of sorting the array is by constructing an associative array with the range names:

const PREFIX = "fieldName" // case sensitive
var ss = SpreadsheetApp.getActive()
var namedRanges = ss.getNamedRanges()
    .filter(namedRange => namedRange.getName().startsWith(PREFIX))
var associativeArray = []
for (var namedRange of namedRanges) {
  associativeArray[namedRange.getName().substring(PREFIX.length)] =
      namedRange.getRange().getValue()
}
var values = associativeArray.flat()

Or with a custom comparator:

const PREFIX = "fieldName" // case sensitive
var ss = SpreadsheetApp.getActive()
var values = ss.getNamedRanges()
    .filter(namedRange => namedRange.getName().startsWith(PREFIX))
    .map(namedRange => [
        Number(namedRange.getName().substring(PREFIX.length)),
        namedRange.getRange().getValue() ])
    .sort((itemA,itemB) => itemA[0] - itemB[0])
    .map(item => item[1])

In any case, you will end up with an array of values that you can use.

about 4 years ago · Santiago Trujillo Denunciar

0

Get all named ranges in to an array

SpreadsheetApp.getActive().getNamedRanges();

Class namedRanges

function namedRangesInAnArray() {
  const ss = SpreadsheetApp.getActive();
  const names = ['r1','r2'.....];//array of names to get
  return SpreadsheetApp.getActive().getNamedRanges().filter(r => ~names.indexOf(r.getName()));
}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda