微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Google 表格中搜索两次之间的命名范围并填充差异

如何解决在 Google 表格中搜索两次之间的命名范围并填充差异

我正在尝试创建时间表热图,以便我们可以调整人员配备时间和天数。为了使这尽可能简单,我提出了一个表格结构,允许用户输入员工的姓名,然后从下拉列表中选择他们的轮班开始时间和轮班结束时间,然后使用复选框指示他们将在哪几天工作,如图所示:

Schedule Select table

最终结果将是一个热图,它计算某个值存在于开始时间和结束时间之间的范围内的实例数,按小时和按天细分。我最初的想法是这样使用 COUNTIFS=COUNTIFS(Calculations!D:D,">=9:00:00",Sheet9!D:D,"=TRUE") Where Calculations!D:D 是所选开始时间的列,其中 ">=9:00:00" 检查开始时间大于或等于上午 9 点,并且其中 Sheet9!D:D,"=TRUE" 检查当天的复选框是否被选中。因此,此示例将检查是否有人在周一上午 9 点工作。

但是,这并没有成功,因为我们正在检查任何大于上午 9 点的值,而且大多数员工的工作时间不会超过 10 小时,所以我得到了误报。

我的下一个想法是使用从开始时间值开始的命名范围,然后在必要时循环回到结束时间(例如,如果员工从晚上 10 点开始工作,而他们的班次在早上 7 点结束) .由于此范围是动态的(并非所有员工每天都严格工作 8 小时),因此我需要检查范围内是否a存在,但是,我不确定如何 A:循环或 B:检查值是否在动态范围内。我认为这需要 Google Apps Script 才能实现,但我并不精通它,而且我一直在努力解决这个问题。任何帮助将不胜感激!

哦,这是所需输出的屏幕截图,其中填充了几个值:

Schedule Heat Map

解决方法

我不知道这是否正是您想要的,但我想练习使用时间是 Google Scripts,所以我解决了您的问题。但是,有一些我没有时间写,我已经在脚本中注释了。

如果我正确理解了你的问题,你想改变生活:

enter image description here

并将其转换为这样的热图:

enter image description here

(我还没有完成您的所有格式设置,但我猜您想查看一天中每小时有多少人有空)

在我的代码中,我将第一张表格称为“Roster”,第二张表格称为“RosterOverview”——注意大小写和缺少空格。

方法

我写了两个脚本。一个(排班)检查工作人员的工作天数,第二个检查他们的工作时间并更新 RosterOverview (updateCalendar) 中的值。

对于每位员工,我都记录了他们的工作时间和工作日。我检查了他们是否在某一天工作,如果他们工作,将他们的工作时间发送到一个名为 updateCalendar 的脚本。然后,第二个脚本查看他们是否上夜班,即是否在午夜工作。然后,脚本会向工作人员被列入名册的每个小时窗口添加 1。

脚本

function rostering() {  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var rosterSheet = ss.getSheetByName("Roster");
  var overviewSheet = ss.getSheetByName("RosterOverview");
  var range = rosterSheet.getRange(2,2,4,9);  //You will need to work out how to accurately get this range for your data - 2 and 2 are the starting row and column respectively i.e. B2; 4 indicates the number of rows; 9 indicates the number of columns
  var times = range.getValues();
  var calendarRange = overviewSheet.getRange("B2:I25"); //On RosterOverview,this is the range of cells for Monday-Monday,midnight-2300. You need 8 columns of data because the extra one is for Sunday night overflowing into Monday morning.

//You will need to write a bit of code yourself to make sure all the values in calendarRange are set to 0. I have set this manually and pull them below when I was testing.

  var calendarValues = calendarRange.getValues();

  //This section loops through each staff member's hours and days.
  //It looks to see if the check box is ticked for each day of the week in turn.
  //If a checkbox is ticked,it calls a second script: updateCalendar
  //It's probably possible to write a separate loop to go through the days of the week

  for (var i = 0; i < range.getHeight(); i++){
    //if Monday shift
  if(times[i][2] == true){
    calendarValues = updateCalendar(times[i][0],times[i][1],calendarValues,0)
  }
    //if Tuesday shift
  if(times[i][3] == true){
    calendarValues = updateCalendar(times[i][0],1)
  }
  //if Wednesday shift
  if(times[i][4] == true){
    calendarValues = updateCalendar(times[i][0],2)
  }
  //if Thursday shift
  if(times[i][5] == true){
    calendarValues = updateCalendar(times[i][0],3)
  }
  //if Friday shift
  if(times[i][6] == true){
    calendarValues = updateCalendar(times[i][0],4)
  }
  //if Saturday shift
  if(times[i][7] == true){
    calendarValues = updateCalendar(times[i][0],5)
  }
  //if Sunday shift
  if(times[i][8] == true){
    calendarValues = updateCalendar(times[i][0],6)
  }

  }
  //Once ithas looped through al the staff member's shifts,it updates the RosterOverview
  //sheet with how many people are on each hour
  calendarRange.setValues(calendarValues);
}

function updateCalendar(startTime,endTime,roster,day){ //Let day be a digit: 0 for Monday,1 for Tuesday.... Roster is the calendarValues array from the roster function
    if(startTime > endTime){ //night shift over midnight
      for (var j = startTime.getHours(); j < 24; j++){
        roster[j][day] = roster[j][day] + 1;        
      }
      for (var j = 0; j < endTime.getHours(); j++){
        roster[j][day + 1] = roster[j][day + 1] + 1;
      }
    } else {
      for (var j = startTime.getHours(); j < endTime.getHours(); j++){
        roster[j][day] = roster[j][day] + 1;
      }
    }  
    return roster;
}

你需要做什么

  1. 最好添加一个自定义菜单,以便您可以从工作表中运行它:请参阅此基础代码实验室了解如何操作:https://developers.google.com/codelabs/apps-script-fundamentals-3#0
  2. 您需要确保您的 Google 表格和 Google 脚本设置为相同的时区。要为脚本设置时区,请在脚本编辑器中单击右上角的 [使用旧版编辑器],然后转到文件 > 项目属性。设置后,返回到新编辑器。
  3. 在再次运行脚本之前,您需要编写一些代码来选择员工轮班的数据范围,并编写一些代码将 RosterOverview 电子表格的值重置为 0。

如果您有任何问题,请告诉我。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。