Skip to content

Commit

Permalink
Fix for formula string values. Issue #122.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Jul 22, 2014
1 parent 3e601af commit 81e39fc
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 30 deletions.
14 changes: 14 additions & 0 deletions xlsxwriter/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
# Copyright (c), 2013-2014, John McNamara, jmcnamara@cpan.org
#

import sys
import datetime
from decimal import Decimal

try:
# For compatibility between Python 2 and 3.
from StringIO import StringIO
Expand All @@ -25,3 +29,13 @@
# For Python 2.5 support.
from .compat_collections import defaultdict
from .compat_collections import namedtuple

# Types to check in Python 2/3.
if sys.version_info[0] == 2:
num_types = (float, int, long, Decimal, Fraction)
str_types = basestring
date_types = (datetime.datetime, datetime.date, datetime.time)
else:
num_types = (float, int, Decimal, Fraction)
str_types = str
date_types = (datetime.datetime, datetime.date, datetime.time)
62 changes: 62 additions & 0 deletions xlsxwriter/test/comparison/test_types06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2014, John McNamara, jmcnamara@cpan.org
#

import unittest
import os
from ...workbook import Workbook
from ..helperfunctions import _compare_xlsx_files


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

def setUp(self):
self.maxDiff = None

filename = 'types06.xlsx'

test_dir = 'xlsxwriter/test/comparison/'
self.got_filename = test_dir + '_test_' + filename
self.exp_filename = test_dir + 'xlsx_files/' + filename

self.ignore_files = ['xl/calcChain.xml',
'[Content_Types].xml',
'xl/_rels/workbook.xml.rels']
self.ignore_elements = {}

def test_write_formula_default(self):
"""Test writing formulas with strings_to_formulas on."""
filename = self.got_filename

####################################################

workbook = Workbook(filename)
worksheet = workbook.add_worksheet()

worksheet.write(0, 0, '="0"&".0"', None, '0.0')

workbook.close()

####################################################

got, exp = _compare_xlsx_files(self.got_filename,
self.exp_filename,
self.ignore_files,
self.ignore_elements)

self.assertEqual(got, exp)

def tearDown(self):
# Cleanup.
if os.path.exists(self.got_filename):
os.remove(self.got_filename)

if __name__ == '__main__':
unittest.main()
Binary file not shown.
7 changes: 2 additions & 5 deletions xlsxwriter/workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from zipfile import ZipFile, ZIP_DEFLATED
from struct import unpack

from .compatibility import str_types

# Package imports.
from . import xmlwriter
from xlsxwriter.worksheet import Worksheet
Expand Down Expand Up @@ -640,11 +642,6 @@ def _prepare_num_formats(self):
for xf_format in (self.xf_formats + self.dxf_formats):
num_format = xf_format.num_format

if sys.version_info[0] == 2:
str_types = basestring
else:
str_types = str

# Check if num_format is an index to a built-in number format.
if not isinstance(num_format, str_types):
xf_format.num_format_index = int(num_format)
Expand Down
29 changes: 4 additions & 25 deletions xlsxwriter/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@

# Standard packages.
import re
import datetime
import tempfile
import codecs
import os
import sys
from decimal import Decimal

from warnings import warn

# Standard packages in Python 2/3 compatibility mode.
from .compatibility import StringIO
from .compatibility import Fraction
from .compatibility import defaultdict
from .compatibility import namedtuple
from .compatibility import num_types, str_types, date_types

# Package imports.
from . import xmlwriter
Expand Down Expand Up @@ -354,16 +352,6 @@ def write(self, row, col, *args):
# The first arg should be the token for all write calls.
token = args[0]

# Types to check in Python 2/3.
if sys.version_info[0] == 2:
num_types = (float, int, long, Decimal, Fraction)
str_types = basestring
date_types = (datetime.datetime, datetime.date, datetime.time)
else:
num_types = (float, int, Decimal, Fraction)
str_types = str
date_types = (datetime.datetime, datetime.date, datetime.time)

# Write None as a blank cell.
if token is None:
return self.write_blank(row, col, *args)
Expand Down Expand Up @@ -1293,7 +1281,6 @@ def set_column(self, firstcol, lastcol, width=None, cell_format=None,
def set_row(self, row, height=None, cell_format=None, options={}):
"""
Set the width, and other properties of a row.
range of columns.
Args:
row: Row number (zero-indexed).
Expand Down Expand Up @@ -4111,14 +4098,8 @@ def _get_range_data(self, row_start, col_start, row_end, col_end):
def _csv_join(self, *items):
# Create a csv string for use with data validation formulas and lists.

# We need to ensure that it works with unicode strings in Python 2/3.
if sys.version_info[0] == 2:
str_type = basestring
else:
str_type = str

# Convert non string types to string.
items = [str(item) if not isinstance(item, str_type) else item
items = [str(item) if not isinstance(item, str_types) else item
for item in items]

return ','.join(items)
Expand Down Expand Up @@ -4827,9 +4808,7 @@ def _write_cell(self, row, col, cell):

elif type(cell).__name__ == 'Formula':
# Write a formula. First check if the formula value is a string.
try:
float(cell.value)
except ValueError:
if isinstance(cell.value, str_types):
attributes.append(('t', 'str'))

self._xml_formula_element(cell.formula, cell.value, attributes)
Expand Down

0 comments on commit 81e39fc

Please sign in to comment.