如何将一系列单元格存储到阵列中? [英] How can I store a range of cells to an array?

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

问题描述

如果我在A1:A150单元格中有一个数据列表(但数量可能有所不同),是否有一种方法可以将其推入数组,而无需单独查看每个单元格是否为空?通过这样做,我超过了执行时间,并且我需要一种更快的方法来存储数据并在数据到达空单元格时停止.

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 and stop when it hits an empty cell.

以下是我目前的操作方式:

Below is how I currently do it:

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);

方法 .join() .split(',')一起将2D数组转换为纯数组(["A2","A3", "A4","A5","A8","A9",]). 然后,方法 .filter(Boolean)剥离空元素.上面的代码返回[A2,A3,A4,A5,A8,A9].

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天全站免登陆