Home > Software design >  How can the input of one cell be used to add or subtract from another cell in Google Sheets?
How can the input of one cell be used to add or subtract from another cell in Google Sheets?

Time:10-31

I'd like to build a simple Google sheets that helps me keep track of my D&D campaign's resources. Specifically, how can I have blank cell (say like one with the placeholder tag "update value") that adds the input number to a specific cell (say if I input " 34" or "-21")? And I'd like the input cell to to back to being blank afterwards.

enter image description here

CodePudding user response:

There is no built-in feature that does that but you could use a Google Apps Script trigger.

You might have to start by reading https://developers.google.com/apps-script/guides/sheets then https://developers.google.com/apps-script/guides/triggers.

The following is a simple example of using a simple on edit trigger to update the value of the cell on the left of the edited cell.

function onEdit(e){
  const placeholder = '<update value>';
  if( e.oldValue === placeholder ){
     /** Modify the cell to the left */
     let target = e.range.offset(0,-1);
     target.setValue(target.getValue()   e.value);
     e.range.setValue(placeholder);
  } else {
   // do nothing
  }
} 
  • Related