Script:
function lockFirstRowAsHeading() {
// Open the active spreadsheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Check if the first row is already frozen
if (sheet.getFrozenRows() !== 1) {
// Freeze the first row
sheet.setFrozenRows(1);
}
// Notify the user
SpreadsheetApp.getUi().alert("The first row has been locked as a heading.");
}
How It Works:
- Get the Active Sheet:
- The script retrieves the active sheet in the active spreadsheet.
- Check Frozen Rows:
- It checks if the first row is already frozen using
getFrozenRows()
.
- It checks if the first row is already frozen using
- Freeze the First Row:
- If the first row is not frozen, it freezes it using
setFrozenRows(1)
.
- If the first row is not frozen, it freezes it using
- User Notification:
- After freezing the first row, it shows a confirmation dialog box to notify the user.
Steps to Use:
- Open your Google Sheet.
- Go to
Extensions
>Apps Script
. - Paste the code into the Apps Script editor.
- Save the script.
- Run the
lockFirstRowAsHeading
function.
Result:
The first row of the sheet will be frozen, making it a header that stays visible when scrolling down the sheet.