xlsxwriter 和 LibreOffice 不显示公式的结果
xlsxwriter and LibreOffice not showing formula's result
我正在尝试用一个简单的公式创建一个 Excel 文件:
import xlsxwriter
workbook = xlsxwriter.Workbook('testxlsx.xlsx', {'strings_to_numbers': True})
ws = workbook.add_worksheet()
ws.write('A2', 'Number one')
ws.write('B2', '1')
ws.write('A3', 'Number two')
ws.write('B3', "1000")
ws.write('A4', "Number three")
ws.write('B4', "1050")
ws.write('A5', "Number four")
ws.write('B5', "3")
ws.write('A6', "Result")
ws.write('B6', '=IF(B5=3,ROUND(100-(B3/B4*100),1),ROUND(100-(B3/(B4*1.502)*100),1))')
workbook.close()
生成的文件在 Excel 中完美运行,但在 LibreOffice Calc 中打开时,公式未计算。我需要重新输入数值,然后它才起作用。
我做错了什么?
来自 xlsxwriter docs:
XlsxWriter doesn’t calculate the result of a formula and instead stores the value 0 as the formula result. It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened. This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications. However, applications that don’t have a facility to calculate formulas, such as Excel Viewer, or some mobile applications will only display the 0 results.
至于为什么没有自动重新计算,来自 ask.libreoffice.org answer:
LibreOffice intentionally does not recalculate older spreadsheets, because as formulas are updated from version to version or between different spreadsheet programs, the results can be different. Go to Tools – Options – LibreOffice Calc, under 'Recalculation on file load', change the two drop-downs, 'Excel 2007 and newer' and 'ODF Spreadsheet (not saved by LibreOffice)', to 'Always recalculate'. Click Ok, close the spreadsheet and LibreOffice. Now open the file in LibreOffice and you should see that the formulas have recalculated.
Also go to Tools – Cell Contents and be sure that AutoCalculate is
selected.
我已经确认设置“始终重新计算”或“提示”对我有用。或者,您可以随时按 control-shift-F9。
来自FAQ of the xlsxwriter website:
Note: LibreOffice doesn’t recalculate Excel formulas that reference other cells by default, in which case you will get the default XlsxWriter value of 0. You can work around this by setting the “LibreOffice Preferences -> LibreOffice Calc -> Formula -> Recalculation on File Load” option to “Always recalculate” (see the LibreOffice documentation). Or, you can set a blank result in the formula, which will also force recalculation:
worksheet.write_formula('A1', '=Sheet1!$A', None, '')
我刚刚测试了一下,确实有效。
我正在尝试用一个简单的公式创建一个 Excel 文件:
import xlsxwriter
workbook = xlsxwriter.Workbook('testxlsx.xlsx', {'strings_to_numbers': True})
ws = workbook.add_worksheet()
ws.write('A2', 'Number one')
ws.write('B2', '1')
ws.write('A3', 'Number two')
ws.write('B3', "1000")
ws.write('A4', "Number three")
ws.write('B4', "1050")
ws.write('A5', "Number four")
ws.write('B5', "3")
ws.write('A6', "Result")
ws.write('B6', '=IF(B5=3,ROUND(100-(B3/B4*100),1),ROUND(100-(B3/(B4*1.502)*100),1))')
workbook.close()
生成的文件在 Excel 中完美运行,但在 LibreOffice Calc 中打开时,公式未计算。我需要重新输入数值,然后它才起作用。
我做错了什么?
来自 xlsxwriter docs:
XlsxWriter doesn’t calculate the result of a formula and instead stores the value 0 as the formula result. It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened. This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications. However, applications that don’t have a facility to calculate formulas, such as Excel Viewer, or some mobile applications will only display the 0 results.
至于为什么没有自动重新计算,来自 ask.libreoffice.org answer:
LibreOffice intentionally does not recalculate older spreadsheets, because as formulas are updated from version to version or between different spreadsheet programs, the results can be different. Go to Tools – Options – LibreOffice Calc, under 'Recalculation on file load', change the two drop-downs, 'Excel 2007 and newer' and 'ODF Spreadsheet (not saved by LibreOffice)', to 'Always recalculate'. Click Ok, close the spreadsheet and LibreOffice. Now open the file in LibreOffice and you should see that the formulas have recalculated.
Also go to Tools – Cell Contents and be sure that AutoCalculate is selected.
我已经确认设置“始终重新计算”或“提示”对我有用。或者,您可以随时按 control-shift-F9。
来自FAQ of the xlsxwriter website:
Note: LibreOffice doesn’t recalculate Excel formulas that reference other cells by default, in which case you will get the default XlsxWriter value of 0. You can work around this by setting the “LibreOffice Preferences -> LibreOffice Calc -> Formula -> Recalculation on File Load” option to “Always recalculate” (see the LibreOffice documentation). Or, you can set a blank result in the formula, which will also force recalculation:
worksheet.write_formula('A1', '=Sheet1!$A', None, '')
我刚刚测试了一下,确实有效。