在while循环中使用xlsxwriter将多个公式写入excel工作表

问题描述 投票:1回答:1

我正在尝试使用xlsxwriter在工作表的多个单元格中填充公式。该公式应如下所示:=IF(E2=MIN($E$2:$E$545),E2,""),行值存储在脚本中的变量中,分别为formularowcountrowcount

我收到此异常:AttributeError: 'NoneType' object has no attribute 'group'

代码:

while formularowcount < rowcount:
profiledatasheet.write_formula("'F"+str(formularowcount)+"','=IF(E"+str(formularowcount)+"=MIN($E$2:$E$"+str(rowcount)+"),E"+str(formularowcount)+",\"""\")'")

我已经尝试存储字符串本身并传递它,但是得到相同的异常:

formulavalue = "'F"+str(formularowcount)+"','=IF(E"+str(formularowcount)+"=MIN($E$2:$E$"+str(rowcount)+"),E"+str(formularowcount)+",\"""\")'"
        profiledatasheet.write_formula(formulavalue)

但是,如果我在调试中打印出字符串并将输出粘贴到调试控制台,它将起作用并返回0:

print(formulavalue)
'F2','=IF(E2=MIN($E$2:$E$545),E2,"")'

profiledatasheet.write_formula('F2','=IF(E2=MIN($E$2:$E$545),E2,"")')
0

关于我的语法有什么问题的任何想法?

python xlsxwriter
1个回答
0
投票

[试试看(如果您使用Python3)] >>

profiledatasheet.write_formula(
    f'F{formularowcount}',
    f'=IF(E{formularowcount}=MIN($E$2:$E${rowcount}),E{formularowcount},"")'
)
    
© www.soinside.com 2019 - 2024. All rights reserved.