How to set background color for a row based on current date in Google Docs Spreadsheet?

后端 未结 7 1945
野性不改
野性不改 2021-01-02 04:48

I have a Google Docs SpreadSheet, where in the column A are dates (A1: 2013-11-22, A2: 2013-11-23, A3: 2013-11-24 etc). I would like to automatically highlight - set a backg

7条回答
  •  抹茶落季
    2021-01-02 05:43

    If you want it to be automatic on spreadsheet open you will have to install an installable onOpen that will call the below function (from script editor goto ressources > this script triggers > add a new trigger > sreadsheet / on Open)

    And here is the code for columns: (see below for rows)

    function customOnOpen() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sh = ss.getActiveSheet();
      var headers = sh.getRange(1,1,1,sh.getLastColumn()).getValues();
      var today = new Date().setHours(0,0,0,0);
      for(var n=0;n=2){sh.getRange(1,n-1,sh.getMaxRows(),1).setBackground(null);};// resets the backGround for yesterday if not the first column
          sh.getRange(1,n,sh.getMaxRows(),1).setBackground('yellow');
          break;
        }
      }
    }
    

    this version to colorize rows

    function customOnOpen2() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sh = ss.getActiveSheet();
      var headers = sh.getRange(1,1,sh.getLastRow()).getValues();
      var today = new Date().setHours(0,0,0,0);
      for(var n=0;n=2){sh.getRange(n-1,1,1,sh.getMaxColumns()).setBackground(null);}
          sh.getRange(n,1,1,sh.getMaxColumns()).setBackground('yellow');
          break;
        }
      }
    }
    

    Note : if you want it to run fully automatically based on a timer it's perfectly doable, just change the ss and sh variable using openById and getSheetByName (see doc here)and set up a timer to make it run every day around 1 AM.

提交回复
热议问题