/* cardUI
* generate day and weekday every day
* markout who did not punch in previous day
*/

// Day of year
function dayOfYear(date) {
    if (!(date instanceof Date)) {
        return 0;
    }
    var start = new Date(date.getFullYear(), 0, 0);
    var diff = date - start;
    var oneDay = 86400000; // (1000*60*60*24)
    return Math.floor(diff / oneDay);
}

function dayRoutine() {
    var row = dayOfYear(new Date());
    var offset = 2;
    var punchCardSheet = SpreadsheetApp.openById('1d3jLeX_FcNEEq_bQvcK7LRKQnq01-8hKHH5JSR_HH5k').getSheetByName("Punch");
    generateDayHead(punchCardSheet, row + offset);
    markMiss(punchCardSheet, row + offset);
}

function generateDayHead(sheet, row) {
    var today = new Date();
    sheet.getRange(row, 1).setValue(today);
    var weekDay = { 1: '月', 2: '火', 3: '水', 4: '木', 5: '金', 6: '土', 7: '日' };
    sheet.getRange(row, 2).setValue(weekDay[today.getDay()]);
}

function markMiss(sheet, row) {
  var header = sheet.getRange("1:1").getValues();
  Logger.log(header);
    for(var i=3; i<=header[0].length; i++){
        if(sheet.getRange(row, i).isBlank()){
            sheet.getRange(row, i).setBackground("red");
        }
    }
}

function onOpen(e){
    var row = dayOfYear(new Date());
    var punchCardSheet = SpreadsheetApp.openById('1d3jLeX_FcNEEq_bQvcK7LRKQnq01-8hKHH5JSR_HH5k').getSheetByName("Punch");
    SpreadsheetApp.setActiveRange(punchCardSheet.getRange(row + 10, 1)); //Move activate cell to recent date
} 
@@ -3,16 +3,17 @@
  cardUI%0A
+ 
 * genera
@@ -41,16 +41,17 @@
 ery day%0A
+ 
 * markou
@@ -86,16 +86,17 @@
 ous day%0A
+ 
 */%0A%0A// D
@@ -795,63 +795,123 @@
  = %7B
- 1: '%E6%9C%88', 2: '%E7%81%AB', 3: '%E6%B0%B4', 4: '%E6%9C%A8', 5: '%E9%87%91', 6: '%E5%9C%9F', 7
+%0A        1: '%E6%9C%88',%0A        2: '%E7%81%AB',%0A        3: '%E6%B0%B4',%0A        4: '%E6%9C%A8',%0A        5: '%E9%87%91',%0A        6: '%E5%9C%9F',%0A        0
 : '%E6%97%A5'
+%0A   
  %7D;%0A
@@ -1007,16 +1007,18 @@
  row) %7B%0A
+  
   var he
@@ -1063,50 +1063,33 @@
 ();%0A
-  Logger.log(header);%0A
     for
+ 
 (var i
-=
+ = 
 3; i
+ 
 %3C=
+ 
 head
@@ -1106,16 +1106,17 @@
 th; i++)
+ 
 %7B%0A      
@@ -1119,16 +1119,17 @@
       if
+ 
 (sheet.g
@@ -1154,16 +1154,17 @@
 Blank())
+ 
 %7B%0A      
@@ -1251,16 +1251,17 @@
 nOpen(e)
+ 
 %7B%0A    va
@@ -1523,9 +1523,9 @@
 t date%0A%7D
- 
+%0A

getDay, Sunday is 0 not 7