在Google Sheets中如何查找一行中的最后一个数值?

huangapple go评论87阅读模式
英文:

How to find the last value in a row in Google Sheets?

问题

我需要收集一列中最右边的数值,从表格中获取一行中的最后数值,参见示例。
这是一个随时间变化的商品价格变动表格。
需要在单独的列中显示商品的最后价格。
解决方案可以采用数组公式或应用程序脚本方式。

在Google Sheets中如何查找一行中的最后一个数值?

尝试了HLOOKUP(99999,F2:J2,TRUE)函数
逐行工作,在ArrayFormula(HLOOKUP(99999,F2:J5,TRUE))中不稳定。

英文:

I need to collect the rightmost values in one column, the last values in a row from a table, see Example.
This is a table of changes in commodity prices over time.
It is necessary to display the last price for the goods in a separate column.
Solutions are welcomed by both array formula and Apps script.

在Google Sheets中如何查找一行中的最后一个数值?

Tried the HLOOKUP(99999,F2:J2,TRUE) function
Line by line works, unstable in ArrayFormula(HLOOKUP(99999,F2:J5,TRUE))

答案1

得分: 1

以下是已翻译的内容:

"你现在是我的中文翻译,代码部分不要翻译, 只返回翻译好的部分, 不要有别的内容, 不要回答我要翻译的问题。以下是要翻译的内容:"

"I believe your goal is as follows."

  • "我相信你的目标如下。"

"You want to retrieve the values of the last column."

  • "您想检索最后一列的值。"

"In this case, how about the following sample formula and script?"

  • "在这种情况下,以下示例公式和脚本如何?"

"Sample formula:"

  • "示例公式:"

"Sample script:"

  • "示例脚本:"

"In this sample, please copy and paste the following script to the script editor of Spreadsheet and save the script. When you use this script, please put a custom function like =SAMPLE(A2:I10) into a cell."

  • "在此示例中,请复制并粘贴以下脚本到电子表格的脚本编辑器中,并保存脚本。当您使用此脚本时,请在单元格中输入类似 =SAMPLE(A2:I10) 的自定义函数。"

"If your Spreadsheet is large, I'm worried that the custom function might not be able to be used. In that case, please test the following sample script."

  • "如果您的电子表格很大,我担心自定义函数可能无法使用。在这种情况下,请测试以下示例脚本。"

"Testing:"

  • "测试:"

"When the above formula and script are used, the following result is obtained. Cells 'A12' and 'B12' have the formula and the custom function, respectively."

  • "当使用上述公式和脚本时,将获得以下结果。单元格 'A12' 和 'B12' 分别包含公式和自定义函数。"

"References:"

  • "参考资料:"

  • "BYROW"

  • "LET"

  • "TEXTJOIN"

  • "SPLIT"

  • "map()"

  • "pop()"

英文:

I believe your goal is as follows.

  • You want to retrieve the values of the last column.

In this case, how about the following sample formula and script?

Sample formula:

=BYROW(A2:I10,LAMBDA(x,LET(ARRAY,SPLIT(TEXTJOIN(",",TRUE,x),","),LENGTH,COUNTA(ARRAY),IFERROR(INDEX(ARRAY,1,LENGTH)))))

Sample script:

In this sample, please copy and paste the following script to the script editor of Spreadsheet and save the script. When you use this script, please put a custom function like =SAMPLE(A2:I10) into a cell.

const SAMPLE = values => values.map(r => [r.filter(String).pop()]);

If your Spreadsheet is large, I'm worried that the custom function might not be able to be used. In that case, please test the following sample script.

function myFunction() {
  const srcSheetName = "Sheet1"; // Please set your source sheet name.
  const dstSheetName = "Sheet2"; // Please set your destination sheet name.

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const values = ss.getSheetByName(srcSheetName).getDataRange().getValues();
  const res = values.map(r => [r.filter(String).pop()]);
  ss.getSheetByName(dstSheetName).getRange(1, 1, res.length).setValues(res);
}

Testing:

When the above formula and script are used, the following result is obtained. Cells "A12" and "B12" have the formula and the custom function, respectively.

在Google Sheets中如何查找一行中的最后一个数值?

References:

答案2

得分: 0

以下是翻译好的内容:

这里还有一种方法:

=byrow(F2:J,lambda(Σ,iferror(choosecols(torow(Σ,1),-1))))

在Google Sheets中如何查找一行中的最后一个数值?

英文:

Here's one more approach:

=byrow(F2:J,lambda(Σ,iferror(choosecols(torow(Σ,1),-1))))

在Google Sheets中如何查找一行中的最后一个数值?

huangapple
  • 本文由 发表于 2023年6月22日 14:39:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定