简体   繁体   中英

Change background color for columns with weekend dates in google sheets with apps script?

I'm attempting to write a script that changes the background color of each column containing a weekend date. For some reason, the script gets thrown off for the month of March and I have no idea why.

For purposes of debugging, I limited the script to only change Sundays. Here is what I know:

  1. Script loops through each column in sequential order as expected without skipping a column (col 2 - col 85)
  2. Script colors the wrong dates in March
  3. March 8 appears to be the culprit
    • it reads the date 3/8, correctly as a Sunday, but it also reads the following Monday (3/9) as "Sun Mar 08" instead of "Mon Mar 09". So it reads both Sunday (3/8) and Monday (3/9) as "Sun Mar 08".
    • dates remain off by 1 for the rest of March
  4. "Sun Apr 05" gets skipped all together - goes from "Sat Apr 04" to "Mon Apr 06"
  5. Dates resume as expected on "Mon Apr 06"
  6. Happens to 2019 dates as well (dates off by one starting the 2nd Sunday in March to the 2nd Sunday in April)

I'm 100% perplexed. I've googled endlessly and tried so many things to no avail! Anyone have a clue what I'm missing or doing incorrectly?! Any help would be greatly appreciated!

Here's the script:

//Change column background color for weekend dates

function colorAll() {

  var sheet = SpreadsheetApp.getActiveSheet();
  var startCol = 2;  //start with column B
  var endCol = sheet.getLastColumn();  // find last column in the sheet

  for (var c = startCol; c <= endCol; c++) { //loop through columns
    colorCol(c);
  }
}

//Change background color for each column passed to 'c'
function colorCol(c) {

  var sheet = SpreadsheetApp.getActiveSheet();
  var dataRange = sheet.getRange(1, c, 5, 1); //row 1, column "c", selection size = 5 rows by 1 column

  var data = dataRange.getValues();
  var col = data[0];

  var date = new Date(col); 
  var dayOfWeek = date.getDay();

//Logs values being passed to each variable in order to help debug process
  Logger.log("*** Processing column number: " + c + " now ***");
  Logger.log(col);
  Logger.log(date); 
  Logger.log(dayOfWeek);


  //If the day of week is Sunday:
  if (dayOfWeek == 0) {
      dataRange.setBackground("#E8E8E8");  //set weekends to grey
    } else {
      dataRange.setBackground("#ffffff");  //set non-weekends to white
    }

//  //If day of week is Sat or Sun:
//  if (dayOfWeek == 0 || dayOfWeek == 6) {
//    if (col[0] === "") {
//      dataRange.setBackgroundRGB(255, 255, 255);
//    } else {
//      dataRange.setBackground("#E8E8E8");
//    }
//  }

}

I put together the following google sheet for testing. Permissions are set so anyone with the link can edit so have at it :)
By double clicking B1, you can choose whatever start day you want. The bottom 3 rows return the week day number as read by sheets (1-7), the week day number as read by apps script (0-6), and the corresponding number for each column (B=2, C=3, etc).

Also, in case it helps...

Latest log for the dates surrounding March 8
expected date pattern = 6, 0, 1, 2, 3
returned date pattern = 6, 0, 0, 1, 2

[20-03-10 10:13:05:886 PDT] ***Processing column number: 7 now***   
[20-03-10 10:13:05:887 PDT] [Sat Mar 07 00:00:00 GMT-06:00 2020]
[20-03-10 10:13:05:888 PDT] Sat Mar 07 00:00:00 GMT-06:00 2020
[20-03-10 10:13:05:890 PDT] 6.0
[20-03-10 10:13:06:207 PDT] *** Processing column number: 8 now ***
[20-03-10 10:13:06:209 PDT] [Sun Mar 08 00:00:00 GMT-06:00 2020]
[20-03-10 10:13:06:211 PDT] Sun Mar 08 00:00:00 GMT-06:00 2020
[20-03-10 10:13:06:212 PDT] 0.0
[20-03-10 10:13:06:555 PDT] *** Processing column number: 9 now ***
[20-03-10 10:13:06:558 PDT] [Sun Mar 08 23:00:00 GMT-06:00 2020]
[20-03-10 10:13:06:561 PDT] Sun Mar 08 23:00:00 GMT-06:00 2020
[20-03-10 10:13:06:563 PDT] 0.0
[20-03-10 10:13:06:915 PDT] *** Processing column number: 10 now ***
[20-03-10 10:13:06:917 PDT] [Mon Mar 09 23:00:00 GMT-06:00 2020]
[20-03-10 10:13:06:918 PDT] Mon Mar 09 23:00:00 GMT-06:00 2020
[20-03-10 10:13:06:920 PDT] 1.0
[20-03-10 10:13:07:343 PDT] *** Processing column number: 11 now ***
[20-03-10 10:13:07:345 PDT] [Tue Mar 10 23:00:00 GMT-06:00 2020]
[20-03-10 10:13:07:347 PDT] Tue Mar 10 23:00:00 GMT-06:00 2020
[20-03-10 10:13:07:348 PDT] 2.0

Log for the dates surrounding April 5
expected date pattern = 5, 6, 0, 1
returned date pattern = 5, 6, 1, 2

[20-03-10 10:13:15:446 PDT] *** Processing column number: 35 now ***
[20-03-10 10:13:15:447 PDT] [Fri Apr 03 23:00:00 GMT-06:00 2020]
[20-03-10 10:13:15:449 PDT] Fri Apr 03 23:00:00 GMT-06:00 2020
[20-03-10 10:13:15:450 PDT] 5.0
[20-03-10 10:13:16:032 PDT] *** Processing column number: 36 now ***
[20-03-10 10:13:16:036 PDT] [Sat Apr 04 23:00:00 GMT-06:00 2020]
[20-03-10 10:13:16:038 PDT] Sat Apr 04 23:00:00 GMT-06:00 2020
[20-03-10 10:13:16:039 PDT] 6.0
[20-03-10 10:13:16:334 PDT] *** Processing column number: 37 now ***
[20-03-10 10:13:16:336 PDT] [Mon Apr 06 00:00:00 GMT-05:00 2020]
[20-03-10 10:13:16:338 PDT] Mon Apr 06 00:00:00 GMT-05:00 2020
[20-03-10 10:13:16:339 PDT] 1.0
[20-03-10 10:13:16:652 PDT] *** Processing column number: 38 now ***
[20-03-10 10:13:16:658 PDT] [Tue Apr 07 00:00:00 GMT-05:00 2020]
[20-03-10 10:13:16:678 PDT] Tue Apr 07 00:00:00 GMT-05:00 2020
[20-03-10 10:13:16:684 PDT] 2.0

Thank you in advance for your time and help!

UPDATE: Adding screenshot of the aforementioned google sheet for reference
debugScript_colorAll screenshot

(note: it says I'm not allowed to embed images yet since I am a new user. Apparently a link to the image is the best I can do. Sorry Cooper)

The problem in this case is a mismatch between the timezone in your script and the timezone in your sheet, and more specifically that one of those timezones is observing a daylight-savings time change while the other is either not, or is observing it a month later.

The sudden time change to 23:00:00 on the "second" March 8th in your logs is the main clue. Note how the previous times are at 00:00:00 - midnight.

Your sheet is set to "Central Time", while your script is set to "Central Time - Mexico City". Apparently they disagree in March.

One solution is to ensure both your script and sheet are in the same timezone.

In the sheet this is found under File -> Settings. In the script under File -> Properties.

If you want to preserve the different timezones, or if you want the script to be timezone, you will need to take further action in your code to manually handle the difference, which can be complex. (It can also be as simple as ensuring the dates are at noon, instead of midnight).

Try this:

function colorAll() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var sc=2;
  var vs=sh.getRange(1,sc,1,sh.getLastColumn()).getValues()[0];
  vs.forEach(function(c,i){
    var dow=new Date(c).getDay();
    //Sun - Sat = 0 - 6
    if(dow==6 || dow==0) {
      sh.getRange(1,i+sc,5,1).setBackground('#E8E8E8');
    }else{
      sh.getRange(1,i+sc,5,1).setBackground('#FFFFFF');
    }
  });
}

Animation:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM