Clear a Sheet but Keep Header Row

Wiping a sheet before a fresh import is common — but you usually want the header row to survive. This clears content below the header(s) without touching formatting on row 1 (unless you extend it).

What you'll need

  • A Sheet object
  • How many header rows to keep (default 1)

How to use this snippet

function clearSheetKeepHeaders(sheet, headerRows) { headerRows = headerRows || 1; var lastRow = sheet.getLastRow(); var lastCol = sheet.getLastColumn(); if (lastRow <= headerRows || lastCol < 1) return; sheet.getRange(headerRows + 1, 1, lastRow - headerRows, lastCol).clearContent(); } function refreshDailyTab() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Daily'); clearSheetKeepHeaders(sheet, 1); // then write fresh rows under the headers }

Tips:

  • Uses clearContent() so cell colors / validation stay put. Swap to clear() if you want formatting gone too.
  • If the sheet is empty or only headers, this is a no-op — safe to call every run.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free themes and snippets — plus optional Co-Pilot when you want a boost (1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension