Google Sheets中用ArrayFormula计算比赛场次与得分问题
比赛得分统计:单行公式生成可排序列表方案
现有比赛数据
| ---- | Hermione | Harry | Ron | Neville |
|---|---|---|---|---|
| Hermione | - | Harry | Hermione | Hermione |
| Harry | Harry | - | Harry | Harry |
| Ron | Hermione | Harry | - | |
| Neville | Hermione | Harry | - |
问题分析
- ArrayFormula引用错误:原单单元格公式
=IF(B2=$A2,3,IF(B2=B$1,1,0))扩展为数组公式时,$A2和B$1的混合引用未随数组逐行/列动态适配,导致平局得分(IF(B2=B$1,1))未被正确计算。 - 逐行统计逻辑失效:
SUMIF/QUERY直接遍历全局范围无法实现按玩家维度的精准场次与得分聚合,临时方案的COUNTIF除以2的逻辑仅适用于无平局场景,不具备通用性。
单行公式解决方案
假设比赛数据位于A1:E5区域,使用以下单行公式即可生成包含玩家名称、场次、得分的排序结果:
={"Player","Played","Points";SORT(QUERY(FLATTEN( {A2:A5&B2:E5,B2:E5,A2:A5, IF((B2:E5="-")+(B2:E5=""),,IF(B2:E5=A2:A5,3,IF(B2:E5=B1:E1,1,0))) }), "select Col2, count(Col1), sum(Col4) where Col2 is not null and Col3 is not null group by Col2 label count(Col1)'Played', sum(Col4)'Points'"),3,0)}
公式核心逻辑
- 数据扁平化:
FLATTEN将二维比赛表转为一维记录,每条记录包含「比赛配对标识」「结果归属玩家」「对手玩家」「单场得分」四个字段。 - 得分规则映射:
- 跳过单元格为
-或空的无效记录 - 结果归属行玩家(
B2:E5=A2:A5):胜,得3分 - 结果归属列玩家(
B2:E5=B1:E1):平,得1分 - 其他情况:负,得0分
- 跳过单元格为
- 聚合与排序:
QUERY按玩家分组统计总场次和得分,SORT按得分降序排列,直接输出最终统计表格。
结果验证
原数据统计结果
| Player | Played | Points |
|---|---|---|
| Harry | 3 | 9 |
| Hermione | 3 | 6 |
| Ron | 2 | 0 |
| Neville | 2 | 0 |
平局场景验证(Ron与Neville平局)
将Ron行的Neville列(D3)设为Neville,Neville行的Ron列(C4)设为Ron,公式自动生成:
| Player | Played | Points |
|---|---|---|
| Harry | 3 | 9 |
| Hermione | 3 | 6 |
| Ron | 3 | 1 |
| Neville | 3 | 1 |
内容的提问来源于stack exchange,提问作者Karl Richey
相关产品推荐
相关产品推荐

