确定单列中的最后一行 [英] Determining the last row in a single column
问题描述
我有一张从 A
到 H
列的数据表.
I have a sheet with data in cols A
through H
.
我需要确定 A
列中包含数据的最后一行(它们都是连续的 - 数据/行中没有间隙).
I need to determine the last row in column A
that contains data (it's all contiguous - no gaps in the data/rows).
其他列中也有数据比A
列更多行数据,所以我只需要隔离A
列.(和/或只是在 col A
内的范围).
There is also data in the other columns that have more rows of data than column A
, so I need to isolate only column A
. (And/or just a range within col A
).
我可以使用
=COUNTA(A2:A100)
然而,在我对 Google Apps Script 解决方案的所有研究中,我似乎发现的只是执行包含数十行代码的多种功能的要求 - 包括大量 i++
内容......其中我可以通过直接从 A1
偏移来做不那么复杂的事情.
However in all of my researching for a Google Apps Script solution, all I seem to find are requirements to perform multiple functions encompassing dozens of lines of code - including plenty of i++
stuff... Which I could do less complexly via offsetting directly from A1
.
是否有特定列的方法可以修改此方法?
Is there possibly a column-specific way of modifying this method?
var aLast = ss.getDataRange().getNumRows();
如果需要一个复杂的过程,那就这样吧.但我发现很难想象(甚至更难找到!)一个更简单的解决方案.
If a convoluted process is what is required, then so be it. But I find it difficult to imagine (and even more difficult to find!) a simpler solution.
有人关心启发我(或戳破我的泡泡)吗?
Does anyone care to enlighten me (or pop my bubble)?
推荐答案
如何使用 JavaScript 技巧?
How about using a JavaScript trick?
var Avals = ss.getRange("A1:A").getValues();
var Alast = Avals.filter(String).length;
我从这个答案中借用了这个想法.Array.filter()
方法对 Avals
数组进行操作,该数组包含 A 列中的所有单元格.通过对本机函数的构造函数进行过滤,我们只返回非空元素.
I borrowed this idea from this answer. The Array.filter()
method is operating on the Avals
array, which contains all the cells in column A. By filtering on a native function's constructor, we get back only non-null elements.
这仅适用于单列;如果范围包含多列,则 filter()
的结果将包括所有列的单元格,因此超出范围的填充维度.
This works for a single column only; if the range contains multiple columns,then the outcome of filter()
will include cells from all columns, and thus be outside the populated dimensions of the range.
这篇关于确定单列中的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!