自动更新 Google 工作表中的公式,以便计算具有每周更新值的两个相邻列之间的差异

Automatically Update Formula in Google Sheets so that it calculates difference between two adjacent columns with weekly updated values

我有下一行表示参加团队会议:

Employee  Wk 3/7    Wk of 3/14  Wk of 3/21  Wk of 3/28  Wk of 4/4   
John

Wk [Date] 列每周一早上更新。下周一我会看到这样的东西

Employee  Wk 3/7    Wk of 3/14  Wk of 3/21  Wk of 3/28  Wk of 4/4   
John        4            

John 在 3 月 7 日那周参加了四次会议

在那之后的一周我会看到这样的东西:

Employee  Wk 3/7    Wk of 3/14  Wk of 3/21  Wk of 3/28  Wk of 4/4   
John        4        5

约翰在 3 月 14 日那周参加了 5 次会议。

我在 Employee John 旁边创建了一个专栏:

  A                 B               C         D         E           F
1 Employee  Change from last Week Wk 3/7    Wk of 3/14  Wk of 3/21  Wk of 3/28  Wk of 4/4   
John                                 4        5

下周单元格的更改将具有以下 Google 工作表公式:

=if(D1<C1, "Negative Attendance Trend", "")

每个星期一,相邻的列都会更新有关该周出勤的信息。我不想在每个星期一早上都去并调整公式如下

=if(E1<D1, "Negative Attendance Trend", "")

然后

=if(F1<E1, "Negative Attendance Trend", "") 

我想移动公式,当新值出现在 ** 列的那一周时,每周自动向右移动一列,并从其前一列中的值中减去该出勤率左。

例如:

=if([Most Recent Full Week's Attendance] <[The second most Recent Full Week's Attendance], "Negative Change", "")

还是

=if([Most Recent Full Week's Attendance] > [The second most Recent Full Week's Attendance], "Positive Attendance Trend", "")

我曾尝试使用动态 Google Sheets 数组,结果却卡在那里,不知道如何继续进行自动重新计算,因为每周都会填充周列。

更新2

脚本

最好的解决方案可能是使用脚本。

转到“工具”>“脚本编辑器”,然后将此代码粘贴到其中:

function LastTwoDiff(Arr) {
  var Response = [];
    for(var i = 0; i < Arr.length; i++) {
      var cube = Arr[i];
      for(var j = cube.length-1; j >=0; j--) {
        if (cube[j] !== '') {
          Response.push(cube[j]-cube[j-1]);
          break;
      }
    }
  }
return Response;  
}

那么就可以照常使用公式了:

=LastTwoDiff(C2:AB3)

它将填充值并粘贴计算出的数组,其差值为:

  1. 从右侧(列)找到的最后一个单元格,其中值 <> ''
  2. 上一个左侧单元格

您只需将此公式粘贴到 B2:

=if(LastTwoDiff(C2:BD4)>0,"","Negative...")

拜托,look at example

但如果您仍然只需要无脚本解决方案,那就更进一步。


简单公式

这个公式可以用,但是要抄下来:

=if(INDEX(A2:H2,1,ArrayFormula(max(--(C2:H2>0)*column(C2:H2))))<INDEX(A2:H2,1,ArrayFormula(max(--(C2:H2>0)*column(C2:H2)))-1), "Negative Attendance Trend", "")

结果是:

公式在单元格 B2 中输入,然后我将其复制到 B2:B6。添加新数据时,公式会正确运行。 Example File。如果只有一列有数据,公式给出 #REF!,所以我建议使用 IFERROR

或者这个公式会做同样的事情:

=if(QUERY({ArrayFormula(TRANSPOSE(COLUMN(C2:H2))),TRANSPOSE(C2:H2)},"select Col2 where Col2 > 0 order by Col1 desc limit 1")<QUERY({ArrayFormula(TRANSPOSE(COLUMN(C2:H2))),TRANSPOSE(C2:H2)},"select Col2 where Col2 > 0 order by Col1 desc limit 1 offset 1"), "Negative Attendance Trend", "")

数组公式

这个公式并不优雅,但它会自动填充:

=ArrayFormula(if(MMULT(ArrayFormula(C2:H6*--(if(row(C2:H6),COLUMN(C2:H6))=(sort(QUERY(TRANSPOSE(SPLIT(CONCATENATE(if(COLUMN(C2:H6),ROW(C2:H6))&"."&if(row(C2:H6),COLUMN(C2:H6))*(C2:H6<>"")&"|"),"|")),"order by Col1 desc skipping "&COLUMNS(C1:H1)))-INT(sort(QUERY(TRANSPOSE(SPLIT(CONCATENATE(if(COLUMN(C2:H6),ROW(C2:H6))&"."&if(row(C2:H6),COLUMN(C2:H6))*(C2:H6<>"")&"|"),"|")),"order by Col1 desc skipping "&COLUMNS(if(COLUMN(C2:H6),ROW(C2:H6)))))))*10 -1)),TRANSPOSE(ArrayFormula(COLUMN(C1:H1)^0)))>MMULT(ArrayFormula(C2:H6*--(if(row(C2:H6),COLUMN(C2:H6))=(sort(QUERY(TRANSPOSE(SPLIT(CONCATENATE(if(COLUMN(C2:H6),ROW(C2:H6))&"."&if(row(C2:H6),COLUMN(C2:H6))*(C2:H6<>"")&"|"),"|")),"order by Col1 desc skipping "&COLUMNS(C1:H1)))-INT(sort(QUERY(TRANSPOSE(SPLIT(CONCATENATE(if(COLUMN(C2:H6),ROW(C2:H6))&"."&if(row(C2:H6),COLUMN(C2:H6))*(C2:H6<>"")&"|"),"|")),"order by Col1 desc skipping "&COLUMNS(if(COLUMN(C2:H6),ROW(C2:H6)))))))*10)),TRANSPOSE(ArrayFormula(COLUMN(C1:H1)^0))),"Negative Attendance Trend",""))

它被放置在 our example 中的单元格 B2 中。

更新1

以上公式仅适用于少量列 (<10)。所以我修复了它并得到了最终的巨大 arrayFormula:

=ArrayFormula(if(MMULT(ArrayFormula(C2:AO4*--(if(row(C2:AO4),COLUMN(C2:AO4))=ROUNDUP((sort(QUERY(TRANSPOSE(SPLIT(CONCATENATE(if(COLUMN(C2:AO4),ROW(C2:AO4))&"."&text(if(row(C2:AO4),COLUMN(C2:AO4))*(C2:AO4<>""),"000")&"|"),"|")),"order by Col1 desc skipping "&COLUMNS(C2:AO4)))-INT(sort(QUERY(TRANSPOSE(SPLIT(CONCATENATE(if(COLUMN(C2:AO4),ROW(C2:AO4))&"."&text(if(row(C2:AO4),COLUMN(C2:AO4))*(C2:AO4<>""),"000")&"|"),"|")),"order by Col1 desc skipping "&COLUMNS(if(COLUMN(C2:AO4),ROW(C2:AO4)))))))*1000 -1))),TRANSPOSE(ArrayFormula(COLUMN(C2:AO4)^0)))>MMULT(ArrayFormula(C2:AO4*--(if(row(C2:AO4),COLUMN(C2:AO4))=ROUNDUP((sort(QUERY(TRANSPOSE(SPLIT(CONCATENATE(if(COLUMN(C2:AO4),ROW(C2:AO4))&"."&text(if(row(C2:AO4),COLUMN(C2:AO4))*(C2:AO4<>""),"000")&"|"),"|")),"order by Col1 desc skipping "&COLUMNS(C2:AO4)))-INT(sort(QUERY(TRANSPOSE(SPLIT(CONCATENATE(if(COLUMN(C2:AO4),ROW(C2:AO4))&"."&text(if(row(C2:AO4),COLUMN(C2:AO4))*(C2:AO4<>""),"000")&"|"),"|")),"order by Col1 desc skipping "&COLUMNS(if(COLUMN(C2:AO4),ROW(C2:AO4)))))))*1000))),TRANSPOSE(ArrayFormula(COLUMN(C2:AO4)^0))),"Negative Attendance Trend",""))

Link to final workbook