(core) Fix issue that ints would be imported with a trailing ".0" from Google Sheets.

Summary:
Whole numbers, when imported from Excel into a Text column show up
without decimals (e.g. "300"), but when imported from Google Sheets show
up with decimals (e.g. "300.0"). The decimals are hard for end-users to
remove. Fix by treating whole numbers consistently as ints.

Test Plan: Added a fixture reproducing the issue, and a test case.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D3800
This commit is contained in:
Dmitry S 2023-02-13 22:52:48 -05:00
parent cbf925aa00
commit 17569561bf
3 changed files with 14 additions and 0 deletions

View File

@ -97,6 +97,14 @@ class TestImportXLS(unittest.TestCase):
[-10.25, -8.00, -5.75, -3.50, "n/a", ' 1. ', " ??? ", 5.50, None, "-", [-10.25, -8.00, -5.75, -3.50, "n/a", ' 1. ', " ??? ", 5.50, None, "-",
12.25, 0.00, None, 0.00, "--", 23.50, "NA", 28.00, 30.25, 32.50]) 12.25, 0.00, None, 0.00, "--", 23.50, "NA", 28.00, 30.25, 32.50])
def test_excel_numeric_gs(self):
# openpyxl sometimes sees floats for int values when those values come from Google Sheets (if
# saved via Excel, they'll look like ints). Check that they don't get imported with ".0" suffix.
parsed_file = import_xls.parse_file(*_get_fixture('test_excel_numeric_gs.xlsx'))
sheet = parsed_file[1][0]
self._check_col(sheet, 0, "TagId", "Numeric", [10, 20, 30, 40.5])
self._check_col(sheet, 1, "TagName", "Any", ["foo", "300", "bar", "300.1"])
def test_excel_single_merged_cell(self): def test_excel_single_merged_cell(self):
# An older version had a bug where a single cell marked as 'merged' would cause an exception. # An older version had a bug where a single cell marked as 'merged' would cause an exception.
parsed_file = import_xls.parse_file(*_get_fixture('test_single_merged_cell.xlsx')) parsed_file = import_xls.parse_file(*_get_fixture('test_single_merged_cell.xlsx'))

View File

@ -195,6 +195,12 @@ class ColumnConverter(object):
# For some reason, we get 'str' type rather than 'unicode' for empty strings. # For some reason, we get 'str' type rather than 'unicode' for empty strings.
# Correct this, since all text should be unicode. # Correct this, since all text should be unicode.
value = u"" if value == "" else value value = u"" if value == "" else value
# Integer values sometimes show up as ints (from Excel), sometimes as floats (from Google).
# Make them consistently ints; this avoid addition of ".0" suffix when converting to text.
if type(value) == float and value.is_integer():
value = int(value)
try: try:
conv = self._converter.convert(value) conv = self._converter.convert(value)
self._converted_values.append(conv) self._converted_values.append(conv)