使用F-string给列赋值时出错

0 投票
1 回答
69 浏览
提问于 2025-04-14 16:39

我需要在原始数据表中用公式替换几个列(从这个表中透视出来)。
我使用了Xlwings这个工具来完成这个任务,对于其他公式都成功了,但这个特定的公式却不行。

这些公式可以正常工作;

#column Y: Year 1-FA   #Works perfectly
sheet_obj.range("Y2:Y"+str(max_row)).formula = f'=IF(NUMBERVALUE(C2)=1,CONCAT("Y1-",L2),L2)'
    
#column z: custom    #Works perfectly
sheet_obj.range("Z2:Z"+str(max_row)).formula = f'=CONCAT(B2,"-",E2,"-",I2,"-",J2,"-",K2)'

而下面这个公式会返回错误;

#column P: New Out of PP   #this is whwre the it shows error
    sheet_obj.range("P2:P"+str(max_row)).formula = f'''=IFERROR(IF(OR(O2<>"",O2<>"NULL"),IF(OR(J2="Term 1",J2="Term 33"),O2+MIN(XLOOKUP(CONCAT(N2,C2,J2,H2),Subject_Map!$G:$G,Subject_Map!$H:$H,"",0),Raw(Merged)!S2),O2),O2),O2)'''

我想要使用的公式是

'=IFERROR(IF(OR(O2<>"",O2<>"NULL"),IF(OR(J2="Term 1",J2="Term 33"),O2+MIN(XLOOKUP(CONCAT(N2,C2,J2,H2),Subject_Map!$G:$G,Subject_Map!$H:$H,"",0),Raw(Merged)!S2),O2),O2),O2)'

1 个回答

1

我觉得问题出在工作表的名字 Raw(Merged) 上,你需要在它周围加上单引号,像这样 'Raw(Merged)'!S2

sheet_obj.range("P2:P"+str(max_row)).formula = '''=IFERROR(IF(OR(O2<>"",O2<>"NULL"),IF(OR(J2="Term 1",J2="Term 33"),O2+MIN(XLOOKUP(CONCAT(N2,C2,J2,H2),Subject_Map!$G:$G,Subject_Map!$H:$H,"",0),'Raw(Merged)'!S2),O2),O2),O2)'''

撰写回答