Skip to content

How to Add and Subtract Cell Values with Apps Script in Google Sheets

How to Add and Subtract Cell Values with Google Sheets

Google Sheets is a powerful tool for managing and analyzing data, but sometimes its built-in functions aren’t enough to meet your specific needs. That’s where Google Apps Script comes in. Apps Script is a JavaScript-based platform that allows you to automate tasks, create custom functions, and extend the functionality of Google Sheets. In this article, we’ll walk you through how to add and subtract cell values using Apps Script, making your spreadsheet tasks more efficient and dynamic.

Table of Contents

  1. Introduction to Google Apps Script
  2. Setting Up Your Google Sheets Environment
  3. Writing a Script to Add and Subtract Cell Values
  4. Best Practices for Using Apps Script
  5. Conclusion

1. Introduction to Google Apps Script

Google Apps Script is a cloud-based scripting language that integrates seamlessly with Google Workspace applications like Sheets, Docs, and Gmail. It’s particularly useful for automating repetitive tasks, creating custom formulas, and building workflows. With Apps Script, you can manipulate data in Google Sheets programmatically, saving time and reducing errors.


2. Setting Up Your Google Sheets Environment

Before diving into scripting, ensure your Google Sheets environment is ready. Open a new or existing Google Sheet and navigate to Extensions > Apps Script. This will open the Apps Script editor, where you can write and manage your scripts. Familiarize yourself with the interface, as it’s where you’ll spend most of your time coding.

How to Add and Subtract Cell Values with Apps Script in Google Sheets - 1
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 1

3. Writing a Script to Add and Subtract Cell Values

Let’s start by creating a script to add and subtract values. Suppose you want to add and subtract the values in cells A1 by adding a button and then using the apps script to execute it.

1. In Sheet1 Create two buttons and give them the text “Next” and “Back” by accessing the Insert > Drawing menu. You have to make the buttons one by one, but in this picture I give 2 pictures directly, but in reality it must be one by one because each button will be assigned with a different “function”.

How to Add and Subtract Cell Values with Apps Script in Google Sheets - 2
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 2
How to Add and Subtract Cell Values with Apps Script in Google Sheets - 3
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 3

2. Add a script with the following addition and subtraction functions to the Apps Script that you opened earlier. Note the highlighted function name, then don’t forget to save the script, with a name of your choice.

//+1
function incrementCellValue(){
let spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
let targetSheet = spreadsheet.getSheetByName('Sheet1')
let targetCell = targetSheet.getRange('A1')
let currValue = targetCell.getValue()
if(isNaN(currValue)){
  SpreadsheetApp.getUi().alert('Cell does not contain a number !');
  return
}
let newValue = currValue+1
targetCell.setValue(newValue)
}

//-1
function decrementCellValue(){
let spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
let targetSheet = spreadsheet.getSheetByName('Sheet1')
let targetCell = targetSheet.getRange('A1')
let currValue = targetCell.getValue()
if (currValue <= 0) {
SpreadsheetApp.getUi().alert('cannot be less than 0 !');
} else {
//
  if(isNaN(currValue)){
  SpreadsheetApp.getUi().alert('Cell does not contain a number !');
  return
}
let newValue = currValue-1
targetCell.setValue(newValue)
}
//
}
How to Add and Subtract Cell Values with Apps Script in Google Sheets - 4
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 4

3. Assign each button with a function name exactly the same as the one highlighted earlier. For how to assign a button, please click on the button, then a three-dot will appear on the corner of the button, click the three-dot then select “Assign script”. Enter the function name for each button, according to the script, for addition “incrementCellValue”, and for subtraction “decrementCellValue” then click “Ok“.

How to Add and Subtract Cell Values with Apps Script in Google Sheets - 5
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 5
How to Add and Subtract Cell Values with Apps Script in Google Sheets - 6
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 6

4. Select an empty cell area anywhere, and to run the script, please left click on the button, the first time running you may be asked to authorize, please allow the authorization.

How to Add and Subtract Cell Values with Apps Script in Google Sheets - 7
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 7
How to Add and Subtract Cell Values with Apps Script in Google Sheets - 8
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 8
How to Add and Subtract Cell Values with Apps Script in Google Sheets - 9
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 9
How to Add and Subtract Cell Values with Apps Script in Google Sheets - 10
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 10
How to Add and Subtract Cell Values with Apps Script in Google Sheets - 11
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 11
How to Add and Subtract Cell Values with Apps Script in Google Sheets - 12
How to Add and Subtract Cell Values with Apps Script in Google Sheets – 12

4. Best Practices for Using Apps Script

  • Keep Your Code Organized: Use comments and meaningful function names to make your scripts easy to understand and maintain.
  • Handle Errors Gracefully: Use try-catch blocks to handle potential errors, such as invalid cell references or data types.
  • Optimize Performance: Minimize calls to getRange and setValue by batching operations where possible.
  • Test Thoroughly: Always test your scripts on a sample dataset before deploying them in a live environment.

5. Conclusion

By using Apps Script you will be able to quickly handle something that usually has to be done manually, besides that you will spend less effort on it and can focus more on more critical things.

Maybe you like other interesting articles?

Leave a Reply

Your email address will not be published. Required fields are marked *