简体   繁体   English

获取列中的最后一个非空单元格

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

相关问题 Google Sheets Apps Scripts如何在另一个指定列的最后一个非空单元格的同一行中填充特定文本 - Google Sheets Apps Scripts how to populate specific text in the same row of the last non-empty cell in another specified column 当相邻单元格在谷歌表格中为空时,如何列出非空单元格? - How to list non-empty cells when the adjacent cell is empty in google sheets? 为什么用非空对象调用Meteor.method时服务器会得到一个空对象? - Why is the server getting an empty object when calling Meteor.method with a non-empty object? Node.js:如何仅读取 txt 文件的最后一个非空行? - Node.js: How to read the last non-empty line only of a txt file? 用点而不是逗号完成每个“块”的每个非空最后“项目” - Finish every non-empty last "items" of every "blocks" with a point instead of a comma 如何用“字符串”替换Google表格列中非空单元格的值? - How to replace values of non-empty cells in a Google Sheets Column with a “String”? 任一字段的验证均为非空 - Validation for either of the fields are non-empty 有没有更好的方法可以进行非空验证? - Is there any better way for non-empty validation? 其他非空时需要 Vue 输入 - Vue input required if others are non-empty 如何在 typescript 中键入非空数组? - How to type a non-empty array in typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM