简体   繁体   中英

Automatically reverse an edit to a google sheet cell

I have seen before a google script that, in onEdit(e), automatically reversed whatever edit was made to a given sheet cell. I'd like some code for that!

Thanks for whatever help you can provide.

Explanation:

Your goal is to revert the current edit that was made. Namely, change the new value with the value that was before the edit was made.

  • You want to do that for a specific cell and sheet.

Solution:

In the following script choose the sheet name and the cell reference and after you edit that particular cell (A1 in Sheet1 in the example), the old value will be retrieved back to the cell.

function onEdit(e) {
  const sheetName = "Sheet1"; // choose the name of the Sheet
  const cell = "A1"; // choose the cell
  const rng = e.range;
  if(rng.getA1Notation() == cell && rng.getSheet().getName() == sheetName){
    rng.setValue(e.oldValue); 
  }
}

Illustration:

Every edit on cell A1 is reversed.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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