1
0
mirror of https://github.com/pbnjay/grate.git synced 2025-01-20 21:18:23 +02:00

deal with invalid files instead of making all files worse

This commit is contained in:
Jeremy Jay 2021-02-23 09:15:25 -05:00
parent 460301037b
commit 10c14fa766

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"log" "log"
"time" "time"
"github.com/pbnjay/grate"
) )
// Sheet holds raw and rendered values for a spreadsheet. // Sheet holds raw and rendered values for a spreadsheet.
@ -19,10 +21,6 @@ type Sheet struct {
// Resize the sheet for the number of rows and cols given. // Resize the sheet for the number of rows and cols given.
// Newly added cells default to blank. // Newly added cells default to blank.
func (s *Sheet) Resize(rows, cols int) { func (s *Sheet) Resize(rows, cols int) {
// some sheets are off by one
rows++
cols++
if rows <= 0 { if rows <= 0 {
rows = 1 rows = 1
} }
@ -37,7 +35,7 @@ func (s *Sheet) Resize(rows, cols int) {
s.Rows = append(s.Rows, make([]Cell, cols)) s.Rows = append(s.Rows, make([]Cell, cols))
} }
for i := 0; len(s.Rows[i]) < cols; i++ { for i := 0; i < s.NumRows && len(s.Rows[i]) < cols; i++ {
r2 := make([]Cell, cols-len(s.Rows[i])) r2 := make([]Cell, cols-len(s.Rows[i]))
s.Rows[i] = append(s.Rows[i], r2...) s.Rows[i] = append(s.Rows[i], r2...)
} }
@ -45,10 +43,22 @@ func (s *Sheet) Resize(rows, cols int) {
// Put the value at the cell location given. // Put the value at the cell location given.
func (s *Sheet) Put(row, col int, value interface{}, fmtNum uint16) { func (s *Sheet) Put(row, col int, value interface{}, fmtNum uint16) {
log.Println(row, col, value, fmtNum)
if row >= s.NumRows || col >= s.NumCols { if row >= s.NumRows || col >= s.NumCols {
log.Printf("grate: cell out of bounds row %d>=%d, col %d>=%d", if grate.Debug {
row, s.NumRows, col, s.NumCols) log.Printf("grate: cell out of bounds row %d>=%d, col %d>=%d",
return row, s.NumRows, col, s.NumCols)
}
// per the spec, this is an invalid Excel file
// but we'll resize in place instead of crashing out
if row >= s.NumRows {
s.NumRows = row + 1
}
if col >= s.NumCols {
s.NumCols = col + 1
}
s.Resize(s.NumRows, s.NumCols)
} }
ct, ok := s.Formatter.getCellType(fmtNum) ct, ok := s.Formatter.getCellType(fmtNum)