自动更新Google表格中的公式,以便计算每周更新值

时间:2016-03-10 17:49:25

标签: google-sheets spreadsheet

我有以下一行表示出席团队会议:

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            

约翰出席了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

John在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表格数组,因为每周都会填充周列,因此无法继续进行自动重新计算。

1 个答案:

答案 0 :(得分:1)

<强> UPDATE2

<强>脚本

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

转到工具&gt;脚本编辑器,并在此处粘贴此代码:

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. 最后找到右侧(列)的单元格,其中值&lt;&gt; &#39;&#39;
  2. 上一个左侧单元格
  3. 您需要完成的只是将此公式粘贴到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", "")
    

    结果是:

    enter image description here

    在单元格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

    <强> UPDATE1

    上述公式仅适用于少量列(&lt; 10)。所以我修复了它并获得了最终的巨大数组格式:

    =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