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

150
Vistas
How can I exclude null elements from data array?

I have been using Yuri's answer to a question, but found myself in a new situation and I haven't quite understood each step of this method here:

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var header = sheet.getDataRange().getValues()[0];

  var data = [
    ["ID","Variação","Nome Site","Obs"],
    [11602,185,"Camisa Teste","Teste da Observação"]
  ]

  var indexes = header.map(x => data[0].indexOf(x));
  data = data.map(x => indexes.map(i => x.slice()[0] = x[i]));

  sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
}

Here's what the result looks like:

let result = [
           ["ID","Variação","Nome Site","Obs", "", ""],
           [11602,185,"Camisa Teste","Teste da Observação", "", ""]
         ]

Now, this is returning data with nulls. How can I make it return only mapped array elements? So, instead of heaving an array length of 6, that'd be only 4. Expected Result:

let expectedResult = [
           ["ID","Variação","Nome Site","Obs"],
           [11602,185,"Camisa Teste","Teste da Observação"]
         ]

Thank you!

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

0

One possible reason why you are having null elements in your array is that the cells next to your header have " " or space character in it. Since you are using getDataRange() it will also include those " " characters in your array.

Example:

Here, I added space character to cells E1 and F1:

enter image description here

Here are the values of data after using map function:

enter image description here

If I remove the values of cells E1 and F1 the output of data is:

enter image description here

Null values can also occur if the values below the header overlap the number of columns in your header.

Example:

enter image description here

Output:

enter image description here

Solution:

Instead of using getDataRange(), explicitly define the range you are using for the header. You can use either getRange(row, column, numRows, numColumns) or getRange(a1Notation).

Reference:

  • getRange(a1Notation)
  • getRange(row, column, numRows, numColumns)
about 4 years ago · Juan Pablo Isaza Denunciar

0

One way is to filter the inner values:

let result = [
  ["ID", "Variação", "Nome Site", "Obs", "", ""],
  [11602, 185, "Camisa Teste", "Teste da Observação", "", ""]
]
result = result.map(x => x.filter(y => y))
console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Remove nulls

function test() {
Logger.log(JSON.stringify([1,2,3,null,4,null,5,''].filter(e => e)))
}

Execution log
7:48:37 PM  Notice  Execution started
7:48:37 PM  Info    [1,2,3,4,5]
7:48:38 PM  Notice  Execution completed
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