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 :)
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 :)
Share Improve this question edited May 4, 2018 at 3:17 Tanaike 201k12 gold badges119 silver badges211 bronze badges asked May 4, 2018 at 2:14 overeroverer 1471 gold badge2 silver badges11 bronze badges 1-
Your mistake is that
data
is an array of arrays of values, and your parisondata == "0"
assumes thatdata
is a value. – tehhowch Commented May 4, 2018 at 4:06
1 Answer
Reset to default 10You 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.