From 6429588e1448f70539774a88840f094829cb9e07 Mon Sep 17 00:00:00 2001 From: MJacred Date: Thu, 14 Jul 2022 17:36:43 +0200 Subject: [PATCH] adjust `ErrColumnNumber`, rename `TotalColumns` to `MaxColumns` and add new constant `MinColumns` (#1272) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Benjamin Lösch --- calc.go | 8 ++++---- errors.go | 10 +++++----- lib.go | 7 ++----- lib_test.go | 6 +++--- sheet.go | 2 +- stream.go | 5 +---- stream_test.go | 6 +++--- xmlDrawing.go | 3 ++- 8 files changed, 21 insertions(+), 26 deletions(-) diff --git a/calc.go b/calc.go index 78846d2..d8161ce 100644 --- a/calc.go +++ b/calc.go @@ -1419,7 +1419,7 @@ func (f *File) parseReference(ctx *calcContext, sheet, reference string) (arg fo err = newInvalidColumnNameError(tokens[1]) return } - cr.Col = TotalColumns + cr.Col = MaxColumns } } if refs.Len() > 0 { @@ -1439,7 +1439,7 @@ func (f *File) parseReference(ctx *calcContext, sheet, reference string) (arg fo err = newInvalidColumnNameError(tokens[0]) return } - cr.Col = TotalColumns + cr.Col = MaxColumns } cellRanges.PushBack(cellRange{ From: cellRef{Sheet: sheet, Col: cr.Col, Row: 1}, @@ -14457,8 +14457,8 @@ func (fn *formulaFuncs) COLUMNS(argsList *list.List) formulaArg { return newErrorFormulaArg(formulaErrorVALUE, "COLUMNS requires 1 argument") } min, max := calcColumnsMinMax(argsList) - if max == TotalColumns { - return newNumberFormulaArg(float64(TotalColumns)) + if max == MaxColumns { + return newNumberFormulaArg(float64(MaxColumns)) } result := max - min + 1 if max == min { diff --git a/errors.go b/errors.go index 6606f1e..f5ea06e 100644 --- a/errors.go +++ b/errors.go @@ -61,7 +61,7 @@ func newInvalidStyleID(styleID int) error { // newFieldLengthError defined the error message on receiving the field length // overflow. func newFieldLengthError(name string) error { - return fmt.Errorf("field %s must be less or equal than 255 characters", name) + return fmt.Errorf("field %s must be less than or equal to 255 characters", name) } // newCellNameToCoordinatesError defined the error message on converts @@ -76,10 +76,10 @@ var ( ErrStreamSetColWidth = errors.New("must call the SetColWidth function before the SetRow function") // ErrColumnNumber defined the error message on receive an invalid column // number. - ErrColumnNumber = errors.New("column number exceeds maximum limit") + ErrColumnNumber = fmt.Errorf(`the column number must be greater than or equal to %d and less than or equal to %d`, MinColumns, MaxColumns) // ErrColumnWidth defined the error message on receive an invalid column // width. - ErrColumnWidth = fmt.Errorf("the width of the column must be smaller than or equal to %d characters", MaxColumnWidth) + ErrColumnWidth = fmt.Errorf("the width of the column must be less than or equal to %d characters", MaxColumnWidth) // ErrOutlineLevel defined the error message on receive an invalid outline // level number. ErrOutlineLevel = errors.New("invalid outline level") @@ -102,7 +102,7 @@ var ( ErrMaxRows = errors.New("row number exceeds maximum limit") // ErrMaxRowHeight defined the error message on receive an invalid row // height. - ErrMaxRowHeight = fmt.Errorf("the height of the row must be smaller than or equal to %d points", MaxRowHeight) + ErrMaxRowHeight = fmt.Errorf("the height of the row must be less than or equal to %d points", MaxRowHeight) // ErrImgExt defined the error message on receive an unsupported image // extension. ErrImgExt = errors.New("unsupported image extension") @@ -143,7 +143,7 @@ var ( ErrCustomNumFmt = errors.New("custom number format can not be empty") // ErrFontLength defined the error message on the length of the font // family name overflow. - ErrFontLength = fmt.Errorf("the length of the font family name must be smaller than or equal to %d", MaxFontFamilyLength) + ErrFontLength = fmt.Errorf("the length of the font family name must be less than or equal to %d", MaxFontFamilyLength) // ErrFontSize defined the error message on the size of the font is invalid. ErrFontSize = fmt.Errorf("font size must be between %d and %d points", MinFontSize, MaxFontSize) // ErrSheetIdx defined the error message on receive the invalid worksheet diff --git a/lib.go b/lib.go index f285a40..3170a6d 100644 --- a/lib.go +++ b/lib.go @@ -211,7 +211,7 @@ func ColumnNameToNumber(name string) (int, error) { } multi *= 26 } - if col > TotalColumns { + if col > MaxColumns { return -1, ErrColumnNumber } return col, nil @@ -225,10 +225,7 @@ func ColumnNameToNumber(name string) (int, error) { // excelize.ColumnNumberToName(37) // returns "AK", nil // func ColumnNumberToName(num int) (string, error) { - if num < 1 { - return "", fmt.Errorf("incorrect column number %d", num) - } - if num > TotalColumns { + if num < MinColumns || num > MaxColumns { return "", ErrColumnNumber } var col string diff --git a/lib_test.go b/lib_test.go index 027e5dd..64acb8a 100644 --- a/lib_test.go +++ b/lib_test.go @@ -79,7 +79,7 @@ func TestColumnNameToNumber_Error(t *testing.T) { } } _, err := ColumnNameToNumber("XFE") - assert.EqualError(t, err, ErrColumnNumber.Error()) + assert.ErrorIs(t, err, ErrColumnNumber) } func TestColumnNumberToName_OK(t *testing.T) { @@ -103,8 +103,8 @@ func TestColumnNumberToName_Error(t *testing.T) { assert.Equal(t, "", out) } - _, err = ColumnNumberToName(TotalColumns + 1) - assert.EqualError(t, err, ErrColumnNumber.Error()) + _, err = ColumnNumberToName(MaxColumns + 1) + assert.ErrorIs(t, err, ErrColumnNumber) } func TestSplitCellName_OK(t *testing.T) { diff --git a/sheet.go b/sheet.go index e88f953..2a722c9 100644 --- a/sheet.go +++ b/sheet.go @@ -256,7 +256,7 @@ func replaceRelationshipsBytes(content []byte) []byte { // SetActiveSheet provides a function to set the default active sheet of the // workbook by a given index. Note that the active index is different from the -// ID returned by function GetSheetMap(). It should be greater or equal to 0 +// ID returned by function GetSheetMap(). It should be greater than or equal to 0 // and less than the total worksheet numbers. func (f *File) SetActiveSheet(index int) { if index < 0 { diff --git a/stream.go b/stream.go index 641340e..0db2438 100644 --- a/stream.go +++ b/stream.go @@ -387,10 +387,7 @@ func (sw *StreamWriter) SetColWidth(min, max int, width float64) error { if sw.sheetWritten { return ErrStreamSetColWidth } - if min > TotalColumns || max > TotalColumns { - return ErrColumnNumber - } - if min < 1 || max < 1 { + if min < MinColumns || min > MaxColumns || max < MinColumns || max > MaxColumns { return ErrColumnNumber } if width > MaxColumnWidth { diff --git a/stream_test.go b/stream_test.go index 9776b38..6843e20 100644 --- a/stream_test.go +++ b/stream_test.go @@ -75,7 +75,7 @@ func TestStreamWriter(t *testing.T) { assert.NoError(t, file.SaveAs(filepath.Join("test", "TestStreamWriter.xlsx"))) // Test set cell column overflow. - assert.EqualError(t, streamWriter.SetRow("XFD1", []interface{}{"A", "B", "C"}), ErrColumnNumber.Error()) + assert.ErrorIs(t, streamWriter.SetRow("XFD1", []interface{}{"A", "B", "C"}), ErrColumnNumber) // Test close temporary file error. file = NewFile() @@ -139,8 +139,8 @@ func TestStreamSetColWidth(t *testing.T) { streamWriter, err := file.NewStreamWriter("Sheet1") assert.NoError(t, err) assert.NoError(t, streamWriter.SetColWidth(3, 2, 20)) - assert.EqualError(t, streamWriter.SetColWidth(0, 3, 20), ErrColumnNumber.Error()) - assert.EqualError(t, streamWriter.SetColWidth(TotalColumns+1, 3, 20), ErrColumnNumber.Error()) + assert.ErrorIs(t, streamWriter.SetColWidth(0, 3, 20), ErrColumnNumber) + assert.ErrorIs(t, streamWriter.SetColWidth(MaxColumns+1, 3, 20), ErrColumnNumber) assert.EqualError(t, streamWriter.SetColWidth(1, 3, MaxColumnWidth+1), ErrColumnWidth.Error()) assert.NoError(t, streamWriter.SetRow("A1", []interface{}{"A", "B", "C"})) assert.EqualError(t, streamWriter.SetColWidth(2, 3, 20), ErrStreamSetColWidth.Error()) diff --git a/xmlDrawing.go b/xmlDrawing.go index 4808685..3e54b72 100644 --- a/xmlDrawing.go +++ b/xmlDrawing.go @@ -109,7 +109,8 @@ const ( MaxRowHeight = 409 MinFontSize = 1 TotalRows = 1048576 - TotalColumns = 16384 + MinColumns = 1 + MaxColumns = 16384 TotalSheetHyperlinks = 65529 TotalCellChars = 32767 // pivotTableVersion should be greater than 3. One or more of the