根据条件格式颜色从单元格中清除注释

时间:2019-07-11 08:59:30

标签: google-apps-script google-sheets-macros

我有一个Google表格脚本,该脚本可以清除表格中的所有笔记,并且每天都会触发一次。我真的希望它只是在那天有条件地格式化为绿色的单元格中“清除注释”。作为背景信息,这些注释会通过附加组件自动添加到各个单元格中,但是当该单元格再次变为绿色时,我希望清除该单元格上附加的注释。这可能吗?我提供了到目前为止的代码,但与数组getRange部分混淆了。谢谢!

function cleargreens() {
  var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Overview");

  var data1 = sheet1.getDataRange().getValues();  //2D array of sheet1 cell values
  var bg;

  for(var i=1; i<data1.length; i++)
  {
    bg = sheet1.getRange(i+1, 2).getBackground();
    if(bg == "#b7e1cd") //this is the conditional green color code
    {
      sheet1.getRange(i+1, 3).clearNote();
    } 

My spreadsheet, although the colors I will be searching for will only be in columns E to H

1 个答案:

答案 0 :(得分:0)

如果您所有的单元格都可能具有在背景颜色为绿色时需要删除的笔记-那么您需要实现一个嵌套循环,该循环遍历行和列:

function cleargreens() {
  var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Overview");

  var data1 = sheet1.getDataRange().getValues();  //2D array of sheet1 cell values
  var bg;
//iterates through rows
  for(var i=1; i<data1.length; i++)
  {
//iterates through columns    
    for(var j=1; j<data1[0].length; j++)
    {
      var cell=sheet1.getRange(i+1, j);
      bg = cell.getBackground();
      if(bg == "#b7e1cd") //this is the conditional green color code
      {
        cell.clearNote();
      }
    }
  }
}

对于您的困惑,请注意,范围是具有行和列索引的二维数组。