Google Apps Script that locks the first row as a heading

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:

  1. Get the Active Sheet:
    • The script retrieves the active sheet in the active spreadsheet.
  2. Check Frozen Rows:
    • It checks if the first row is already frozen using getFrozenRows().
  3. Freeze the First Row:
    • If the first row is not frozen, it freezes it using setFrozenRows(1).
  4. User Notification:
    • After freezing the first row, it shows a confirmation dialog box to notify the user.

Steps to Use:

  1. Open your Google Sheet.
  2. Go to Extensions > Apps Script.
  3. Paste the code into the Apps Script editor.
  4. Save the script.
  5. 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.