Create a Custom Function in Google Sheets
Tags: googleappsscript, googlesheets, javascript
Published: 2021-01-04 22:19:00

Google Sheets includes hundreds of built-in [functions](https://support.google.com/docs/table/25273) found in most desktop spreadsheet software. These functions can be used to create formulas that manipulate data and calculate strings and numbers. When these built-in functions aren’t enough for your needs, you can use Google Apps Script to create your own custom functions!

## Custom Function Example

![Custom Function Example](https://dev-to-uploads.s3.amazonaws.com/i/ywg76gxdye38pd2ief3s.png)

Let’s say you have a column of email addresses and you’d like to check if they look valid. Let’s create a custom function that will output TRUE if the email address looks valid, FALSE if the email address looks invalid, or display an error message if an email address was not provided.

By the way, if you’re not comfortable with writing Javascript or don’t have the time to learn, check out the [add-on store for Google Sheets](https://developers.google.com/apps-script/guides/sheets/functions#getting_a_custom_function_from_the_add-on_store). Maybe someone has already written the custom function you’re looking for!

## Creating a Custom Function

![Create a Custom Function](https://dev-to-uploads.s3.amazonaws.com/i/0j3pwnp6f5j9sca91w8j.png)

1. Create or open an existing spreadsheet in Google Sheets.
2. Navigate to Tools > Script Editor. If you are presented with a welcome screen, select Blank Project to start a new project.
3. The script editor will include a starting function. Remove all the code and replace it with the code below.
4. Navigate to File > Save. Give the script project a name and select Ok.

```javascript
function VALIDEMAIL(email) {
  if (!email) {
    throw 'Please provide an email address.';
  }
   
  return /\S+@\S+\.\S+/.test(email);
}
```

## Reviewing the Code

The **VALIDEMAIL** function gets passed an email value. If no email value was passed in, we throw an error which gets displayed in the cell.

If an email value was passed in correctly, we test the value against a regular expression. The result of the test gets displayed in the cell.

## Using a Custom Function

![Custom Function Example](https://dev-to-uploads.s3.amazonaws.com/i/ywg76gxdye38pd2ief3s.png)

1. Select the cell where you’d like to use the custom function.
2. Type an equals sign (=) followed by the custom function name and the cell that contains an email address to validate and press Enter.
3. The cell will display Loading… and then return the result.

## Custom Function Help

![Custom Function Help](https://dev-to-uploads.s3.amazonaws.com/i/bojb6ikhluicgk3xa56p.png)

Whenever you use a function, Google Sheets displays helpful information about the function. When using your own custom function, you can also display helpful information by simply including a [JsDoc](https://jsdoc.app/about-getting-started.html) @customfunction tag like the following:

```javascript
/**
 * Check if an email address looks valid.
 *
 * @param {string} email The email address to check.
 * @return TRUE or FALSE.
 * @customfunction
 */
function VALIDEMAIL(email) {
  if (!email) {
    throw 'Please provide an email address.';
  }
   
  return /\S+@\S+\.\S+/.test(email);
}
```

---
#### Visit our website at [https://nightwolf.dev](https://nightwolf.dev) and follow us on [Facebook](https://www.facebook.com/NightWolfDevelopment) and [Twitter](https://twitter.com/nightwolfdev)!
---