[英]Getting the last non-empty cell in a column
I have a script that is adding or substracting a value (picked up in M2 cell) in each cell of a selected range (I mean a range that I can select with the mouse):我有一个脚本,它在选定范围的每个单元格中添加或减去一个值(在 M2 单元格中拾取)(我的意思是我可以用鼠标 select 的范围):
function moreLess() { var ss = SpreadsheetApp.getActive(); var sel = ss.getSelection().getActiveRangeList().getRanges(); for (var i=0; i<sel.length; i++) { var range = sel[i]; var values = range.getValues(); var number = SpreadsheetApp.getActive().getSheetByName("sommaire_redac").getRange('M2').getValue(); for (var j=0; j<values.length; j++) { for (var k=0; k<values[0].length; k++) { values[j][k] += number; } } range.setValues(values); } }
This works.这行得通。 But instead of selecting all the cells in which I want add or substract a value, I would like to select only one cell, and have a script that would select a range from this selected cell to the last non-empty cell of the column.但是,我不想选择要在其中添加或减去一个值的所有单元格,我只想 select 一个单元格,并且有一个脚本可以 select 从这个选定的单元格到该列的最后一个非空单元格的范围。
For example, instead of selecting cells T30:T36 like this…例如,不是像这样选择单元格 T30:T36……
with the code I have today用我今天的代码
… I would like to select only T30, like this… ...我想select只有T30,像这样...
with the code I'd like to have使用我想要的代码
… and then I would like the script to get the last non-empty cell of the column (T36) and select the range T30:T36. … 然后我希望脚本获取列的最后一个非空单元格 (T36) 和 select 范围 T30:T36。
I mean I would like to get exactly the same result by selecting only T30 cell, that I today obtain by selecting T30:T36.我的意思是我想通过仅选择 T30 单元格来获得完全相同的结果,这与我今天通过选择 T30:T36 获得的结果完全相同。
Thanks !谢谢 !
Using your own script, you could add a second line between var ss and var sel like this:使用您自己的脚本,您可以在 var ss 和 var sel 之间添加第二行,如下所示:
var ss = SpreadsheetApp.getActive();
ss.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate()
var sel = ss.getSelection().getActiveRangeList().getRanges();
It will select the remaining rows of that column;)它将 select 该列的剩余行;)
This script helps you to find the last empty row of a column.此脚本可帮助您找到列的最后一个空行。
function lastEmpty(col) { const sss = SpreadsheetApp.getActiveSpreadsheet(); const ss = sss.getActiveSheet(); const getlastEmptyRow = (colValues) => { return colValues.length - colValues.reverse().findIndex(row =>;.row[0]): } const colValues = ss.getRange(col+";"+col);getValues(); const lastEmptyRow = getlastEmptyRow(colValues). return lastEmptyRow; } console.log(`last empty row in column T is ${lastEmpty('T')}`)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.