cat / 012
clone

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

main
012 / internal / fileio / xlsxlines_test.go
xlsxlines_test.go 2.5K
1package fileio23import (4 "context"5 "path/filepath"6 "testing"78 "github.com/xuri/excelize/v2"910 "src.rest/cat/012/internal/sheet"11)1213// Column and row formats go to Excel as column and row styles and come14// back as line formats, without a cell per formatted blank.15func TestXLSXLineFormats(t *testing.T) {16 src := build(t, map[string]string{"A1": "Item", "B1": "Price", "B2": "3.5", "A3": "total"})17 src.SetFormat(sheet.Rect{From: sheet.Addr{Col: 1}, To: sheet.Addr{Col: 2, Row: sheet.MaxRows - 1}}, sheet.Preset(sheet.FmtCurrency))18 src.SetStyle(sheet.Rect{From: sheet.Addr{Row: 2}, To: sheet.Addr{Col: sheet.MaxCols - 1, Row: 2}}, func(st *sheet.Style) { st.Bold = true })19 src.SetStyle(sheet.Rect{From: sheet.Addr{Row: 99}, To: sheet.Addr{Col: sheet.MaxCols - 1, Row: 99}}, func(st *sheet.Style) { st.Italic = true })2021 name := filepath.Join(t.TempDir(), "lines.xlsx")22 if _, err := Export(context.Background(), name, XLSX, Snap(src, sheet.Rect{}, "Lines"), ExportOptions{}); err != nil {23 t.Fatal(err)24 }25 x, err := excelize.OpenFile(name)26 if err != nil {27 t.Fatal(err)28 }29 if id, _ := x.GetColStyle("Lines", "C"); id == 0 {30 t.Error("column C has no style in Excel")31 }32 x.Close()3334 res, err := Import(context.Background(), name, Options{})35 if err != nil {36 t.Fatal(err)37 }38 got := res.Sheet39 for _, a := range []string{"B9", "C1048576"} {40 if f := got.DisplayFormat(addr(t, a)); f.Kind != sheet.FmtCurrency {41 t.Errorf("%s shows %v, want currency", a, f.Kind)42 }43 }44 if !got.CellStyle(addr(t, "D3")).Bold || !got.CellStyle(addr(t, "Q100")).Italic || got.CellStyle(addr(t, "D4")).Bold {45 t.Error("row styles didn't come back")46 }47 if n := len(got.ColFormats()); n != 2 {48 t.Errorf("%d column formats, want 2", n)49 }5051 // The whole sheet's format goes out on every column and comes back52 // as the sheet's.53 src.SetStyle(sheet.Rect{To: sheet.Addr{Col: sheet.MaxCols - 1, Row: sheet.MaxRows - 1}}, func(st *sheet.Style) { st.Underline = true })54 if _, err := Export(context.Background(), name, XLSX, Snap(src, sheet.Rect{}, "Lines"), ExportOptions{}); err != nil {55 t.Fatal(err)56 }57 if res, err = Import(context.Background(), name, Options{}); err != nil {58 t.Fatal(err)59 }60 if _, st := res.Sheet.SheetFormat(); !st.Underline || !res.Sheet.CellStyle(addr(t, "ZZ9")).Underline {61 t.Errorf("the sheet's style came back as %+v", st)62 }63 if f := res.Sheet.DisplayFormat(addr(t, "C9")); f.Kind != sheet.FmtCurrency || !res.Sheet.CellStyle(addr(t, "C9")).Underline {64 t.Errorf("column C came back as %v", f.Kind)65 }66}