I have written this script to clear data and keep formulas, however I would like to keep
the letters. IE column titles I have made so that when I enter data I know where it goes.
This is my current script:
function onOpen(e) {
SpreadsheetApp.getUi().createMenu('Utills')
.addItem("Entire current sheet", 'entireSheet')
.addItem('Current active range', 'activeRange')
.addToUi();
}
function entireSheet() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const range = sheet.getDataRange();
keepFormulas(range);
}
function activeRange() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const range = sheet.getActiveRange();
keepFormulas(range);
}
function keepFormulas(range) {
const formulas = range.getFormulas();
range.clearContent();
range.setFormulas(formulas);
}
I am completely stumped on how to achieve this.
2
Answers
You can target a range that excludes row 1. This means the range you’d pass to the keepFormulas function would be from row 2 and onwards. This will preserve row 1 and let you keep your titles
Exclude Header row from clearing
To exclude the header row from being cleared, you should specify a custom range using various methods from Apps Script’s
Class Range
likegetLastRow
,getLastColumn
etc. This modification achieves the goal of clearing the text while preserving the formulas and the header row.Code
Sample Outout
Before running the script
After running the script
References:
Class Range Methods