已验证 提交 dad8f490 编写于 作者: xurime's avatar xurime

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

上级 72d84c0c
......@@ -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.
......
......@@ -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
......
......@@ -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()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册