简体   繁体   English

如何在谷歌表格中获取最后更新的项目

[英]How to get the Last updated item in Google sheets

I'm trying to find a solution online but it's not been fruitful so far, I want to fetch the data in Columns 1,2,3,4,5,6,7 that is last edited in Sheet1 and then feed it into Sheet2.我试图在网上找到一个解决方案,但到目前为止还没有成果,我想获取在 Sheet1 中最后编辑的第 1、2、3、4、5、6、7 列中的数据,然后将其提供给 Sheet2 .

Giving you an example, If the data is edited in Sheet1 C2 that data should be added into Sheet2 in A2 and B2举个例子,如果在Sheet1 C2中编辑数据,则应将数据添加到A2B2中的Sheet2

What I'm trying to achieve is, if anyone updated Sheet one, I want to know the last update in Sheet 2, is it possible to do this via scrip?我想要实现的是,如果有人更新了工作表 1,我想知道工作表 2 中的最后更新,是否可以通过凭据执行此操作?

Sheet1 Screenshot Sheet1 截图

在此处输入图像描述

Sheet2 Screenshot Sheet2 截图

在此处输入图像描述

I tried a lot of script editing and tried to modify it... so far nothing worked for me.我尝试了很多脚本编辑并尝试对其进行修改……到目前为止,对我来说没有任何效果。 :( :(

View Google Sheet 查看谷歌表格

In your situation, how about the following sample Script?在您的情况下,以下示例脚本如何?

Sample script:示例脚本:

Please copy and paste the following script to the script editor of Spreadsheet and save the script.请将以下脚本复制并粘贴到电子表格的脚本编辑器中并保存脚本。 When you use this script, please edit "Sheet1".当您使用这个脚本时,请编辑“Sheet1”。 By this, the script is run by the simple trigger of OnEdit.通过这种方式,脚本由 OnEdit 的简单触发器运行。

function onEdit(e) {
  const range = e.range;
  const sheet = range.getSheet();
  if (sheet.getSheetName() != "Sheet1" || range.rowStart == 1) return;
  const col = range.columnStart - 1;
  const [header, ...values] = sheet.getDataRange().getValues();
  const value = values[range.rowStart - 2];
  const v = [[`${value[0]} - ${value[1]}`, `${header[col]} - ${value[col]}`]];
  e.source.getSheetByName("Sheet2").getRange("A2:B2").setValues(v);
}
  • For example, when the cell "C2" of "Sheet1" is edited, AB0002 - Orange and Price - ### are put to the cells "A2:B2" of "Sheet2".例如,当编辑“Sheet1”的单元格“C2”时, AB0002 - OrangePrice - ###被放入“Sheet2”的单元格“A2:B2”。

Note:笔记:

  • This script is run by the simple trigger of OnEdit.此脚本由 OnEdit 的简单触发器运行。 So, when you directly run the script with the script editor, an error occurs.因此,当您直接使用脚本编辑器运行脚本时,会出现错误。 Please be careful this.请注意这一点。 Please edit "Sheet1".请编辑“Sheet1”。 By this, the script is run.至此,脚本运行。

  • This sample script is for your sample Spreadsheet.此示例脚本适用于您的示例电子表格。 When you changed the structure of the Spreadsheet and the actual Spreadsheet is different from your sample one, this script might not be able to be used.当您更改电子表格的结构并且实际电子表格与您的示例不同时,此脚本可能无法使用。 Please be careful about this.请注意这一点。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM