Why Should I Code in Google Sheets?

Automate repetitive tasks, such as data entry, calculations, and formatting in Google Sheets.

Why Should I Code in Google Sheets?
Photo by Rubaitul Azad / Unsplash

You may want to code to automate repetitive tasks, such as data entry, calculations, and formatting in Google Sheets.

Or you want to get and send data to and from external APIs, so that you can have data updates automatically and integrate with other web services.

In this guide, we'll start coding in Google Sheets. I'll make it super easy for you!

We'll cover the basic coding concepts to get you started and get your sheets automated!

🤖
"Automation is cost reduction by elimination of repetitive processes."

- Akif M. K.

Why Code in Google Sheets?

You might wonder why coding is necessary when Google Sheets already has over 400 built-in formulas. However, if you've ever wished for a way to automate repetitive tasks or perform custom operations that formulas can't handle, coding is the answer. Coding in Google Sheets allows you to automate processes, create custom functions, and enhance your data management capabilities.

Getting Started with Coding in Google Sheets

Let's dive right in and get started with coding. Follow these steps to open the Apps Script editor and write your first custom function.

Step 1: Open a New Sheet

  1. Create a new sheet: Open your browser and go to sheet.new to create a brand new Google Sheet. This new sheet is empty and ready for you to start coding.

Step 2: Open the Apps Script Editor

  1. Go to Extensions: In your new sheet, click on the "Extensions" menu at the top.
  2. Select Apps Script: Click on "Apps Script" from the dropdown menu. This action will open a new window where you can write your code.

Step 3: Write Your First Function

When you open Apps Script for the first time, you'll see a default function template. Let's write a custom function to perform a basic math operation.

  1. Rename the function: Change the function name from myFunction to math.
  2. Add parameters: Modify the function to accept two parameters, number1 and number2.
  3. Write the code: Add code to perform an addition operation on the two numbers.
function math(number1, number2) {
  var newNumber = number1 + number2;
  return newNumber;
}
  1. Save the project: Press Command + S (or Ctrl + S on Windows) to save your project. The orange save button will disappear once the project is saved.

Step 4: Use Your Custom Function in Google Sheets

  1. Enter data in the sheet: In your Google Sheet, enter two numbers in cells C4 and C5, such as 55 and 66.
  2. Call your function: In another cell, use your custom function as follows:
The function will add the two numbers and display the result in the cell. For example, if the numbers are 55 and 66, the function will return 121.

Modifying the Function

You can easily modify the custom function to perform different operations. For example, to divide the two numbers:

  1. Change the operation: Modify the function to perform division instead of addition.
function math(number1, number2) {
  var newNumber = number1 / number2;
  return newNumber;
}
  1. Save the project: Press Command + S (or Ctrl + S on Windows) to save your changes.
  2. Use the updated function: The function will now divide the two numbers instead of adding them.

Understanding Functions and Formulas

In Google Sheets, we use the term "formula" for operations performed within the sheet using the equal sign (=), such as =SUM(A1:A10). In Apps Script, we refer to the code blocks as "functions." Understanding this distinction will help you navigate and communicate your work more effectively.

Structure of a Function

A function in Apps Script has the following structure:

function functionName(parameters) {
  // Code to execute
}

Function keyword: Defines the start of a function.
Function name: You can name your function anything that describes its purpose.
Parameters: Optional inputs that the function uses to perform operations.
Code block: The code inside curly brackets {} that performs the desired operations.

What Not To Do While Coding in Google Sheets

To avoid bad situations while coding in Google Sheets, it's essential to be mindful of common pitfalls and unusual mistakes that could lead to frustration or errors. Here are some straightforward and quirky things not to do:

Do not ignore error messages; they provide crucial information about what went wrong.

Avoid copying code from unreliable sources without understanding it, as this could introduce bugs.

Don't forget to save your work frequently to prevent data loss.

Refrain from using overly complex functions without proper documentation, as this can confuse you later.

Don't skip testing your functions with various inputs to ensure they work correctly.

On a weirder note, avoid coding while wearing socks on your hands; it might seem fun, but it will hinder your typing.

Also, don't try to debug your code while juggling; multitasking like that can lead to disastrous mistakes.

Can You Answer This Now?

What is the primary purpose of coding in Google Sheets as described in the article?

A) To create visually appealing spreadsheets

B) To automate repetitive tasks and perform custom operations

C) To increase the number of formulas available

D) To enhance the aesthetic layout of the sheet

Conclusion

Congratulations! You've just written your first custom function in Google Sheets and seen how it can be applied directly within your sheet to perform automated tasks. Coding in Google Sheets opens up a world of possibilities for automating tedious tasks, creating custom solutions, and enhancing your data management capabilities.

Stay tuned for part two, where we'll dive deeper into more advanced coding techniques and explore additional functionality in Google Sheets.

If you're looking for more Google Sheets tutorials, check out more tutorials at Better Sheets. Happy coding!