How can I store a range of cells to an array?

后端 未结 4 1374
孤独总比滥情好
孤独总比滥情好 2021-01-06 16:55

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 i

4条回答
  •  Happy的楠姐
    2021-01-06 17:22

    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].

提交回复
热议问题