Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Num_format overrides formula in add_table #741

Closed
mklwong opened this issue Aug 10, 2020 · 2 comments
Closed

Num_format overrides formula in add_table #741

mklwong opened this issue Aug 10, 2020 · 2 comments
Assignees
Labels
under investigation The issue is being investigated.

Comments

@mklwong
Copy link

mklwong commented Aug 10, 2020

Hi,

I am using XlsxWriter to create a spreadsheet where a column has a formula definition and currency formatting but the formula appears to be overridden (table 1). When the formatting is removed, the formula reappears (table 2)

Interestingly, if a default value is added in one row, the formula persists for that row and excel shows an "Inconsistent Calculated Column Formula" error for that cell (table 3).

I am using Python version 3.7.6 and XlsxWriter 1.3.2 and Excel version 260 (Version 2002 Build 12527.20880).

Here is some code that demonstrates the problem:

import xlsxwriter

workbook = xlsxwriter.Workbook(r'E:/test.xlsx')
worksheet = workbook.add_worksheet()
currency = workbook.add_format({'num_format':'_-$* #,##0.00_-;-$* #,##0.00_-;_-$* "-"??_-;_-@_-'})
# Table 1
worksheet.add_table('A1:C3', {'data': [[1,2,None], [1,2,None]],
                              'columns': [{'header': 'A'},
                                          {'header': 'B'},
                                          {'header': 'C', 'formula':'[@A]*[@B]', 'format':currency}
                                          ]})
# Table 2
worksheet.add_table('A5:C7', {'data': [[1,2,None], [1,2,5]],
                              'columns': [{'header': 'A'},
                                          {'header': 'B'},
                                          {'header': 'C', 'formula':'[@A]*[@B]'}
                                          ]})
# Table 1
worksheet.add_table('A9:C11', {'data': [[1,2,None], [1,2,5]],
                              'columns': [{'header': 'A'},
                                          {'header': 'B'},
                                          {'header': 'C', 'formula':'[@A]*[@B]', 'format':currency}
                                          ]})
workbook.close()
@jmcnamara jmcnamara self-assigned this Aug 10, 2020
@jmcnamara jmcnamara added the under investigation The issue is being investigated. label Aug 10, 2020
@jmcnamara
Copy link
Owner

I think the issues is that you are overriding the values in Column C with None. If you leave that out the program should work as expected:

import xlsxwriter

workbook = xlsxwriter.Workbook(r'test.xlsx')
worksheet = workbook.add_worksheet()
currency = workbook.add_format({'num_format': '_-$* #,##0.00_-;-$* #,##0.00_-;_-$* "-"??_-;_-@_-'})

# Table 1
worksheet.add_table('A1:C3', {'data': [[1,2], [1,2]],
                              'columns': [{'header': 'A'},
                                          {'header': 'B'},
                                          {'header': 'C', 'formula': '[@A]*[@B]', 'format': currency}]})
workbook.close()

Output:

screenshot

@mklwong
Copy link
Author

mklwong commented Aug 10, 2020

Ah seems like "data" doesn't have to have the same shape as the table. This also worked.

worksheet.add_table('A9:C11', {'data': [[1,2], [1,2,5]],
                              'columns': [{'header': 'A'},
                                          {'header': 'B'},
                                          {'header': 'C', 'formula':'[@A]*[@B]', 'format':currency}
                                          ]})

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
under investigation The issue is being investigated.
Projects
None yet
Development

No branches or pull requests

2 participants