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

100
Vistas
Google Apps Script Function output can not be stored in a variable

I have an array that I want to flatten in order to iterate over it. I get the array and then flatten it with the following function:

var Array = ss.getRange("G2:G").getValues(); ArrayFlat = flatten(Array);

Flatten helperfunction

function flatten(array) {
  var flattenedArray = [];

  for (var i = 0; i < array.length; i++) {
    flattenedArray.push(array[i][0])
  }
  return flattenedArray;
}

I would expect the same output for the last two following logs, but I get different logs.

function test() {
  Logger.log(Array) // unflattened array
  Logger.log(flatten(Array)) // flattended array
  Logger.log(ArrayFlat) // []
}

Can someone point me in the right direction, i.e. where the error lies?

EDIT: I have a solution to my problem (flat()), but not an answer to my question as to why my logs output is so different?!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try:

ss.getRange("G2:G").getValues().flat();

Array.flat()

function flatten() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const array = ss.getDataRange().getValues();
  var fA = [];
  if(array.length) {
    array.forEach((r,i) => {
      if(array[i].length) {
        r.forEach((c,j) => {
          fA.push(c);
        })
      }
    })
  } else {
    fA.push(r[0])
  }
  Logger.log(JSON.stringify(fA));
}
about 4 years ago · Juan Pablo Isaza 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