0

I want to be able to click a button and the value of one cell gets transferred to another cell, and the new cell adds them up.

example:

If I put 5 in B6 then click the button, C5 should update to 5 and B6 should be cleared. If I add 5 again to B6, C5 will add 5 to its existing number adding up to 10, then B6 will be cleared.

1

You can use this script and bind it to a button:

function transferCellValueOnButtonPress() {
    const ss = SpreadsheetApp;
    const selectedCell = ss.getCurrentCell();
    const selectedCellValue = selectedCell.getValue();

    // Target cell
    const targetCell = ss.getActiveSheet().getRange("C2");

    if (!(selectedCell.isBlank() || isNaN(selectedCellValue))) {    
      // If the cell is blank or is not a number change value to 0
      if (targetCell.isBlank() || isNaN(targetCell.getValue())) {
        targetCell.setValue(0);
      }
      targetCell.setValue(targetCell.getValue() + selectedCellValue);   
    }
}

I have made an example sheet which has a button that executes the script and some example values to showcase the script.

| improve this answer | |
  • thanks a ton this helped me alot – junkychap Dec 7 '19 at 11:41
  • Can you accept my answer if it has solved your problem? – Bas van der Linden Dec 7 '19 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.