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

2024-10-01 07:16:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Python 2.7中的xlsxwriter在一个工作表的多个单元格中填充公式。我需要将公式作为包含单引号和双引号的字符串传入,如下所示: 'F2','=IF(E2=MIN($E$2:$E$545),E2,"")',其中行值存储在脚本中的变量中为formularowcountrowcount

我得到了这个异常:AttributeError: 'NoneType' object has no attribute 'group'

守则:

while rowcount > formularowcount:
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)

但是,如果我在debug中打印字符串并将输出粘贴到debug控制台,它将工作并返回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

我的语法有什么问题吗


Tags: 字符串debugifminwrite公式f2formula
1条回答
网友
1楼 · 发布于 2024-10-01 07:16:47

试试这个(如果你使用3)

profiledatasheet.write_formula(
    f'F{formularowcount}',
    f'=IF(E{formularowcount}=MIN($E$2:$E${rowcount}),E{formularowcount},"")'
)

对于Python2,类似于

profiledatasheet.write_formula(
'F{0}'.format(formularowcount),
'=IF(E{0}=MIN($E$2:$E${1}),E{0},"")'.format(formularowcount, rowcount)

)

相关问题 更多 >