Skip to content

Commit f696ca8

Browse files
carlos-lopez-tecnativafeg-adhoc
authored andcommitted
[FIX] account_statement_import_sheet_file: Prevent negative index in parser header
When importing an Excel file with a header and the field header_lines_skip_count is set to its default value (0), the header is retrieved using the -1 index (the last row). complementary to commit 13285ab
1 parent 3a5d50e commit f696ca8

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

account_statement_import_sheet_file/models/account_statement_import_sheet_parser.py

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,40 @@ class AccountStatementImportSheetParser(models.TransientModel):
4141
_description = "Bank Statement Import Sheet Parser"
4242

4343
@api.model
44-
def parse_header(self, data_file, encoding, csv_options, header_lines_skip_count=0):
45-
try:
46-
workbook = xlrd.open_workbook(
47-
file_contents=data_file,
48-
encoding_override=encoding if encoding else None,
49-
)
50-
sheet = workbook.sheet_by_index(0)
51-
values = sheet.row_values(header_lines_skip_count - 1)
52-
return [str(value) for value in values]
53-
except xlrd.XLRDError:
54-
_logger.error("Pass this method")
55-
56-
data = StringIO(data_file.decode(encoding or "utf-8"))
57-
csv_data = reader(data, **csv_options)
58-
csv_data_lst = list(csv_data)
59-
header = [value.strip() for value in csv_data_lst[header_lines_skip_count - 1]]
44+
def parse_header(self, csv_or_xlsx, mapping):
45+
if mapping.no_header:
46+
return []
47+
header_line = mapping.header_lines_skip_count
48+
# prevent negative indexes
49+
if header_line > 0:
50+
header_line -= 1
51+
if isinstance(csv_or_xlsx, tuple):
52+
sheet = csv_or_xlsx[1]
53+
# Check if it's xlrd (old Excel format)
54+
if isinstance(sheet, xlrd.sheet.Sheet):
55+
header = [str(value).strip() for value in sheet.row_values(header_line)]
56+
else:
57+
# It's openpyxl (new Excel format)
58+
# iter_rows is 1-indexed, so we need to add 1
59+
rows = list(
60+
sheet.iter_rows(
61+
min_row=header_line + 1,
62+
max_row=header_line + 1,
63+
values_only=True,
64+
)
65+
)
66+
if rows:
67+
header = [
68+
str(value).strip() if value is not None else ""
69+
for value in rows[0]
70+
]
71+
else:
72+
header = []
73+
else:
74+
[next(csv_or_xlsx) for _i in range(header_line)]
75+
header = [value.strip() for value in next(csv_or_xlsx)]
76+
if mapping.offset_column:
77+
header = header[mapping.offset_column :]
6078
return header
6179

6280
@api.model
@@ -190,33 +208,7 @@ def _parse_lines(self, mapping, data_file, currency_code):
190208
csv_or_xlsx = reader(StringIO(decoded_file), **csv_options)
191209

192210
# Procesar el header
193-
header = False
194-
if not mapping.no_header:
195-
header_line = mapping.header_lines_skip_count - 1
196-
if isinstance(csv_or_xlsx, tuple):
197-
workbook, sheet = csv_or_xlsx
198-
if isinstance(sheet, xlrd.sheet.Sheet):
199-
# XLS
200-
header = [
201-
str(value).strip() for value in sheet.row_values(header_line)
202-
]
203-
else:
204-
# XLSX
205-
header = [
206-
str(cell.value).strip()
207-
for cell in list(
208-
sheet.iter_rows(
209-
min_row=header_line + 1, max_row=header_line + 1
210-
)
211-
)[0]
212-
]
213-
else:
214-
# CSV
215-
[next(csv_or_xlsx) for _ in range(header_line)]
216-
header = [value.strip() for value in next(csv_or_xlsx)]
217-
218-
if mapping.offset_column:
219-
header = header[mapping.offset_column :]
211+
header = self.parse_header(csv_or_xlsx, mapping)
220212

221213
for column_name in self._get_column_names():
222214
columns[column_name] = self._get_column_indexes(

account_statement_import_sheet_file/views/account_statement_import_sheet_mapping.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
class="alert alert-warning"
4646
role="alert"
4747
invisible="not no_header"
48+
colspan="2"
4849
>
4950
<span
5051
class="fa fa-info-circle"

0 commit comments

Comments
 (0)