Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

35
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?!

7 months 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));
}
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos