diff --git a/cell.go b/cell.go index e47edd6..912aa69 100644 --- a/cell.go +++ b/cell.go @@ -20,6 +20,12 @@ func (c CellPos) A1Notation() string { return fmt.Sprintf("%s%d", aRangeLetter(c.Col), c.Row + 1) } +func (c CellPos) RangeForData(data [][]string) CellRange { + bottomLeft := CellPos{c.Row + len(data) - 1, c.Col + len(data[0]) - 1} + + return CellRange{Start: c, End: bottomLeft} +} + type CellRange struct { Start CellPos End CellPos diff --git a/cell_test.go b/cell_test.go index 8889214..7dc762e 100644 --- a/cell_test.go +++ b/cell_test.go @@ -36,3 +36,36 @@ func TestCellPosA1Notation(t *testing.T) { } } +var rangeTests = []struct{ + topLeft CellPos + width int + height int + expected string +}{ + {CellPos{}, 1, 1, "A1:A1"}, + {CellPos{}, 1, 2, "A1:A2"}, + {CellPos{}, 2, 2, "A1:B2"}, + {CellPos{0, 10}, 2, 2, "K1:L2"}, + {CellPos{10, 3}, 2, 3, "D11:E13"}, +} + +func TestRange(t *testing.T) { + for _, tt := range rangeTests { + var data [][]string + for i := 0; i < tt.height; i++ { + var row []string + for j := 0; j < tt.width; j++ { + row = append(row, "1") + } + data = append(data, row) + } + + got := tt.topLeft.RangeForData(data).String() + + if got != tt.expected { + t.Errorf("Wanted %s, but got %s for %+v, table: %v", tt.expected, got, tt, data) + } + + } +} + diff --git a/spreadsheet.go b/spreadsheet.go index 34a1696..be5882f 100644 --- a/spreadsheet.go +++ b/spreadsheet.go @@ -91,7 +91,7 @@ func (s *Sheet) UpdateFromPosition(data [][]string, start CellPos) error { converted = append(converted, strToInterface(row)) } - cellRange := DefaultRange(data) + cellRange := start.RangeForData(data) sheetRange := fmt.Sprintf("%s!%s", s.Title(), cellRange.String()) diff --git a/spreadsheet_test.go b/spreadsheet_test.go index ce3eee9..a1780ca 100644 --- a/spreadsheet_test.go +++ b/spreadsheet_test.go @@ -19,7 +19,7 @@ var tsvTests = []struct { func TestTsvToArr(t *testing.T) { for _, tt := range tsvTests { - got := TsvToArr(strings.NewReader(tt.data)) + got := TsvToArr(strings.NewReader(tt.data), "\t") if len(got) != len(tt.result) { t.Errorf("For \n%s\n, mismatched rows. wanted %v, but got %v", tt.data, tt.result, got) }