- 题设:Excel表格中,计算如下图所示不同颜色(蓝、黄、桔)单元格值:各颜色填涂的单元格值的总和
- 条件:
- Excle表格中
- 分色标记,单元格有值
- 开始列(当前为D),开始行(当前为6)随机设置
- 下三角直角边长(当前为10个单元格)随机设置,与桔色单行长度相同(当前为10个单元格)
- 计算:1)蓝色单元格中值总和、蓝色单元格中值总和、蓝色单元格中值总和;2)各色单元格的数量
1. 方法1:Excel选中对应颜色的单元格,自动生成格子数和单元格值总和
2. 方法2:Excel格式查找(前题,先标颜色)
蓝:660 ;黄:110 ;桔:660
3. 方法3:宏(VBA编程,没做,也许找时间试)
4. 方法4:Numpy(构建列表,然后分别计算,没做,也许找时间试)
5. 方法5:python数格子(也许是最笨的方法),生成Excel的sum函数,计数、求和 (忽略代码冗余)
###################################
# 2023.12
# Sum as request
###################################
# 构建列组合
la = ['','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
lb = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
sc = str.upper('D') # 开始列,自动转成大写
##ec = 'AI' # 结束列
sr = 6 # 开始行
er = 37 # 结束行
bc = 10 # 下三角黄色边长
lw = 26 # English字母表长度
li = 0 # 开始列数值
if len(sc) == 1:
li = lb.index(sc)
else:
li = la.index(sc[0]) * lw + lb.index(sc[1])
lai = li//26
lbi = li%26
###################### up,上三角蓝色
##sum_up = 0
up_str = '=sum(' # excle sum function string
up_cn = 0
sr_up = sr
er_up = er - bc # 上三角下边界
sc_up = sc
ec_up = li + (er - bc - sr)
tlai = la[ec_up//26]
tlbi = lb[ec_up%26]
tc = er - bc - sr + 1 # 上三角边长
for r in range(tc):
ti = li + tc - 1 - r
tlai = la[ti//26]
tlbi = lb[ti%26]
up_str = up_str + sc + str(sr_up + r) + ':' + tlai + tlbi + str(sr_up + r) + ','
up_cn = up_cn + tc - r
up_str = up_str + ")"
print("Up: ", up_str)
print("Up Count: ", up_cn)
###################### mid,中间四边形桔色
##sum_mid = 0
mid_str = '=sum(' # excle sum function string
mid_cn = 0
sr_mid = sr
er_mid = er - bc
sc_mid = li + 1
ec_mid = li + (er - bc - sr) + bc
tc = er - bc - sr + 1 # 桔色高度
for r in range(tc):
tsc = sc_mid + r # 开始列
tec = tsc + bc - 1 # 结束列
tr = er_mid - r # 行号
mid_str = mid_str + la[tsc//lw] + lb[tsc%lw] + str(tr) + ':' + la[tec//lw] + lb[tec%lw] + str(tr) + ','
mid_cn = mid_cn + bc
mid_str = mid_str + ")"
print("Mid: ", mid_str)
print("Mid Count: ", mid_cn)
###################### down,下三角黄色
##sum_down = 0
down_str = '=sum(' # excle sum function string
down_cn = 0 # down cell count
sr_down = er - bc + 1 # 下三角开始行
##sc_down = sc # 下三角开始列
##ec_down = li + bc - 1 # 下三角结束列
tlai = lai
tlbi = lbi
for r in range(bc):
ti = li + bc - 1 - r
tlai = la[ti//26]
tlbi = lb[ti%26]
down_str = down_str + sc + str(sr_down + r) + ':' + tlai + tlbi + str(sr_down + r) + ','
down_cn = down_cn + bc - r
down_str = down_str + ")"
print("Down: ", down_str)
print("Down Count: ", down_cn)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Up: =sum(D6:Y6,D7:X7,D8:W8,D9:V9,D10:U10,D11:T11,D12:S12,D13:R13,D14:Q14,D15:P15,D16:O16,D17:N17,D18:M18,D19:L19,D20:K20,D21:J21,D22:I22,D23:H23,D24:G24,D25:F25,D26:E26,D27:D27,)
Up Count: 253
Mid: =sum(E27:N27,F26:O26,G25:P25,H24:Q24,I23:R23,J22:S22,K21:T21,L20:U20,M19:V19,N18:W18,O17:X17,P16:Y16,Q15:Z15,R14:AA14,S13:AB13,T12:AC12,U11:AD11,V10:AE10,W9:AF9,X8:AG8,Y7:AH7,Z6:AI6,)
Mid Count: 220
Down: =sum(D28:M28,D29:L29,D30:K30,D31:J31,D32:I32,D33:H33,D34:G34,D35:F35,D36:E36,D37:D37,)
Down Count: 55