This code will help you with what you want (Days off are empty cells):
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(2, 2, lastRow-1, lastColumn-1);
var daysNotWorkedCounter = 0;
var numberEmpls = 0;
var emplWorkingDay = 0;
var emplWorkingNight = 0;
function onOpen() {
var entries = [{
name : "Good Way",
functionName : "goodWay"
}]
ss.addMenu('Checker', entries);
};
/*
GOOD - Create a client-side array of the relevant data
search through the array and if there is a dash or zero,
then add the relevant background color.
*/
function goodWay() {
var rangeValues = searchRange.getValues();
daysWorkedByWeek(rangeValues);
numbDailyEmp(rangeValues);
}
/*
It checks for every user if he/she has worked more than two days
It also sets to red the cells where are days off
*/
function daysWorkedByWeek(rangeValues){
// It checks all cols in a row
for(row = 0 ; row < lastRow - 1; row++){
daysNotWorkedCounter = 0;
for (col = 0; col < lastColumn - 1; col++){
// If there is a day off (empty cell), then set a color to that cell
// and count the day the empl is not working
if(!rangeValues[row][col] && col !== 0){
sheet.getRange(row+2,col+2).setBackground("#ff0000");
daysNotWorkedCounter++;
// If there are more than two that week, Log a message
if(daysNotWorkedCounter > 2){
Logger.log("This employee has worked more than two days in a week: " + sheet.getRange(row+2,1).getValues());
}
}
// If 7 days passed (one week), restart the counter
if(col % 7 === 0) daysNotWorkedCounter = 0;
}
}
}
/*
For every day it checks how many employees are working in each shift
*/
function numbDailyEmp(rangeValues){
// It checks all rows in a col
for (col = 0; col < lastColumn - 1; col++){
emplWorkingDay = 0;
emplWorkingNight = 0;
for(row = 0 ; row < lastRow - 1; row++){
// Checks empls working that day
if(rangeValues[row][col] && col !== 0){
// Count the numbers of empls depending in the shift
if(rangeValues[row][0] === "Morning") emplWorkingDay++;
else if (rangeValues[row][0] === "Night") emplWorkingNight++;
}
}
// Log a message with the number of empls working in a shift that day
if(col !== 0){
Logger.log("Number of employees working at day " + emplWorkingDay);
Logger.log("Number of employees working at night " + emplWorkingNight);
// if there are less than a min, then Log a message
if(emplWorkingDay < 6 || emplWorkingNight < 6){
Logger.log("Not enough working that day")
}
}
}
}
Notice how I added two functions (daysWorkedByWeek and numbDailyEmp) to separate the two tasks you were requiring
daysWorkedByWeek checks for every employee the number of days he/she has had off by a week.
/*
It checks for every user if he/she has worked more than two days
It also sets to red the cells where are days off
*/
function daysWorkedByWeek(rangeValues){
// It checks all cols in a row
for(row = 0 ; row < lastRow - 1; row++){
daysNotWorkedCounter = 0;
for (col = 0; col < lastColumn - 1; col++){
// If there is a day off (empty cell), then set a color to that cell
// and count the day the empl is not working
if(!rangeValues[row][col] && col !== 0){
sheet.getRange(row+2,col+2).setBackground("#ff0000");
daysNotWorkedCounter++;
// If there are more than two that week, Log a message
if(daysNotWorkedCounter > 2){
Logger.log("This employee has worked more than two days in a week: " + sheet.getRange(row+2,1).getValues());
}
}
// If 7 days passed (one week), restart the counter
if(col % 7 === 0) daysNotWorkedCounter = 0;
}
}
}
numbDailyEmp checks for every day if the minimum amount of employees are working that day depending in their shifts.
/*
For every day it checks how many employees are working in each shift
*/
function numbDailyEmp(rangeValues){
// It checks all rows in a col
for (col = 0; col < lastColumn - 1; col++){
emplWorkingDay = 0;
emplWorkingNight = 0;
for(row = 0 ; row < lastRow - 1; row++){
// Checks empls working that day
if(rangeValues[row][col] && col !== 0){
// Count the numbers of empls depending in the shift
if(rangeValues[row][0] === "Morning") emplWorkingDay++;
else if (rangeValues[row][0] === "Night") emplWorkingNight++;
}
}
// Log a message with the number of empls working in a shift that day
if(col !== 0){
Logger.log("Number of employees working at day " + emplWorkingDay);
Logger.log("Number of employees working at night " + emplWorkingNight);
// if there are less than a min, then Log a message
if(emplWorkingDay < 6 || emplWorkingNight < 6){
Logger.log("Not enough working that day")
}
}
}
}
Docs
For best practices I recommend you check: