Google apps script 使用googlesheet的googlescriptshift调度器

Google apps script 使用googlesheet的googlescriptshift调度器,google-apps-script,google-sheets,drive,Google Apps Script,Google Sheets,Drive,我还在学习谷歌脚本, 现在,我正在一个项目中工作,使用Google sheet自动创建轮班时间表 我设法找到了一些脚本,介绍了如何获得某个范围,并使用一些简单的条件和循环将其填充到特定的单元格中 var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getActiveSheet(); var rangeData = sheet.getDataRange(); var lastColumn = rangeData.getLas

我还在学习谷歌脚本, 现在,我正在一个项目中工作,使用Google sheet自动创建轮班时间表

我设法找到了一些脚本,介绍了如何获得某个范围,并使用一些简单的条件和循环将其填充到特定的单元格中

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");
      }; 
    };
  };

};
但问题更复杂,我需要确保在允许脚本填充时间表之前满足以下几个条件:

每天每个班次必须有最少数量的员工。 每个员工每周工作不得超过5天,休息2天,但不一定按顺序排列 换班情况如下:

你知道如何在不使用其他外部软件的情况下在谷歌脚本中实现这一点吗


提前感谢此代码将帮助您解决您需要的问题。休息日为空单元格:

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")
      }
    }
  }
}
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")
      }
    }
  }
}
文件 有关最佳实践,我建议您检查:


当员工不工作时,该如何显示在单元格中?空白?我是说你怎么知道他们什么时候不工作?在你的床单里考虑一个星期,从第1天到第7天等等?当两个条件都满足时,是否要更改单元格颜色?你能分享一份电子表格,里面有一些虚拟的数据用于测试吗?这比图片更容易帮助你。哦,是的,它应该是空的,或者任何字母都可以工作,颜色可以根据上面的单元格值进行更改,这是初始的移位填充,只是尝试手动显示休息日的样子。这是带有演示数据的谷歌表
/*
  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")
      }
    }
  }
}