Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear cell google sheet script

I have a sheet on googlesheet where there is a lot of line and column.

I want clear the cell where there is the value 0 in the column B.

I wrote this code but it doesn't work, I'm not an expert of javascript :|

function clean0() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getDisplayValues();
  if(data == "0"){
    sheet.getRange('B:B').clearContent();
  }
}

Where is my mistake?

Thanks for help :)

like image 427
overer Avatar asked May 04 '18 02:05

overer


People also ask

How do you automatically delete cells in Google Sheets?

Goto Run & select Run function and then select clearRange. Once you have run the script, your spreadsheet should be cleared.


1 Answers

You want to clear the cell content for the cells of column B which has the value of "0". If my understanding is correct, how about this modification? I think that there are several answers for your situation. So please think of this as one of them.

Modification points :

  • Retrieve values of "B:B".
  • Retrieve ranges when the retrieved values are "0".
  • Clear content for the retrieved ranges.

Modified script :

function clean0() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getRange('B:B').getDisplayValues();
  var range = [];
  data.forEach(function(e, i){
    if (e[0] == "0") range.push("B" + (i + 1));
  });
  sheet.getRangeList(range).clearContent();
}

Reference :

  • getRangeList()
    • This method was added at April 11, 2018.

If I misunderstand your question, I'm sorry.

like image 124
Tanaike Avatar answered Oct 06 '22 01:10

Tanaike