javascript 清除单元格谷歌工作表脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50166192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:57:28  来源:igfitidea点击:

Clear cell google sheet script

javascriptgoogle-apps-scriptgoogle-sheets

提问by overer

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

我在 googlesheet 上有一张工作表,其中有很多行和列。

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

我想清除 B 列中值为 0 的单元格。

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

我写了这段代码,但它不起作用,我不是 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 :)

感谢帮助 :)

回答by Tanaike

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.

您想清除 B 列值为“0”的单元格的单元格内容。如果我的理解是正确的,这个修改怎么样?我认为对于您的情况有几种答案。因此,请将此视为其中之一。

Modification points :

改装要点:

  • Retrieve values of "B:B".
  • Retrieve ranges when the retrieved values are "0".
  • Clear content for the retrieved ranges.
  • 检索“B:B”的值。
  • 当检索到的值为“0”时检索范围。
  • 清除检索范围的内容。

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.

如果我误解了你的问题,我很抱歉。