问题
I'll be honest and say that I asked a similar question the other day, but I think I over complicated the question and so it wasn't particularly clear on what I a looking for.
To sum up my previous question, whilst using xlsxwriter I am looking for a way of storing/assigning the result of a formula as a numerical value so that I can reference it later on in the script and so avoiding a recalculation. I should mention that I am producing two separate Excel files in the process, so want to avoid the dreaded #REF! error on the second Excel workbook
Apologies for the crude code, but something like below is kind of what I am looking for, but 'result' would have been assigned a number like '12.56' for example, so when I write to a second Excel workbook I avoid the recalculation and simply input the assigned value of 'result'
# on first EXCEL workbook calculate result and store as number
cell_range = xl_range(0, col+3, tot, col+3)
formula = '=AVERAGE(DATA!%s)' % cell_range
worksheetFILTER_ON.write('O2', formula ,bold)
result = formula
# on second EXCEL workbook print numerical result
worksheetMAIN.write('A1', result ,bold)
I hope this makes a bit more sense than my last attempt.
Thanks, MikG
回答1:
The update is a little bit clearer but the question is still missing the part that would help someone answer this: a small working example.
One thing to note is that XlsxWriter doesn't calculated the result of a formula. The result
variable in the code above is just a reference to formula
variable which is a string like '=AVERAGE(DATA!A1:D4)'
.
If you are using that formula in a worksheet that doesn't have a worksheet named DATA
then you will probably get the #REf
error that you were referring to.
来源:https://stackoverflow.com/questions/25114594/python-with-xlsxwriter-to-save-or-assign-formula-result-as-a-value-for-use-later