Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!