在Google App脚本中删除数组中的特定空项目 [英] Remove specific null items in an array in Google App Script

查看:49
本文介绍了在Google App脚本中删除数组中的特定空项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义函数,可以将Google表格中不同工作表中的一个数组数据编译为一个数组.

I've created a custom function to compile in one array data from different sheets in Google Sheets.

这是怎么回事:

function COMPILER(){
  var i;
  var c;
  var display = [];
  var resultado = [];
  for (i = 0; i < arguments.length; i++) {
    display.push(arguments[i]);
  };
  for (c = 0; c < display.length; c++) {
    resultado = resultado.concat(display[c]);
  };
  return resultado;
};

代码运行得很好,但是我遇到了意外问题.由于数据源之间存在隐藏的公式,因此我的最终数组将使用几个空值进行编译.最终矩阵看起来像这样:

The code runs just fine, but I've ran into an unexpected issue. Since there are hidden formulas in between the data sources, my final array is compiled with several null values. The final matrix looks a little like this:

resultado [[3, f, 2, 6, 0, r], [2, 2, t, 5, 6, 8], **[, , , , , ]**, **[, , , , , ]**, **[, , , , , ]**, [4, y, y, 7, 8, 0], **[, , , , , ]**...]

我不知道如何正确地(有选择地)从矩阵内删除那些空值.我以为我必须阅读和测试整个矩阵,并删除每个选定的值,以及循环中的所有内容,但是我不能自己做.

I have no clue how to correctly (and selectively) remove those empty values from within the matrix. I would assume I'd have to read and test the whole matrix and remove each selected value, everything within a loop, but I couldn't do on my own.

推荐答案

此代码将帮助您解决问题,它接收主数组中的每个数组,然后检查是否有空值来放置它们并构建新的干净的数组.

This code will help you with your issue, it takes every array within the main array and then checks if there are null values to put them away and build a new clean array.

function testArray(){
  var arrTest = [[3, 'f', 2, 6, 0, 'r'], [2, 2, 't', 5, 6, 8],[, , , , , ],[, , , , , ], [, , , , , ], [4, 'y', 'y', 7, 8, 0], [, , , , , ]];
  // Initiate an empty array to fill it with the non empty data from your Array
  var cleanArr = [];
  // Check every array inside your main array
  arrTest.forEach(function(el){
    // If there are null values, this will clean them
    var filteredArr = el.filter(function(e){ return e != null;});
    // If the array ends up empty, don't push it into your clean array
    if(filteredArr.length) cleanArr.push(filteredArr);
  });
  Logger.log(cleanArr);
} 

文档

由于您正在使用数组处理大量操作,因此我建议您检查一下:

Docs

Due to the fact you are handling a lot of operations with arrays, I would recommend you to check this:

这篇关于在Google App脚本中删除数组中的特定空项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆