在Google Sheets中计算夜班时间的公式

huangapple go评论58阅读模式
英文:

Formula for calculating night shift times in Google Sheets

问题

我已经努力思考了几个小时,试图在 Excel 表格中计算员工班次的持续时间。
这是酒店业,所以他们下午开始工作,要么在午夜结束,要么在凌晨1点结束。

我正在尝试在值班表中添加一个每周总工时列,理论上当插入新的小时数时,它将自动更新。

但是我遇到一个问题,谷歌表会显示一个奇怪的持续时间为负数,或者在超过24小时后重置。

我将在下面展示。

在这里我们看到我正在处理的表格。值班表以显示人员的开始时间的方式布局。这被格式化为 TIME[HH]:[AM/PM],所以实际上,当一个单元格显示 4 PM 时,这等同于 4:00:00 PM,依此类推。
我在表格上没有 END TIME,也不能更改它(雇主的要求)。

人们在 星期一/星期二/星期三/星期四/星期日 有固定的下班时间,下班时间是午夜(00:00:00),而在 星期五/星期六 下班时间是凌晨1点(01:00:00)。

我还有一些单元格没有说明任何时间,但表示 'OFF',表示某人那天不工作。

我需要制作一个列;如下所示 'Total',显示某人在那周工作的总小时数。

注意 需要计算 Grace 的总时数。

我已经计算了 Bobby 的小时,使用以下公式:

= (IF(B2<>"Off", (TIME(24,0,0) - B2),"0") + 
  IF(C2<>"Off", (TIME(24,0,0) - C2),"0") + 
  IF(D2<>"Off", (TIME(24,0,0) - D2),"0") + 
  IF(E2<>"Off", (TIME(24,0,0) - E2),"0") + 
  IF(F2<>"Off", (TIME(25,0,0) - F2),"0") + 
  IF(G2<>"Off", (TIME(25,0,0) - G2),"0") + 
  IF(H2<>"Off", (TIME(24,0,0) - H2),"0"))

这给我 16:00:00,而我期望的是 40:00:00,这是在 TIME 格式下。
当超过24小时时,数字会重置为 00:00:00
当我在谷歌表中将数字格式化为 Duration 时,它显示 -80:00:00,这是我期望的两倍,并且是负数。

我也意识到我需要考虑到 Delivery 的存在,这使事情变得更加复杂。

我的头脑有点混乱,我感到困惑,我看到有建议使用 FormulaArray,但不知道如何和为什么使用它。

我应该如何计算这里的小时数。

英文:

I've been wracking my brains for hours, trying to calculate duration of people shifts in an excel sheet.
It is the hospitality industry, so they start in the afternoon and finish either midnight or 1am.

I am trying to add a weekly total hours column to their rota sheet, that in theory will auto update when new hours are inserted.

But I am having a problem where Google sheets will show me a weird duration in a negative number, or resets when it goes past 24 hours.

I will show down below.

Week Starting: 1st May Monday Tuesday Wednesday Thursday Friday Saturday Sunday Total
Bobby OFF OFF 4 PM 5 PM 4 PM 4P M 5 PM 16:00:00
Grace OFF OFF DELIVERY OFF IN IN OFF -------
Howard 5 PM OFF 6 PM OFF 5 PM 6 PM 3 PM --------
Katie OFF 5 PM OFF OFF OFF 12 PM 12 PM --------
David OFF OFF OFF OFF OFF 7 PM OFF --------
Max OFF OFF OFF OFF OFF 7 PM OFF --------
Events Event Event Event --------
Weekly Tasks Restock Clean Delivery Restock Clean Restock Clean --------

Here we see the table I am working with. The Rota is laid out to display people start time. this is formatted as TIME[HH]:[AM/PM], so in reality where a cell says 4 PM, this translates to 4:00:00 PM, and so on.
I do not have an END TIME on the Table and cannot change that (Employers request).

People have regular end times, on Monday/Tuesday/Wednesday/Thursday/Sunday, people finish work at midnight (00:00:00), while on Friday/Saturday people finish at 1am (01:00:00).

I also have cells that don't state anytime, but states 'OFF' denoting that someone is not working that day.

I need to make a column; shown here as 'Total' that shows the total hours someone has worked that week.

NOTE I don't need to calculate totals for Grace

I have gotten as far as calculating the hours for Bobby using the formula:

= (If(B2&lt;&gt;&quot;Off&quot;, (TIME(24,0,0) - B2),&quot;0&quot;) + 
  If(C2&lt;&gt;&quot;Off&quot;, (TIME(24,0,0) - C2),&quot;0&quot;) + 
  If(D2&lt;&gt;&quot;Off&quot;, (TIME(24,0,0) - D2),&quot;0&quot;) + 
  If(E2&lt;&gt;&quot;Off&quot;, (TIME(24,0,0) - E2),&quot;0&quot;) + 
  If(F2&lt;&gt;&quot;Off&quot;, (TIME(25,0,0) - F2),&quot;0&quot;) + 
  If(G2&lt;&gt;&quot;Off&quot;, (TIME(25,0,0) - G2),&quot;0&quot;) + 
  If(H2&lt;&gt;&quot;Off&quot;, (TIME(24,0,0) - H2),&quot;0&quot;))

Which gives me 16:00:000 when I am expecting 40:00:00 this is when formatted at TIME
As it goes over 24 hours the number resets to 00:00:00
When I format the number as Duration in Google Sheets, it shows -80:00:00
Which is double what I am expecting and a negative number.

I am also aware I need to account for the presence of Delivery which complicates matters.

My brain is broken and I am confused, I have seen suggestions of using FormulaArray, but am at a loss of how and why to use this.

How do I go about calculating the hours here.

答案1

得分: 1

您可以尝试使用这个 arrayformula()

=map(A2:A,lambda(Λ,if(Λ=&quot;&quot;,,sum(index(let(Σ,offset(Λ,,1,,7),iferror(if(regexmatch(B1:H1,&quot;^(Fr|Sa)&quot;),if(Σ=&quot;&quot;,,&quot;25:00:00&quot;-Σ),if(Σ=&quot;&quot;,,&quot;24:00:00&quot;-Σ)))))))))

在Google Sheets中计算夜班时间的公式

英文:

You may try this arrayformula()

=map(A2:A,lambda(Λ,if(Λ=&quot;&quot;,,sum(index(let(Σ,offset(Λ,,1,,7),iferror(if(regexmatch(B1:H1,&quot;^(Fr|Sa)&quot;),if(Σ=&quot;&quot;,,&quot;25:00:00&quot;-Σ),if(Σ=&quot;&quot;,,&quot;24:00:00&quot;-Σ)))))))))

在Google Sheets中计算夜班时间的公式

答案2

得分: 0

16:00 是一天中已经过去了40小时的时间点,从午夜开始计算。要将结果显示为持续时间而不是时间点,请将结果单元格格式设置为格式 > 数字 > 持续时间。要使用公式完成相同的操作,请使用query() 函数,如下所示:

=query( 
  if(B2 <> "Off", "24:00" - B2, "0:00") + 
  if(C2 <> "Off", "24:00" - C2, "0:00") + 
  if(D2 <> "Off", "24:00" - D2, "0:00") + 
  if(E2 <> "Off", "24:00" - E2, "0:00") + 
  if(F2 <> "Off", "25:00" - F2, "0:00") + 
  if(G2 <> "Off", "25:00" - G2, "0:00") + 
  if(H2 <> "Off", "24:00" - H2, "0:00"), 
  "格式 Col1 '[h]:mm' ", 0 
)

请参阅在Google Sheets中处理日期和时间值

英文:

16:00 is the time of the day when 40 hours have passed from a midnight. To display the result as duration rather than as time of day, format the result cell as Format > Number > Duration. To do the same with a formula, use query(), like this:

=query( 
  if(B2 &lt;&gt; &quot;Off&quot;, &quot;24:00&quot; - B2, &quot;0:00&quot;) + 
  if(C2 &lt;&gt; &quot;Off&quot;, &quot;24:00&quot; - C2, &quot;0:00&quot;) + 
  if(D2 &lt;&gt; &quot;Off&quot;, &quot;24:00&quot; - D2, &quot;0:00&quot;) + 
  if(E2 &lt;&gt; &quot;Off&quot;, &quot;24:00&quot; - E2, &quot;0:00&quot;) + 
  if(F2 &lt;&gt; &quot;Off&quot;, &quot;25:00&quot; - F2, &quot;0:00&quot;) + 
  if(G2 &lt;&gt; &quot;Off&quot;, &quot;25:00&quot; - G2, &quot;0:00&quot;) + 
  if(H2 &lt;&gt; &quot;Off&quot;, &quot;24:00&quot; - H2, &quot;0:00&quot;), 
  &quot;format Col1 &#39;[h]:mm&#39; &quot;, 0 
)

See Working with date and time values in Google Sheets.

huangapple
  • 本文由 发表于 2023年5月11日 05:20:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定