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.
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.
Google Sheets does not support freezing arbitrary columns and rows. For e.g., you cannot freeze 2 rows (say rows 10 and 11) in the middle of the sheet.
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
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!