1package sheet23import (4 "bytes"5 "testing"6)78func TestPivotColumnSubtotals(t *testing.T) {9 w := sales(t)10 s := newPivot(t, w, func(p *Pivot) {11 p.Columns = []PivotGroup{{Col: 0}, {Col: 1}}12 p.Values = []PivotValue{{Col: 3, Summarize: SumBy, Name: "Units sold"}}13 })14 // A subtotal column after each region's products, named in the15 // Region row; the value's own name heads every column.16 checkGrid(t, s,17 "Region|East||East Total|North|North Total|West||West Total|Grand Total",18 "Product|Ink|Pens||Pens||Ink|Pens",19 "|Units sold|Units sold|Units sold|Units sold|Units sold|Units sold|Units sold|Units sold|Units sold",20 "Grand Total|6|10|16|0|0|4|3|7|23")21 if c := s.Cell(at("D1")); c == nil || !c.Style.Bold {22 t.Error("the subtotal's header isn't bold")23 }24 // Without column totals, neither subtotals nor the grand total show.25 p, _ := s.Pivot()26 p.ColumnTotals = false27 s.SetPivot(p, "")28 checkGrid(t, s,29 "Region|East||North|West",30 "Product|Ink|Pens|Pens|Ink|Pens",31 "|Units sold|Units sold|Units sold|Units sold|Units sold",32 "Grand Total|6|10|0|4|3")33 // Sorting the outer field by its total keeps each region together.34 p.Columns[0] = PivotGroup{Col: 0, Desc: true, SortBy: 1}35 s.SetPivot(p, "")36 checkGrid(t, s,37 "Region|East||West||North",38 "Product|Ink|Pens|Ink|Pens|Pens",39 "|Units sold|Units sold|Units sold|Units sold|Units sold",40 "Grand Total|6|10|4|3|0")41}4243func TestPivotBlankGroupsAndNamesRoundTrip(t *testing.T) {44 w := sales(t)45 w.Lookup("Sales").Set(at("A3"), "")46 s := newPivot(t, w, func(p *Pivot) {47 p.Rows = []PivotGroup{{Col: 0}, {Col: 1}}48 p.Columns = []PivotGroup{{Col: 0}, {Col: 1}}49 p.Values = []PivotValue{{Col: 3, Summarize: SumBy, Name: "Units"}}50 })51 // Blank cells group under a blank label, as in Sheets; their total52 // is just "Total".53 grid := pivotGrid(s)54 found := false55 for _, row := range grid {56 if len(row) >= 6 && row[:6] == "Total|" {57 found = true58 }59 }60 if !found {61 t.Errorf("no Total row for the blank group:\n%v", grid)62 }63 var b bytes.Buffer64 if err := w.Write(&b); err != nil {65 t.Fatal(err)66 }67 back, err := ReadBook(&b)68 if err != nil {69 t.Fatal(err)70 }71 p, _ := back.Lookup(s.Name()).Pivot()72 if p.Values[0].Name != "Units" || !p.ColumnTotals {73 t.Errorf("read back %+v", p)74 }75 checkGrid(t, back.Lookup(s.Name()), grid...)76}