Skip to content

Commit

Permalink
table: fix escaping of future funtions
Browse files Browse the repository at this point in the history
Issue #1015
  • Loading branch information
jmcnamara committed Oct 9, 2023
1 parent 832f7db commit 878affa
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
59 changes: 59 additions & 0 deletions xlsxwriter/test/comparison/test_table35.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c), 2013-2023, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparison_test import ExcelComparisonTest
from ...workbook import Workbook


class TestCompareXLSXFiles(ExcelComparisonTest):
"""
Test file created by XlsxWriter against a file created by Excel.
"""

def setUp(self):
self.set_filename("table35.xlsx")

self.ignore_files = [
"xl/calcChain.xml",
"[Content_Types].xml",
"xl/_rels/workbook.xml.rels",
]

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file with tables."""

workbook = Workbook(self.got_filename)
worksheet = workbook.add_worksheet()
format1 = workbook.add_format({"num_format": "0.0000"})

data = [
["Foo", 1234, 0, 4321],
["Bar", 1256, 0, 4320],
["Baz", 2234, 0, 4332],
["Bop", 1324, 0, 4333],
]

worksheet.set_column("C:F", 10.288)

worksheet.add_table(
"C2:F6",
{
"data": data,
"columns": [
{},
{},
{},
{"formula": "BASE(0,2)", "format": format1},
],
},
)

workbook.close()

self.assertExcelEqual()
Binary file not shown.
7 changes: 5 additions & 2 deletions xlsxwriter/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ def write_dynamic_array_formula(

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

if not self.use_future_functions:
if not self.use_future_functions and not expand_future_functions:
return formula

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

# Escape any future functions.
formula = self._prepare_formula(formula, True)

col_data["formula"] = formula
# We write the formulas below after the table data.

Expand Down

0 comments on commit 878affa

Please sign in to comment.