user714023
user714023

Reputation: 23

Get difference between current value of cell and its previous value in google sheets

Suppose I have a cell, C1. The value stored in C1 is current 300. The value of C1 then changes to 200. Another cell, D1 will then display the difference in value between the current value of C1 with its previous value. So, at the moment the value of C1 changes, the value of D1 will become 200 - 300.

How do I make this happen?

Upvotes: 1

Views: 432

Answers (1)

Wicket
Wicket

Reputation: 38210

Create a function to be triggered on edit. Then use the oldValue and value properties of the event object. Below is an example of very simple function triggered by an on edit simple trigger:

function onEdit(e){
  if(e.range.getA1Notation() === 'C1'){
     SpreadsheetApp.getRange('D1').setValue(e.oldValue - e.value);
  }
}

Note: I didn't tested it.

Upvotes: 1

Related Questions