选择列的最后一个值 [英] Selecting the last value of a column

查看:96
本文介绍了选择列的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列G中有一个包含一些值的电子表格。有些单元格之间是空的,我需要从该列中获取最后一个值到另一个单元格。



类似:

  = LAST(G2:G9999)
$ b $ p除了 LAST 不是一个函数。

解决方案

所以这个解决方案需要一个字符串作为参数。它会查找工作表中有多少行。它获取指定列中的所有值。它循环遍历从结尾到开头的值,直到找到一个不是空字符串的值。最后它重新调整价值。

脚本:

  function lastValue(column){
var lastRow = SpreadsheetApp.getActiveSheet()。getMaxRows();
var values = SpreadsheetApp.getActiveSheet()。getRange(column +1:+ column + lastRow).getValues(); (; values [lastRow - 1] ==&& lastRow> 0; lastRow--){}
return values [lastRow - 1];

;
}

用法:

  = lastValue(G)



编辑:



为了回应要求功能自动更新的评论:

我可以找到的最好的方法是在上面的代码中使用它:

  function onEdit(event){ 
SpreadsheetApp.getActiveSheet()。getRange(A1)。setValue(lastValue(G));
}

不再需要在单元格中使用函数,例如<强>使用部分状态。相反,你很难编码你想要更新的单元格和你想跟踪的列。有可能有一个更有说服力的方式来实现这个(希望那个不是硬编码的),但这是我现在能找到的最好的方式。



注意如果你像前面说过的那样使用单元格中的函数,它将在重载时更新。也许有一种方法可以钩入 onEdit(),并强制更新单元格函数。我在文档中找不到它。


I have a spreadsheet with some values in column G. Some cells are empty in between, and I need to get the last value from that column into another cell.

Something like:

=LAST(G2:G9999)

except that LAST isn't a function.

解决方案

So this solution takes a string as its parameter. It finds how many rows are in the sheet. It gets all the values in the column specified. It loops through the values from the end to the beginning until it finds a value that is not an empty string. Finally it retunrs the value.

Script:

function lastValue(column) {
  var lastRow = SpreadsheetApp.getActiveSheet().getMaxRows();
  var values = SpreadsheetApp.getActiveSheet().getRange(column + "1:" + column + lastRow).getValues();

  for (; values[lastRow - 1] == "" && lastRow > 0; lastRow--) {}
  return values[lastRow - 1];
}

Usage:

=lastValue("G")

EDIT:

In response to the comment asking for the function to update automatically:

The best way I could find is to use this with the code above:

function onEdit(event) {
  SpreadsheetApp.getActiveSheet().getRange("A1").setValue(lastValue("G"));
}

It would no longer be required to use the function in a cell like the Usage section states. Instead you are hard coding the cell you would like to update and the column you would like to track. It is possible that there is a more eloquent way to implement this (hopefully one that is not hard coded), but this is the best I could find for now.

Note that if you use the function in cell like stated earlier, it will update upon reload. Maybe there is a way to hook into onEdit() and force in cell functions to update. I just can't find it in the documentation.

这篇关于选择列的最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆