[英]Google Script shift scheduler using Google sheet
我仍在學習 Google Script,現在我正在從事一個使用 Google 工作表自動創建輪班計划的項目
我設法找到了一些關於如何獲得特定范圍的腳本,並使用一些簡單的條件和循環將其填充到具有特定值的特定單元格中
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);
function onOpen() {
ui.createMenu('Checker')
.addItem('Bad Way', 'badWay')
.addItem('Good Way', 'goodWay')
.addToUi();
};
/*
BAD - Go to each cell and see if it contains a value
and then fill in the background if it contains a dash or
zero
*/
function badWay() {
//Loop through each column and each row in the sheet.
for(i = 1; i < lastColumn; i++){
for (j = 1; j < lastRow ; j++){
var cell = searchRange.getCell(j,i).getValue();
if (cell === "-"){
sheet.getRange(j+1,i+1).setBackground("#cc4125");
}else if (cell === 0){
sheet.getRange(j+1,i+1).setBackground("#e69138");
};
};
};
};
/*
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() {
// Get array of values in the search Range
var rangeValues = searchRange.getValues();
// Loop through array and if condition met, add relevant
// background color.
for ( i = 0; i < lastColumn - 1; i++){
for ( j = 0 ; j < lastRow - 1; j++){
if(rangeValues[j][i] === "-"){
sheet.getRange(j+2,i+2).setBackground("#cc4125");
}else if (rangeValues[j][i] === 0){
sheet.getRange(j+2,i+2).setBackground("#e69138");
};
};
};
};
但問題更復雜,在允許腳本填寫時間表之前,我需要確保一些條件,例如:
班次看起來像這樣:
此代碼將幫助您完成所需的操作(休息日為空單元格):
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")
}
}
}
}
請注意我如何添加兩個函數( daysWorkedByWeek
和numbDailyEmp
)來分隔您需要的兩個任務
daysWorkedByWeek
為每位員工檢查他/她每周休假的天數。
/*
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
每天檢查是否有最低數量的員工在當天工作,具體取決於他們的班次。
/*
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")
}
}
}
}
對於最佳實踐,我建議您檢查:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.