将单元格的范围存储到数组中 [英] Store range of cells to array

查看:118
本文介绍了将单元格的范围存储到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有单元格A1:A150中的数据列表(但数量可能会有所不同),有没有办法将它推入数组中,而无需单独查看每个单元格以确定它是否为空?我这样做超出了我的执行时间,而且我需要更快的方式来存储数据和数据。当它碰到空单元格时停下来。



以下是我目前的操作方法。

  for(var i = 1; i <500; i ++){
if (datasheet.getRange(A+ i).getValue()==){
break;
}

else {
addedlist_old.push(datasheet.getRange(A+ i).getValue())
}



  // my2DArrayFromRng = sh.getRange(A2:A10)。getValues(); 
var my2DArrayFromRng = [[A2],[A3],[A4],[A5],[],[],[A8],[A9], []];
var a = my2DArrayFromRng.join()。split(',')。filter(Boolean);

方法 .join() .split(' ,')一起将2D数组转换为普通数组([A2,A3,A4,A5,,,A8,A9,])。
然后 .filter(布尔)方法去除空元素。上面的代码返回[A2,A3,A4,A5,A8,A9]。


If I have a list of data in cells A1:A150 (but the amount can vary), is there a way to push that into an array without looking at each cell individually to determine if it is empty? I exceed my execution time by doing this and I need a faster way to store the data & stop when it hits an empty cell.

Below is how I currently do it. I am new to this.

for (var i = 1; i < 500; i++) {
  if(datasheet.getRange("A" + i).getValue() == ""){
   break;   
  }

  else{
     addedlist_old.push(datasheet.getRange("A" + i).getValue())
    }

解决方案

If you're using only one column, I'd suggest:

  // my2DArrayFromRng = sh.getRange("A2:A10").getValues();
  var my2DArrayFromRng = [["A2"],["A3"],["A4"],["A5"],[],[],["A8"],["A9"],[]];
  var a = my2DArrayFromRng.join().split(',').filter(Boolean);

The methods .join() and .split(',') together convert the 2D array to a plain array (["A2","A3","A4","A5",,,"A8","A9",]). Then the method .filter(Boolean) strips the empty elements. The code above returns [A2, A3, A4, A5, A8, A9].

这篇关于将单元格的范围存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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