簡體   English   中英

在while循環中使用xlsxwriter將多個公式寫入excel工作表

[英]Write multiple formulas to excel sheet with xlsxwriter in while loop

我正在嘗試使用帶有 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)

但是,如果我在調試中打印字符串並將 output 粘貼到調試控制台中,它會起作用並返回 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

關於我的語法有什么問題的任何想法?

試試這個(如果你使用 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)

)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM