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
Sheetobject - 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 toclear()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!
