问题
I'm successfully creating many Excel workbooks using XLSXWriter. Now I'm trying to place one of Excel's new (as of 2019) FILTER functions into a cell:
=FILTER(A19:B90,B19:B90=E19)
When I open the workbook, Excel gives me this error dialog:
The workbook opens, but a "0" is in the cell instead of the FILTER function.
But if I paste the exact same filter function into the same cell manually, it works!
All the other formulas I'm creating work as expected, and if I use XLSXWriter to place a generic function into the same cell where I want the filter to go, e.g. =100 *5, it also works.
Does XLSXWriter have a bug when it comes to using =FILTER() functions?
回答1:
This is a little bit of an odd one. From the XlsxWriter docs on Formulas added in Excel 2010 and later:
Excel 2010 and later added functions which weren’t defined in the original file specification. These functions are referred to by Microsoft as future functions. Examples of these functions are
ACOT,CHISQ.DIST.RT,CONFIDENCE.NORM,STDEV.P,STDEV.SandWORKDAY.INTL.When written using
write_formula()these functions need to be fully qualified with a_xlfn.(or other) prefix as they are shown the list below. For example:worksheet.write_formula('A1', '=_xlfn.STDEV.S(B1:B10)')They will appear without the prefix in Excel
However, this formula has a _xlfn._xlws. prefix and is also an array formula so you would have to do this:
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write_array_formula('C1:D72',
'=_xlfn._xlws.FILTER(A19:B90,B19:B90=E19)')
workbook.close()
Output:
One difference between the XlsxWriter output and Excel's is that this shows the array formula as {FILTER(...)}, with braces that are typical of array formulas, but Excel doesn't. However, I think the formula works as intended. You can try it in a more complex example to verify.
来源:https://stackoverflow.com/questions/60838976/xlsxwriter-and-excel-filter-function