Google-sheets – subtract a cell value in another cell that already have one in Google Spreadsheets

formulasgoogle sheets

Let's say that I have two cells A1 (time needed) and B1 (time spent).

I want that when I enter a value in B1 the value of A1 will be reduced automatically.

I want that in some way I could enter in A1 the following formula: =A1-B1

Is it possible in Google Spreadsheets?

Best Answer

As Google Sheets does not natively support iteration with circular referencing, you will need to resort to Google Apps Script. Eg:

function onEdit(e)
{
  var editRange = e.range;
  var sheet = editRange.getSheet();
  if (sheet.getName() == "Sheet1" && editRange.getA1Notation() == "B1")
  {
    var updateRange = sheet.getRange("A1");
    var newValue = updateRange.getValue() - e.value;
    updateRange.setValue(newValue);
  }
}