Skip to content

Commit 47eaf27

Browse files
committed
table: fix issue where column formulas were overwritten by table data
Issue #1015
1 parent bb21ddf commit 47eaf27

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
###############################################################################
2+
#
3+
# Tests for XlsxWriter.
4+
#
5+
# SPDX-License-Identifier: BSD-2-Clause
6+
# Copyright (c), 2013-2023, John McNamara, [email protected]
7+
#
8+
9+
from ..excel_comparison_test import ExcelComparisonTest
10+
from ...workbook import Workbook
11+
12+
13+
class TestCompareXLSXFiles(ExcelComparisonTest):
14+
"""
15+
Test file created by XlsxWriter against a file created by Excel.
16+
17+
"""
18+
19+
def setUp(self):
20+
self.set_filename("table34.xlsx")
21+
22+
self.ignore_files = [
23+
"xl/calcChain.xml",
24+
"[Content_Types].xml",
25+
"xl/_rels/workbook.xml.rels",
26+
]
27+
28+
def test_create_file(self):
29+
"""Test the creation of a simple XlsxWriter file with tables."""
30+
31+
workbook = Workbook(self.got_filename)
32+
worksheet = workbook.add_worksheet()
33+
format1 = workbook.add_format({"num_format": "0.0000"})
34+
35+
data = [
36+
["Foo", 1234, 0, 4321],
37+
["Bar", 1256, 0, 4320],
38+
["Baz", 2234, 0, 4332],
39+
["Bop", 1324, 0, 4333],
40+
]
41+
42+
worksheet.set_column("C:F", 10.288)
43+
44+
worksheet.add_table(
45+
"C2:F6",
46+
{
47+
"data": data,
48+
"columns": [
49+
{},
50+
{},
51+
{},
52+
{"formula": "Table1[[#This Row],[Column3]]", "format": format1},
53+
],
54+
},
55+
)
56+
57+
workbook.close()
58+
59+
self.assertExcelEqual()
Binary file not shown.

xlsxwriter/worksheet.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -3388,9 +3388,7 @@ def add_table(self, first_row, first_col, last_row, last_col, options=None):
33883388
formula = formula.replace("@", "[#This Row],")
33893389

33903390
col_data["formula"] = formula
3391-
3392-
for row in range(first_data_row, last_data_row + 1):
3393-
self._write_formula(row, col_num, formula, xformat)
3391+
# We write the formulas below after the table data.
33943392

33953393
# Handle the function for the total row.
33963394
if user_data.get("total_function"):
@@ -3475,6 +3473,17 @@ def add_table(self, first_row, first_col, last_row, last_col, options=None):
34753473
j += 1
34763474
i += 1
34773475

3476+
# Write any columns formulas after the user supplied table data to
3477+
# overwrite it if required.
3478+
for col_id, col_num in enumerate(range(first_col, last_col + 1)):
3479+
column_data = table["columns"][col_id]
3480+
if column_data and column_data["formula"]:
3481+
formula_format = col_formats.get(col_id)
3482+
formula = column_data["formula"]
3483+
3484+
for row in range(first_data_row, last_data_row + 1):
3485+
self._write_formula(row, col_num, formula, formula_format)
3486+
34783487
# Store the table data.
34793488
self.tables.append(table)
34803489

0 commit comments

Comments
 (0)