【问题标题】:Write multiple formulas to excel sheet with xlsxwriter in while loop在while循环中使用xlsxwriter编写多个公式到excel表
【发布时间】:2020-06-17 02:51:59
【问题描述】:

我正在尝试使用xlsxwriter 和 Python 2.7 在工作表的多个单元格中填充公式。我需要将公式作为包含单引号和双引号的字符串传递,如下所示: '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)

但是,如果我在调试中打印字符串并将输出粘贴到调试控制台中,它会起作用并返回 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】:

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

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

    对于 python 2 类似

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

    )

    【讨论】:

    • 不幸的是,我使用的是 python 2.7
    猜你喜欢
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    相关资源
    最近更新 更多