Skip to content

Commit 49602d3

Browse files
committed
Add rows.RowColumns to retrieve raw cell values
To be able to convert the to time.Time with ExcelDateToTime, without de-formatting them
1 parent 89465f4 commit 49602d3

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

date.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,11 @@ func ExcelDateToTime(excelDate float64, use1904Format bool) (time.Time, error) {
182182
}
183183
return timeFromExcelTime(excelDate, use1904Format), nil
184184
}
185+
186+
// ExcelDateToTime converts a float-based excel date representation to a time.Time.
187+
func (f *File) ExcelDateToTime(excelDate float64) (time.Time, error) {
188+
if excelDate < 0 {
189+
return time.Time{}, newInvalidExcelDateError(excelDate)
190+
}
191+
return timeFromExcelTime(excelDate, f.WorkBook.WorkbookPr.Date1904), nil
192+
}

lib.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,16 @@ func ColumnNumberToName(num int) (string, error) {
165165
if num > TotalColumns {
166166
return "", fmt.Errorf("column number exceeds maximum limit")
167167
}
168-
var col string
168+
col := make([]byte, 0, 4)
169169
for num > 0 {
170-
col = string(rune((num-1)%26+65)) + col
170+
col = append(col, byte(65+(num-1)%26))
171171
num = (num - 1) / 26
172172
}
173-
return col, nil
173+
// reverse col
174+
for i, j := 0, len(col)-1; i < j; i, j = i+1, j-1 {
175+
col[i], col[j] = col[j], col[i]
176+
}
177+
return string(col), nil
174178
}
175179

176180
// CellNameToCoordinates converts alphanumeric cell name to [X, Y] coordinates

rows.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,22 @@ func (rows *Rows) Error() error {
7676

7777
// Columns return the current row's column values.
7878
func (rows *Rows) Columns() ([]string, error) {
79+
_, columns, err := rows.RawColumns(false)
80+
return columns, err
81+
}
82+
83+
// RawColumns return the current row's column raw, unformatted values
84+
func (rows *Rows) RawColumns(noFormatted bool) ([]xlsxC, []string, error) {
7985
var (
8086
err error
8187
inElement string
8288
row, cellCol int
8389
columns []string
90+
cells []xlsxC
8491
)
8592

8693
if rows.stashRow >= rows.curRow {
87-
return columns, err
94+
return cells, columns, err
8895
}
8996

9097
d := rows.f.sharedStringsReader()
@@ -101,11 +108,11 @@ func (rows *Rows) Columns() ([]string, error) {
101108
if attr.Name.Local == "r" {
102109
row, err = strconv.Atoi(attr.Value)
103110
if err != nil {
104-
return columns, err
111+
return cells, columns, err
105112
}
106113
if row > rows.curRow {
107114
rows.stashRow = row - 1
108-
return columns, err
115+
return cells, columns, err
109116
}
110117
}
111118
}
@@ -117,21 +124,30 @@ func (rows *Rows) Columns() ([]string, error) {
117124
if colCell.R != "" {
118125
cellCol, _, err = CellNameToCoordinates(colCell.R)
119126
if err != nil {
120-
return columns, err
127+
return cells, columns, err
121128
}
122129
}
123130
blank := cellCol - len(columns)
124-
val, _ := colCell.getValueFrom(rows.f, d)
125-
columns = append(appendSpace(blank, columns), val)
131+
for i := 1; i < blank; i++ {
132+
cells = append(cells, xlsxC{})
133+
if !noFormatted {
134+
columns = append(columns, "")
135+
}
136+
}
137+
cells = append(cells, colCell)
138+
if !noFormatted {
139+
val, _ := colCell.getValueFrom(rows.f, d)
140+
columns = append(columns, val)
141+
}
126142
}
127143
case xml.EndElement:
128144
inElement = startElement.Name.Local
129145
if inElement == "row" {
130-
return columns, err
146+
return cells, columns, err
131147
}
132148
}
133149
}
134-
return columns, err
150+
return cells, columns, err
135151
}
136152

137153
// appendSpace append blank characters to slice by given length and source slice.

0 commit comments

Comments
 (0)