Skip to content

Commit

Permalink
deal with invalid files instead of making all files worse
Browse files Browse the repository at this point in the history
  • Loading branch information
pbnjay committed Feb 23, 2021
1 parent 4603010 commit 10c14fa
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions commonxl/sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"log"
"time"

"github.com/pbnjay/grate"
)

// Sheet holds raw and rendered values for a spreadsheet.
Expand All @@ -19,10 +21,6 @@ type Sheet struct {
// Resize the sheet for the number of rows and cols given.
// Newly added cells default to blank.
func (s *Sheet) Resize(rows, cols int) {
// some sheets are off by one
rows++
cols++

if rows <= 0 {
rows = 1
}
Expand All @@ -37,18 +35,30 @@ func (s *Sheet) Resize(rows, cols int) {
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]))
s.Rows[i] = append(s.Rows[i], r2...)
}
}

// Put the value at the cell location given.
func (s *Sheet) Put(row, col int, value interface{}, fmtNum uint16) {
log.Println(row, col, value, fmtNum)
if row >= s.NumRows || col >= s.NumCols {
log.Printf("grate: cell out of bounds row %d>=%d, col %d>=%d",
row, s.NumRows, col, s.NumCols)
return
if grate.Debug {
log.Printf("grate: cell out of bounds row %d>=%d, col %d>=%d",
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)
Expand Down

0 comments on commit 10c14fa

Please sign in to comment.