From eb662681a183ac23d27704d9b732f507900e3458 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 23 Feb 2026 00:40:47 +0800 Subject: [PATCH] fix incorrect column count when importing mscfb excel file --- .../excel/excel_mscfb_file_basic_data_table.go | 9 ++++++--- .../excel/excel_mscfb_file_basic_data_table_test.go | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/converters/excel/excel_mscfb_file_basic_data_table.go b/pkg/converters/excel/excel_mscfb_file_basic_data_table.go index a2dc89b5..59debfd6 100644 --- a/pkg/converters/excel/excel_mscfb_file_basic_data_table.go +++ b/pkg/converters/excel/excel_mscfb_file_basic_data_table.go @@ -86,7 +86,7 @@ func (t *ExcelMSCFBFileBasicDataTable) DataRowIterator() datatable.BasicDataTabl // ColumnCount returns the total count of column in this data row func (r *ExcelMSCFBFileBasicDataTableRow) ColumnCount() int { row := r.sheet.Row(r.rowIndex) - return row.LastCol() + 1 + return row.LastCol() } // GetData returns the data in the specified column index @@ -195,7 +195,10 @@ func CreateNewExcelMSCFBFileBasicDataTable(data []byte, hasTitleLine bool) (data } if i == 0 { - for j := 0; j <= row.LastCol(); j++ { + // row.LastCol() returns "colMac" in the "Row" struct, that is an unsigned integer that specifies the one-based index of the last column. + // But row.FirstCol() returns "colMic" in the "Row" struct, that is an unsigned integer that specifies the zero-based index of the first column. + // Reference: https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/4aab09eb-49ed-4d01-a3b1-1d726247d3c2 + for j := 0; j < row.LastCol(); j++ { headerItem := row.Col(j) if headerItem == "" { @@ -205,7 +208,7 @@ func CreateNewExcelMSCFBFileBasicDataTable(data []byte, hasTitleLine bool) (data firstRowItems = append(firstRowItems, headerItem) } } else { - for j := 0; j <= min(row.LastCol(), len(firstRowItems)-1); j++ { + for j := 0; j < min(row.LastCol(), len(firstRowItems)); j++ { headerItem := row.Col(j) if headerItem != firstRowItems[j] { diff --git a/pkg/converters/excel/excel_mscfb_file_basic_data_table_test.go b/pkg/converters/excel/excel_mscfb_file_basic_data_table_test.go index 742427aa..93519c37 100644 --- a/pkg/converters/excel/excel_mscfb_file_basic_data_table_test.go +++ b/pkg/converters/excel/excel_mscfb_file_basic_data_table_test.go @@ -300,10 +300,10 @@ func TestExcelMSCFBFileBasicDataRowColumnCount(t *testing.T) { iterator := datatable.DataRowIterator() row1 := iterator.Next() - assert.EqualValues(t, 4, row1.ColumnCount()) + assert.EqualValues(t, 3, row1.ColumnCount()) row2 := iterator.Next() - assert.EqualValues(t, 4, row2.ColumnCount()) + assert.EqualValues(t, 3, row2.ColumnCount()) } func TestExcelMSCFBFileBasicDataRowGetData(t *testing.T) {