Munish Jain
Munish Jain

Reputation: 25

Clear columns of selected cell row?

I want to clear contents of selected row (not delete entire row). Can someone help?

enter image description here

Eg: D3 is selected Clear D3,E3,F3,G3,H3 but not delete the row

Upvotes: 0

Views: 141

Answers (3)

Munish Jain
Munish Jain

Reputation: 25

function Deleterow() {
  const sheetName = "Anju"; // put the name of your sheet here
  var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell();
  const sheet = range.getSheet();
  if(sheet.getName()==sheetName && range.getColumn() == 4){
    sheet.getRange(range.getRow(),4,1,5).clearContent();
  }  
}

Upvotes: 0

Marios
Marios

Reputation: 27348

Explanation:

You are looking for the onSelectionChange(e).

The following script will clear D:H of the row/cell you selected in column D.

Code snippet:

function onSelectionChange(e) {
  const sheetName = "Sheet1"; // put the name of your sheet here
  const range = e.range;
  const sheet = range.getSheet();
  if(sheet.getName()==sheetName && range.getColumn() == 4){
    sheet.getRange(range.getRow(),4,1,5).clearContent();
  }  
}

I hope it is clear that this is a trigger function. Namely, you are not supposed to manually execute it by yourself but it will be triggered upon selections in the sheet (see Illustration).

Illustration:

enter image description here

Possible Restriction:

  • onSelectionChange is a little bit buggy but it can work really well. If it does not work in the beginning, disable the v8 run time environment, refresh the script editor page and then enable v8 environment again. You might need to wait or refresh the page again but it will eventually work. Please read the related issue here.

Upvotes: 2

Daniel H.
Daniel H.

Reputation: 670

Simply select the cells the you wish to clear and hit backspace to clear the contents of the cells.

Alternatively, run this function in the script editor:

function clearData(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('THE_NAME_OF_YOUR_SHEET').getRange('D3:H3');    
  ss.clear();
}

Upvotes: 0

Related Questions