Home > Net >  Google sheets add value to itself in same cell
Google sheets add value to itself in same cell

Time:12-31

I cannot find this question anywhere online somehow. On google sheets, I'd like to add a number to itself as I am typing it in. That is if cell C1 has the value of 23, and I select C1 and type 13, the new value of C1 would be 36, the sum of the new and previous values.

Is there any way I could do this? I would've thought there was some "add" mode I could turn on to do this or some shortcut like typing in " 23" but nothing seems to work and I can't find any help online for a somewhat simple task.

Thanks in advance.

CodePudding user response:

Assuming your sheet is 'mySheet' and the cell is C1, Try

function onEdit(event){
  var sh = event.source.getActiveSheet();
  var cel = event.source.getActiveRange();
  if (sh.getName()=='mySheet' && cel.getA1Notation()=='C1'){
    cel.setValue(1*(event.value) 1*(event.oldValue))
  }
}

explanation

Note that the event structure is as follows

var event = {
    "authMode": {object},
    "range": {
      "columnStart": int,
      "rowStart": int,
      "rowEnd": int,
      "columnEnd": int
    },
    "source": {Spreadsheet_object},
    "oldValue": "string",
    "user": {
      "nickname": "string",
      "email": "string"
    },
    "value": "string"
  };

So I use oldValue and value to obtain the expected result.

CodePudding user response:

You could also use single incursive formulating

Go to your google sheet document

Go to file > Settings

File Settings

Then go to the Calculation tab and enter the following settings.

  • Recalculation - On Change
  • Iterative Calculation - On
  • Max number of iterations - 1
  • Threshold - 0.05 An image of the settings

Now you can reference cells to themselves, having a running total. Showing 23 in cell A2 and in B2 Showing 36 in cell A2 and in B2, 13

The Formula in the Running Total Cell =IF(B2 = -1,0,B2 A2)

If the VALUE is -1, reset to 0, if it isn't add its value to whatever value I am.

Be warned, if you leave a value in this cell and update the spreadsheet anywhere else, it will continue to add that same value in the running total. Because we set the recalculation setting to "On Change".

  • Related