Home > Back-end >  How to get the Last updated item in Google sheets
How to get the Last updated item in Google sheets

Time:02-18

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.

Giving you an example, If the data is edited in Sheet1 C2 that data should be added into Sheet2 in A2 and B2

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?

Sheet1 Screenshot

enter image description here

Sheet2 Screenshot

enter image description here

I tried a lot of script editing and tried to modify it... so far nothing worked for me. :(

View Google Sheet

CodePudding user response:

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". By this, the script is run by the simple trigger of 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".

Note:

  • This script is run by the simple trigger of OnEdit. So, when you directly run the script with the script editor, an error occurs. Please be careful this. Please edit "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:

  • Related