From dad8f490cc2df664bf1e7c6770ecd89a0c0e7fe4 Mon Sep 17 00:00:00 2001 From: xuri Date: Thu, 9 Sep 2021 23:43:16 +0800 Subject: [PATCH] This closes #417 and closes #520, new API `GetCellType` has been added --- calc.go | 2 +- cell.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ cell_test.go | 13 +++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/calc.go b/calc.go index d650eca..59d97e0 100644 --- a/calc.go +++ b/calc.go @@ -158,7 +158,7 @@ type formulaCriteria struct { Condition string } -// ArgType is the type if formula argument type. +// ArgType is the type of formula argument type. type ArgType byte // Formula argument types enumeration. diff --git a/cell.go b/cell.go index 59d3bbd..2d49a5e 100644 --- a/cell.go +++ b/cell.go @@ -20,6 +20,19 @@ import ( "time" ) +// CellType is the type of cell value type. +type CellType byte + +// Cell value types enumeration. +const ( + CellTypeUnset CellType = iota + CellTypeBool + CellTypeDate + CellTypeError + CellTypeNumber + CellTypeString +) + const ( // STCellFormulaTypeArray defined the formula is an array formula. STCellFormulaTypeArray = "array" @@ -31,6 +44,17 @@ const ( STCellFormulaTypeShared = "shared" ) +// cellTypes mapping the cell's data type and enumeration. +var cellTypes = map[string]CellType{ + "b": CellTypeBool, + "d": CellTypeDate, + "n": CellTypeNumber, + "e": CellTypeError, + "s": CellTypeString, + "str": CellTypeString, + "inlineStr": CellTypeString, +} + // GetCellValue provides a function to get formatted value from cell by given // worksheet name and axis in spreadsheet file. If it is possible to apply a // format to the cell value, it will do so, if not then an error will be @@ -43,6 +67,32 @@ func (f *File) GetCellValue(sheet, axis string, opts ...Options) (string, error) }) } +// GetCellType provides a function to get the cell's data type by given +// worksheet name and axis in spreadsheet file. +func (f *File) GetCellType(sheet, axis string) (CellType, error) { + cellTypes := map[string]CellType{ + "b": CellTypeBool, + "d": CellTypeDate, + "n": CellTypeNumber, + "e": CellTypeError, + "s": CellTypeString, + "str": CellTypeString, + "inlineStr": CellTypeString, + } + var ( + err error + cellTypeStr string + cellType CellType = CellTypeUnset + ) + if cellTypeStr, err = f.getCellStringFunc(sheet, axis, func(x *xlsxWorksheet, c *xlsxC) (string, bool, error) { + return c.T, true, nil + }); err != nil { + return CellTypeUnset, err + } + cellType = cellTypes[cellTypeStr] + return cellType, err +} + // SetCellValue provides a function to set the value of a cell. The specified // coordinates should not be in the first row of the table, a complex number // can be set with string text. The following shows the supported data diff --git a/cell_test.go b/cell_test.go index d56854b..5467e43 100644 --- a/cell_test.go +++ b/cell_test.go @@ -244,6 +244,19 @@ func TestGetCellValue(t *testing.T) { assert.NoError(t, err) } +func TestGetCellType(t *testing.T) { + f := NewFile() + cellType, err := f.GetCellType("Sheet1", "A1") + assert.NoError(t, err) + assert.Equal(t, CellTypeUnset, cellType) + assert.NoError(t, f.SetCellValue("Sheet1", "A1", "A1")) + cellType, err = f.GetCellType("Sheet1", "A1") + assert.NoError(t, err) + assert.Equal(t, CellTypeString, cellType) + _, err = f.GetCellType("Sheet1", "A") + assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`) +} + func TestGetCellFormula(t *testing.T) { // Test get cell formula on not exist worksheet. f := NewFile()