6
votes

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 :)

1
Your mistake is that data is an array of arrays of values, and your comparison data == "0" assumes that data is a value. - tehhowch

1 Answers

8
votes

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 :

If I misunderstand your question, I'm sorry.