brand brand
brand brand

Reputation: 1

Can I clear two seperate ranges with a button script? Google Sheets

I'm trying to clear two seperate ranges with the click of a button but can't figure out how to combine the ranges together

I use this for one range and the other range I need to combine is 'E15:J18'

function ClearCells() {

var sheet = SpreadsheetApp.getActive().getSheetByName('OOTW');

sheet.getRange('E7:J12').clearContent();}`

Upvotes: 0

Views: 672

Answers (3)

Tanaike
Tanaike

Reputation: 201398

I believe your goal is as follows.

  • You want to use clearContent() to multiple ranges of 'E7:J12', 'E15:J18' of "OOTW" sheet.

In this case, how about the following sample script?

Sample script:

SpreadsheetApp
  .getActive()
  .getSheetByName('OOTW')
  .getRangeList(['E7:J12', 'E15:J18'])
  .clearContent();

References:

Upvotes: 0

Cooper
Cooper

Reputation: 64072

Clear RangeList

function clearRangeList() {
  SpreadsheetApp.getActiveSheet().getRangeList(["E7:J12","E15:J18"]).getRanges().forEach(rg => rg.clearContent())
}

Upvotes: 0

mss_
mss_

Reputation: 43

If I understand you correctly, you want to clear multiple, discontinuous ranges on a Spreadsheet with a single function.

My suggestion would be to add another line to your function that gets the subsequent discontinuous range, and clears it.

function clearCells() {
  const sheet = SpreadsheetApp.getActive().getSheetByName("OOTW");
  sheet.getRange("E7:J12").clearContent();
  sheet.getRange("E15:J18").clearContent();
}

Upvotes: 2

Related Questions