XLSXWriter Apply Formula Across Column With Dynamic Cell Reference

こ雲淡風輕ζ 提交于 2019-12-24 06:38:30

问题


Here's an example from the XSLX Writer documentation:

worksheet.write_formula('A1', '=10*B1 + C1')

Looks simple enough. But what if I want to apply this formula for 'A1:A30'? Also, what if in cell A3, the formula should dynamically update to:

'=10*B3 + C3'

?

I've been unable to do this from looking at the docs. I see a promising option for write_array_formula, but I don't want an array formula.

I've come up with a solution involving looping through each row and column, but I'm hoping there's a simpler one.


回答1:


I've come up with a solution involving looping through each row and column, but I'm hoping there's a simpler one.

That is the only way to do it. Something like this:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()

for row_num in range(1, 21):
    worksheet.write_formula(row_num - 1, 0,
                            '=10*$B%d + $C%d' % (row_num, row_num))

    # Write some data for the formula.
    worksheet.write(row_num - 1, 1, row_num)
    worksheet.write(row_num - 1, 2, row_num)

workbook.close()


来源:https://stackoverflow.com/questions/39944961/xlsxwriter-apply-formula-across-column-with-dynamic-cell-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!