9.11. Creating and Using Basic Formulas: Creating Custom Formulas with Google Scripts

Mastering spreadsheets is an essential skill in the world of business and education. Google Sheets, being a powerful and affordable tool, offers a wide range of functionalities, including creating custom formulas through Google Scripts. In this chapter, we'll explore how you can expand your Google Sheets skills by using Google Apps Script to create formulas that meet your specific needs.

Introduction to Google Apps Script

Google Apps Script is a cloud-based scripting platform that enables you to automate tasks in Google apps and create custom functionality. With basic knowledge of JavaScript, you can write code that interacts with data in Google Sheets, creating formulas that go beyond the standard functions offered by the tool.

Getting Started with Scripts

Before we dive into creating custom formulas, it's important to understand how to access and use the Google Sheets script editor. To get started, open your spreadsheet, click "Extensions" in the menu bar and select "Apps Script". This will open the script editor where you can write and run your codes.

Understanding the Basics of JavaScript

JavaScript is the language used in Google Apps Script. If you are new to programming, it may be helpful to learn the basics of JavaScript, such as variables, data types, functions, and control flow structures. There are many free online resources that can help you become familiar with JavaScript.

Creating a Custom Function

A custom function in Google Sheets is created by writing a JavaScript function in the Google Script Editor. For example, to create a function that adds two numbers, you would write something like this:


function addNumbers(number1, number2) {
  return number1 + number2;
}

After saving and running the script, you can use the sumNumbers function directly in your spreadsheet like any other Sheets function.

Working with Ranges and Data

Custom functions can interact with cells and data ranges. To access data in a cell or range, you can use methods like getRange() and getValue() or getValues() for multiple cells . For example:


function calculateMedia(range) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = sheet.getRange(range);
  var values ​​= range.getValues();
  
  var sum = 0;
  var counter = 0;
  
  for (var i = 0; i < values.length; i++) {
    for (var j = 0; j < values[i].length; j++) {
      sum += values[i][j];
      counter++;
    }
  }
  
  return sum/counter;
}

This function would calculate the average of values ​​within a specified range.

Data Manipulation and Complex Calculations

With Google Apps Script, you can perform complex calculations and manipulate data in ways that standard formulas don't allow. For example, you can create a function that formats dates, calculates differences between values, or even makes calls to external APIs and processes the returned data.

Debugging and Error Handling

Errors are inevitable when writing code. Google Script Editor provides debugging tools that allow you to inspect your code, set breakpoints, and view the contents of variables. Additionally, it is important to write code that handles possible errors, such as invalid input or problems connecting to external APIs.

Publishing Your Custom Function

After you create and test your custom function, you can allow other users of your spreadsheet to use it. To do this, you must save and deploy your project as a Google Apps Script library, which generates a library identifier that others can use to add their custom functions to their own sheets.

Good Practices

  • Name your functions clearly and descriptively.
  • Write comments in your code to explain what each part does.
  • Test your functions with different sets of data to ensure they work as expected.
  • Use error handling to make your functions more robust and reliable.

Conclusion

With Google Apps Script, your possibilities for creating and using formulas in Google Sheets are practically limitless. By investing the time to learn and apply this powerful tool, you will be able to customize your spreadsheets to meet specific requirements, optimize workflows, and increase your productivity. Remember that,Just like any other skill, scripting proficiency comes with practice and patience.

Now answer the exercise about the content:

Which of the following statements about using Google Scripts in Google Sheets is true?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Use of common functions (SUM, AVERAGE, etc.)

Next page of the Free Ebook:

21Use of common functions (SUM, AVERAGE, etc.)

6 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text