Freeze rows and columns in Google Sheets using Apps Script

Google Sheets has a useful feature where you can freeze rows and columns to keep them in view as you scroll horizontally or vertically.

In this tutorial I will show you how to freeze rows and columns in Google Sheets using Apps Script. This is especially useful if you are writing data to your spreadsheet via Apps Script and you want to freeze the header row.

The screenshot below shows you a spreadsheet with a single row and column that have been frozen. Therefore, Column A and Row 1 will remain in view even when you scroll.

Screenshot of a Google Sheets spreadsheet with a row and a column that is frozen.

To freeze a row using Apps Script, use the setFrozenRows(N) method of the Sheet object where N is the number of rows at the top to freeze. Similarly, use setFrozenColumns(N) to freeze N columns starting from the left.

function freezeRowsAndColumns() {
  var activeSheet = SpreadsheetApp.getActiveSheet();
  activeSheet.setFrozenRows(1);
  activeSheet.setFrozenColumns(1);
}

To unfreeze rows and columns, use the same method with N set to 0.

function unfreezeRowsAndColumns() {
  var activeSheet = SpreadsheetApp.getActiveSheet();
  activeSheet.setFrozenRows(0);
  activeSheet.setFrozenColumns(0);
}

Conclusion

In this tutorial, I showed you how to use the setFrozenRows() and the setFrozenColumns() methods of the Sheet object to freeze and unfreeze rows and columns in Google Sheets using Apps Script.

Thanks for reading!

Master Google Sheets Automation

Sign up to receive exclusive Automation tips and tutorials!
I'll send you actionable tips on automating tasks with Google Sheets and coding with Apps Script. You'll also be notified whenever new content is published.
PLUS: Get instant access to my Birthday Reminder Template! 🎂
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!