cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / sheet / pivotformat_test.go
pivotformat_test.go 1.7K
1package sheet23import "testing"45// One price typed as "$9,000" among "$x.xx" prices keeps the results in6// the column's two-decimal currency, as in Sheets: the pivot takes the7// format most of the column's numbers show in, not the first row's.8func TestPivotValueFormatIgnoresOutlier(t *testing.T) {9 w := sales(t)10 src := w.Lookup("Sales")11 if err := src.Set(at("E2"), "$9,000"); err != nil {12 t.Fatal(err)13 }14 s := newPivot(t, w, func(p *Pivot) {15 p.Rows = []PivotGroup{{Col: 1}}16 p.Values = []PivotValue{{Col: 4, Summarize: SumBy}}17 })18 checkGrid(t, s,19 "Product|SUM of Price",20 "Ink|$18.00",21 "Pens|$9,005.25",22 "Grand Total|$9,023.25")2324 // A frequency table counts, so its values show no currency at all;25 // each label keeps its own cell's format.26 r, _ := src.UsedRange()27 f, err := w.CreatePivot(src, r, "Frequency of Price", FrequencyPivot(src, r, 4))28 if err != nil {29 t.Fatal(err)30 }31 checkGrid(t, f,32 "Price|Count|Percent",33 "$9.00|2|40.00%",34 "$2.50|1|20.00%",35 "$2.75|1|20.00%",36 "$9,000|1|20.00%",37 "Grand Total|5|100.00%")38}3940// The column's own format wins over its cells'; without one, a tie goes41// to the format met first.42func TestPivotValueFormatColumnAndTies(t *testing.T) {43 w := bookOf(t, page{"Sales", map[string]string{44 "A1": "K", "B1": "N",45 "A2": "a", "B2": "$1",46 "A3": "a", "B3": "2.5%",47 "A4": "a", "B4": "$3",48 "A5": "a", "B5": "4%",49 "A6": "a", "B6": "x",50 }})51 s := newPivot(t, w, func(p *Pivot) {52 p.Rows = []PivotGroup{{Col: 0}}53 p.Values = []PivotValue{{Col: 1, Summarize: MaxBy}}54 })55 checkGrid(t, s, "K|MAX of N", "a|$3", "Grand Total|$3")5657 w.Lookup("Sales").SetFormat(col(1), Preset(FmtNumber))58 checkGrid(t, s, "K|MAX of N", "a|3.00", "Grand Total|3.00")59}