Skip to content

Commit 878affa

Browse files
committed
table: fix escaping of future funtions
Issue #1015
1 parent 832f7db commit 878affa

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
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("table35.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": "BASE(0,2)", "format": format1},
53+
],
54+
},
55+
)
56+
57+
workbook.close()
58+
59+
self.assertExcelEqual()
Binary file not shown.

xlsxwriter/worksheet.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ def write_dynamic_array_formula(
836836

837837
# Utility method to strip equal sign and array braces from a formula and
838838
# also expand out future and dynamic array formulas.
839-
def _prepare_formula(self, formula):
839+
def _prepare_formula(self, formula, expand_future_functions=False):
840840
# Remove array formula braces and the leading =.
841841
if formula.startswith("{"):
842842
formula = formula[1:]
@@ -880,7 +880,7 @@ def _prepare_formula(self, formula):
880880
formula = re.sub(r"\bWRAPROWS\(", "_xlfn.WRAPROWS(", formula)
881881
formula = re.sub(r"\bXLOOKUP\(", "_xlfn.XLOOKUP(", formula)
882882

883-
if not self.use_future_functions:
883+
if not self.use_future_functions and not expand_future_functions:
884884
return formula
885885

886886
formula = re.sub(r"\bACOTH\(", "_xlfn.ACOTH(", formula)
@@ -3403,6 +3403,9 @@ def add_table(self, first_row, first_col, last_row, last_col, options=None):
34033403
# Convert Excel 2010 "@" ref to 2007 "#This Row".
34043404
formula = formula.replace("@", "[#This Row],")
34053405

3406+
# Escape any future functions.
3407+
formula = self._prepare_formula(formula, True)
3408+
34063409
col_data["formula"] = formula
34073410
# We write the formulas below after the table data.
34083411

0 commit comments

Comments
 (0)