Skip to content

Commit

Permalink
This closes #417 and closes #520, new API GetCellType has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Sep 9, 2021
1 parent 72d84c0 commit dad8f49
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
50 changes: 50 additions & 0 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

4 comments on commit dad8f49

@brianmori
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @xuri ,

Excel has these types

would it be possible to align with excel types?

I think the cell format is also important, as example a cell with type "currency" needs to specify also which currency

@xuri
Copy link
Member Author

@xuri xuri commented on dad8f49 Dec 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback, GetCellType following the possible enumeration values for cells in OOXML spec.

@brianmori
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the tip, is it correct there is no method at the moment to get the cell metadata?

Example, there is the need to extract from an Excel file columns with the correct data type to be manipulated after

Some columns are "Accounting", other "Currency", other "Number".

For a type "Currency / Accounting" it is important to get which Currency (EUR, USD, ...)

For a number the precision to distinguish integer from float

Does the library support this use case? I checked the https://xuri.me/excelize/en/cell.html and I have not found this info

@xuri
Copy link
Member Author

@xuri xuri commented on dad8f49 Dec 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number format info was storage in the inner style, this library just support to get style ID, but doesn't support to extract style details by cell currently.

Please sign in to comment.