diff options
author | xuri <xuri.me@gmail.com> | 2022-10-24 00:02:22 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2022-10-24 00:52:09 +0800 |
commit | 14c6a198ce27b44fcce5447a2b757ce403ebb8fc (patch) | |
tree | a59eea77f6e6d691a1015afa2b3629cbe7cc695b /rows.go | |
parent | f843a9ea56710deb4cdb77ea2cd3a08d8d82d3e6 (diff) |
Support get cell value which contains a date in the ISO 8601 format
- Support set and get font color with indexed color
- New export variable `IndexedColorMapping`
- Fix getting incorrect page margin settings when the margin is 0
- Update unit tests and comments typo fixes
- ref #65, new formula functions: AGGREGATE and SUBTOTAL
Diffstat (limited to 'rows.go')
-rw-r--r-- | rows.go | 55 |
1 files changed, 42 insertions, 13 deletions
@@ -20,6 +20,8 @@ import ( "math" "os" "strconv" + "strings" + "time" "github.com/mohae/deepcopy" ) @@ -447,6 +449,39 @@ func (f *File) sharedStringsReader() *xlsxSST { return f.SharedStrings } +// getCellDate parse cell value which containing a boolean. +func (c *xlsxC) getCellBool(f *File, raw bool) (string, error) { + if !raw { + if c.V == "1" { + return "TRUE", nil + } + if c.V == "0" { + return "FALSE", nil + } + } + return f.formattedValue(c.S, c.V, raw), nil +} + +// getCellDate parse cell value which contains a date in the ISO 8601 format. +func (c *xlsxC) getCellDate(f *File, raw bool) (string, error) { + if !raw { + layout := "20060102T150405.999" + if strings.HasSuffix(c.V, "Z") { + layout = "20060102T150405Z" + if strings.Contains(c.V, "-") { + layout = "2006-01-02T15:04:05Z" + } + } else if strings.Contains(c.V, "-") { + layout = "2006-01-02 15:04:05Z" + } + if timestamp, err := time.Parse(layout, strings.ReplaceAll(c.V, ",", ".")); err == nil { + excelTime, _ := timeToExcelTime(timestamp, false) + c.V = strconv.FormatFloat(excelTime, 'G', 15, 64) + } + } + return f.formattedValue(c.S, c.V, raw), nil +} + // getValueFrom return a value from a column/row cell, this function is // intended to be used with for range on rows an argument with the spreadsheet // opened file. @@ -455,15 +490,9 @@ func (c *xlsxC) getValueFrom(f *File, d *xlsxSST, raw bool) (string, error) { defer f.Unlock() switch c.T { case "b": - if !raw { - if c.V == "1" { - return "TRUE", nil - } - if c.V == "0" { - return "FALSE", nil - } - } - return f.formattedValue(c.S, c.V, raw), nil + return c.getCellBool(f, raw) + case "d": + return c.getCellDate(f, raw) case "s": if c.V != "" { xlsxSI := 0 @@ -760,7 +789,7 @@ func (f *File) duplicateMergeCells(sheet string, ws *xlsxWorksheet, row, row2 in // <c r="G15" s="1" /> // </row> // -// Noteice: this method could be very slow for large spreadsheets (more than +// Notice: this method could be very slow for large spreadsheets (more than // 3000 rows one sheet). func checkRow(ws *xlsxWorksheet) error { for rowIdx := range ws.SheetData.Row { @@ -793,7 +822,7 @@ func checkRow(ws *xlsxWorksheet) error { if colCount < lastCol { oldList := rowData.C - newlist := make([]xlsxC, 0, lastCol) + newList := make([]xlsxC, 0, lastCol) rowData.C = ws.SheetData.Row[rowIdx].C[:0] @@ -802,10 +831,10 @@ func checkRow(ws *xlsxWorksheet) error { if err != nil { return err } - newlist = append(newlist, xlsxC{R: cellName}) + newList = append(newList, xlsxC{R: cellName}) } - rowData.C = newlist + rowData.C = newList for colIdx := range oldList { colData := &oldList[colIdx] |