summaryrefslogtreecommitdiff
path: root/cell.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2021-09-09 23:43:16 +0800
committerxuri <xuri.me@gmail.com>2021-09-09 23:43:16 +0800
commitdad8f490cc2df664bf1e7c6770ecd89a0c0e7fe4 (patch)
tree2eefc9599177b0f64a5a2a1e54fb55b7025f39b9 /cell.go
parent72d84c0cbdd0ad748dba19e21d4e92ea077110c7 (diff)
This closes #417 and closes #520, new API `GetCellType` has been added
Diffstat (limited to 'cell.go')
-rw-r--r--cell.go50
1 files changed, 50 insertions, 0 deletions
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