write_formula 给出错误,除非我复制并粘贴完全相同的公式

write_formula gives error unless i copy and paste exactly the same formula

我有一个 python 脚本,它使用 xlsxwriter 最后写入 excel 文件。一切正常,但公式在启动时出错,如果我复制并粘贴完全相同的公式,它会给出预期的结果。 这是行:

worksheet.write_formula('I2', '=SUMIF(B2:B{0};1;F2:F{0})'.format(len(df.index)+1))

编辑:我尝试导出为 xml,我看到 xlsxwriter 写道;作为|。我的意思是 xlsxwriter 给出的错误公式是:

<Cell ss:Formula="of:=SUMIF([.B2:.B11]|1|[.F2:.F11])">
<Data ss:Type="String">Err:508</Data>

复制粘贴的工作公式为:

<Cell ss:Formula="of:=SUMIF([.B2:.B11];1;[.F2:.F11])">
<Data ss:Type="Number">485</Data>

我不知道这里有什么问题。谢谢

我需要将 LibreOffice 设置 > LibreOffice Calc > 公式更改为始终在文件加载时重新计算。但更换;作为 |仍然存在

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.

转到给定的 link 我相信你会找到你的答案:XlsxWriter: Working with Formulas

特别是非美国 Excel 函数和语法说:

Excel 以美国英语版本的格式存储公式,而不管 Excel 的最终用户版本的语言或区域设置如何。因此,公式必须使用美式 separator/range 运算符编写,即逗号(而非分号)。有多个值的公式应该写成:

worksheet.write_formula('A1', '=SUM(1, 2, 3)')   # OK 

worksheet.write_formula('A2', '=SUM(1; 2; 3)')   # Semi-colon. Error on load.

希望对您有所帮助。