Home > OS >  How to clear values from range keeping headers in Google sheet script
How to clear values from range keeping headers in Google sheet script

Time:03-17

Every week I need to clear some information from 5 worksheets. I have to keep the headers and some columns.

How do I just create a script in Googlesheet that I can click and will clear all the cells from A2 to G (until there is value) for 5 different worksheets?

Here is the link with an example with what has to be cleared up. https://prnt.sc/2LpMWmb0esUd

Thank you in advance

CodePudding user response:

You can put all the sheet name in an array and loop all the sheet to clear content on range A2:G.

Open script editor from your spreadsheet and use this sample script:-

function clear()
{
   const ss = SpreadsheetApp.getActiveSpreadsheet()
   const sheets = ['Sheet1','Sheet2'] // your sheetNames
   sheets.forEach(s=> ss.getSheetByName(s).getRange('A2:G').clearContent() )
}

You can set a timeBased trigger for this function to run on a weekly bases.

Reference:-

clearContent

Time Base Trigger

CodePudding user response:

Clear Sheets

function clear() {
  const ss = SpreadsheetApp.getActive()
  const shts = ['Sheet1', 'Sheet2'];
  shts.filter(sh => ~shts.indexOf(sh.getName())).forEach(sh => sh.getRange(2, 1, sh.getLastRow() - 1, 7).clearContent());
}
  • Related