Skip to content

Commit dad8f49

Browse files
committed
This closes qax-os#417 and closes qax-os#520, new API GetCellType has been added
1 parent 72d84c0 commit dad8f49

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

calc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ type formulaCriteria struct {
158158
Condition string
159159
}
160160

161-
// ArgType is the type if formula argument type.
161+
// ArgType is the type of formula argument type.
162162
type ArgType byte
163163

164164
// Formula argument types enumeration.

cell.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ import (
2020
"time"
2121
)
2222

23+
// CellType is the type of cell value type.
24+
type CellType byte
25+
26+
// Cell value types enumeration.
27+
const (
28+
CellTypeUnset CellType = iota
29+
CellTypeBool
30+
CellTypeDate
31+
CellTypeError
32+
CellTypeNumber
33+
CellTypeString
34+
)
35+
2336
const (
2437
// STCellFormulaTypeArray defined the formula is an array formula.
2538
STCellFormulaTypeArray = "array"
@@ -31,6 +44,17 @@ const (
3144
STCellFormulaTypeShared = "shared"
3245
)
3346

47+
// cellTypes mapping the cell's data type and enumeration.
48+
var cellTypes = map[string]CellType{
49+
"b": CellTypeBool,
50+
"d": CellTypeDate,
51+
"n": CellTypeNumber,
52+
"e": CellTypeError,
53+
"s": CellTypeString,
54+
"str": CellTypeString,
55+
"inlineStr": CellTypeString,
56+
}
57+
3458
// GetCellValue provides a function to get formatted value from cell by given
3559
// worksheet name and axis in spreadsheet file. If it is possible to apply a
3660
// 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)
4367
})
4468
}
4569

70+
// GetCellType provides a function to get the cell's data type by given
71+
// worksheet name and axis in spreadsheet file.
72+
func (f *File) GetCellType(sheet, axis string) (CellType, error) {
73+
cellTypes := map[string]CellType{
74+
"b": CellTypeBool,
75+
"d": CellTypeDate,
76+
"n": CellTypeNumber,
77+
"e": CellTypeError,
78+
"s": CellTypeString,
79+
"str": CellTypeString,
80+
"inlineStr": CellTypeString,
81+
}
82+
var (
83+
err error
84+
cellTypeStr string
85+
cellType CellType = CellTypeUnset
86+
)
87+
if cellTypeStr, err = f.getCellStringFunc(sheet, axis, func(x *xlsxWorksheet, c *xlsxC) (string, bool, error) {
88+
return c.T, true, nil
89+
}); err != nil {
90+
return CellTypeUnset, err
91+
}
92+
cellType = cellTypes[cellTypeStr]
93+
return cellType, err
94+
}
95+
4696
// SetCellValue provides a function to set the value of a cell. The specified
4797
// coordinates should not be in the first row of the table, a complex number
4898
// can be set with string text. The following shows the supported data

cell_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,19 @@ func TestGetCellValue(t *testing.T) {
244244
assert.NoError(t, err)
245245
}
246246

247+
func TestGetCellType(t *testing.T) {
248+
f := NewFile()
249+
cellType, err := f.GetCellType("Sheet1", "A1")
250+
assert.NoError(t, err)
251+
assert.Equal(t, CellTypeUnset, cellType)
252+
assert.NoError(t, f.SetCellValue("Sheet1", "A1", "A1"))
253+
cellType, err = f.GetCellType("Sheet1", "A1")
254+
assert.NoError(t, err)
255+
assert.Equal(t, CellTypeString, cellType)
256+
_, err = f.GetCellType("Sheet1", "A")
257+
assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
258+
}
259+
247260
func TestGetCellFormula(t *testing.T) {
248261
// Test get cell formula on not exist worksheet.
249262
f := NewFile()

0 commit comments

Comments
 (0)