I’m trying to create an Excel containing some formulas using the code:
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 1)
worksheet.write('A2', 2)
worksheet.write('A3', 3)
worksheet.write('B1', '+')
worksheet.write('B2', '-')
worksheet.write('B3', '+')
formula = '=((SUMIF(B{f}:B{la},"+",{cl}{f}:{cl}{la})-'\
+ 'SUMIF(B{f}:B{la},"-",{cl}{f}:{cl}{la}))'\
+ '/MAX(SUMIF(B{f}:B{la},"/",{cl}{f}:{cl}{la}),1)'
print(formula.format(f=1, la=3, cl='A'))
# =((SUMIF(B1:B3,"+",A1:A3)-SUMIF(B1:B3,"-",A1:A3))/MAX(SUMIF(B1:B3,"/",A1:A3),1)
worksheet.write_formula('B5', formula.format(f=1, la=3, cl='A'))
workbook.close()
When opening this file using Microsoft Excel we see:
The cell containing 0
is the one with the formula.
When I use LibreOffice to open the same file I see the correct value.
You used two parenthesis, there should be only one in the start of your formula. it should be:
formula = '=(SUMIF(B{f}:B{la},"+",{cl}{f}:{cl}{la})-'\
+ 'SUMIF(B{f}:B{la},"-",{cl}{f}:{cl}{la}))'\
+ '/MAX(SUMIF(B{f}:B{la},"/",{cl}{f}:{cl}{la}),1)'
Tags: excel, pythonpython