This closes #417 and closes #520, new API `GetCellType` has been added

This commit is contained in:
xuri 2021-09-09 23:43:16 +08:00
parent 72d84c0cbd
commit dad8f490cc
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 64 additions and 1 deletions

View File

@ -158,7 +158,7 @@ type formulaCriteria struct {
Condition string Condition string
} }
// ArgType is the type if formula argument type. // ArgType is the type of formula argument type.
type ArgType byte type ArgType byte
// Formula argument types enumeration. // Formula argument types enumeration.

50
cell.go
View File

@ -20,6 +20,19 @@ import (
"time" "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 ( const (
// STCellFormulaTypeArray defined the formula is an array formula. // STCellFormulaTypeArray defined the formula is an array formula.
STCellFormulaTypeArray = "array" STCellFormulaTypeArray = "array"
@ -31,6 +44,17 @@ const (
STCellFormulaTypeShared = "shared" 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 // 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 // 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 // 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 // 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 // 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 // can be set with string text. The following shows the supported data

View File

@ -244,6 +244,19 @@ func TestGetCellValue(t *testing.T) {
assert.NoError(t, err) 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) { func TestGetCellFormula(t *testing.T) {
// Test get cell formula on not exist worksheet. // Test get cell formula on not exist worksheet.
f := NewFile() f := NewFile()