Clear a Range in Google Sheets using Apps Script

In this tutorial I will show you how to clear a range in Google Sheets using Apps Script.

Prerequisites

This tutorial assumes that you're familiar with the following concepts:

There are a couple of different methods to clear a range in Google Sheets using Apps Script. In each case, the first step is to reference the range that you want to clear. Then you need to use the right method depending on what you want cleared in the range.

Here are some of the methods that you can use:

Clear a range's contents using Apps Script

To clear a range's contents, first reference the range and then use the clearContent() method.

function clearContentsOnly()

Before running the clearContentsOnly() function

Screenshot of a range in a Google Sheets spreadsheet.

After running the function

The range's contents have been cleared but its formatting has been preserved.

Screenshot of a range in a Google Sheets spreadsheet.

Clear a range's formatting using Apps Script

To clear a range's contents, first reference the range and then use the clearFormat() method.

function clearFormattingOnly()

Before running the clearFormat() function

Screenshot of a range in a Google Sheets spreadsheet.

After running the function

The range's contents have been preserved but its formatting has been cleared.

Screenshot of a range in a Google Sheets spreadsheet.

Clear data validation rules in a range using Apps Script

To clear the data validation rules in a range, first reference the range and then use the clearDataValidations() method.

function clearDataValidations()

Before running the clearDataValidations() function

Screenshot of a range in a Google Sheets spreadsheet.

After running the function

The range's contents and formatting have been preserved but its data validation rules have been cleared (which is why the checkboxes have disappeared).

Note: Checkboxes are implemented as data validation rules in Google Sheets.

Screenshot of a range in a Google Sheets spreadsheet.

Clear notes in a range using Apps Script

To clear notes in a range, first reference the range and then use the clearNote() method.

function clearNotes()

Before running the clearNotes() function

Screenshot of a range in a Google Sheets spreadsheet.

After running the function

The notes in the range have been cleared.

Screenshot of a range in a Google Sheets spreadsheet.

Clear the contents, formatting and data validation rules in a range using Apps Script

To clear a range's contents, formatting and data validation rules, first reference the range and then use the clear() method.

function clearContentsFormatsValidations()

Before running the clearContentsFormatsValidations() function

Screenshot of a range in a Google Sheets spreadsheet.

After running the function

The contents, formatting and data validation rules in the range have been cleared.

Screenshot of a range in a Google Sheets spreadsheet.

You can pass a set of advanced options to the clear() method to clear multiple things at the same time

To clear multiple things at the same time, you can pass an object that tells the clear() method what all to clear in the range. The code below clears a range's formatting and contents.

The options that you can configure are:

Note: The commentsOnly option does not work

While the Apps Script documentation suggests that you can also clear comments in a range by using the commentsOnly option, this doesn't seem to work as of Jan 2021.

Here is an example that demonstrates how to clear a range's formatting and contents by using the options object.

function clearContentsAndFormatting() < var range = SpreadsheetApp .getActive() .getSheetByName("Clear Range") .getRange(11,2,2,2); // Configure what to clear in the range var options = < formatOnly: true, contentsOnly: true >; // Pass the options object to the clear() method range.clear(options); >

Before running the clearContentsAndFormatting() function

Screenshot of a range in a Google Sheets spreadsheet.

After running the function

The range's contents and formatting have been cleared.

Screenshot of a range in a Google Sheets spreadsheet.

When you specify multiple options, they seem to be applied one at a time. Therefore, if you want to undo these changes in your sheet using CTRL + Z (or ⌘ + Z on a Mac), you'll need to undo multiple times based on the number of options that you specified.

This behavior isn't covered in the documentation but I've observed it while using this feature.

Here is an example

Consider the range below that contains TRUE and FALSE values.

Screenshot of a range in a Google Sheets spreadsheet.

Suppose you clear the contents and formatting in this range by specifying the options object.

Screenshot of a range in a Google Sheets spreadsheet.

Now, when you undo the change, only the range's formatting will be restored.

Screenshot of a range in a Google Sheets spreadsheet.

You'll need to undo once again to restore the contents of the range.

Screenshot of a range in a Google Sheets spreadsheet.

Conclusion

In this tutorial I showed you how to clear a range in Google Sheets using Apps Script. There are multiple methods to clear a range. Here are the ones that we explored in this tutorial:

Thanks for reading!

Stay up to date

Follow me via email to receive actionable tips and other exclusive content. I'll also send you notifications when I publish new content.

By signing up you agree to the Privacy Policy & Terms . Have feedback for me?

I'd appreciate any feedback you can give me regarding this post.

Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!

Stay up to date

Follow me via email to receive actionable tips and other exclusive content. I'll also send you notifications when I publish new content.

By signing up you agree to the Privacy Policy & Terms .

Tutorials

Learn coding using Google Sheets and Apps Script

Do you use spreadsheets a lot? Did you know that you can build simple applications using spreadsheets or even automate your work? If this sounds exciting, this tutorial is for you 🚀👉.

Google Apps Script Tutorial

Learn Google Apps Script, learn how to use it to automate your tasks, and build simple user interfaces and applications in Google Sheets.

Sending email from Google Sheets

Learn how to send emails right from Google Sheets so you can create birthday reminders or automate sending those boring "Daily report" emails at work.

Build custom user interfaces in Google Sheets

Learn how to build custom user interfaces like alerts, prompts, sidebars, etc. in Google Sheets using Google Apps Script.

Coding Concepts using Sheets

In todays world, it can be useful to understand coding concepts even if you aren't a programmer. I'm writing a series of posts to try and teach some of these concepts using spreadsheets.