最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Clear cell google sheet script - Stack Overflow

programmeradmin4浏览0评论

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 parison data == "0" assumes that data is a value. – tehhowch Commented May 4, 2018 at 4:06
Add a ment  | 

1 Answer 1

Reset to default 10

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.

发布评论

评论列表(0)

  1. 暂无评论