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 / xlsxlocale_test.go
xlsxlocale_test.go 1.9K
1package fileio23import (4 "context"5 "path/filepath"6 "testing"78 "src.rest/cat/012/internal/locale"9 "src.rest/cat/012/internal/sheet"10)1112// TestXLSXLocaleFormats checks a workbook in de-DE is downloaded with13// its Currency and Date formats in de-DE's codes, which read back as14// those formats in de-DE, and keep their euro and day-first order as15// patterns in a workbook in en-US.16func TestXLSXLocaleFormats(t *testing.T) {17 src := build(t, map[string]string{"A1": "$1,234.50", "B1": "9/26/2026", "C1": "14:30:00", "D1": "$5"})18 if !src.Book().SetLocale("de-DE") {19 t.Fatal("no de-DE")20 }21 r := func(s string) sheet.Rect { return sheet.NewRect(addr(t, s), addr(t, s)) }22 src.SetFormat(r("C1"), sheet.Format{Kind: sheet.FmtTime})23 de, _ := locale.Lookup("de-DE")24 for f, want := range map[sheet.Format]string{25 {Kind: sheet.FmtCurrency, Decimals: 2}: `#,##0.00\ [$€-407]`,26 {Kind: sheet.FmtDate}: "[$-407]d.m.yyyy",27 {Kind: sheet.FmtTime}: "[$-407]hh:mm:ss",28 } {29 if got := excelCodeIn(f, de); got != want {30 t.Errorf("%+v in de-DE = %q, want %q", f, got, want)31 }32 }33 name := filepath.Join(t.TempDir(), "de.xlsx")34 if _, err := Export(context.Background(), name, XLSX, Snap(src, sheet.Rect{}, "De"), ExportOptions{}); err != nil {35 t.Fatal(err)36 }37 got, err := Import(context.Background(), name, Options{Locale: de})38 if err != nil {39 t.Fatal(err)40 }41 for _, cell := range []string{"A1", "B1", "C1", "D1"} {42 a := addr(t, cell)43 if g, w := got.Sheet.Cell(a).Format, src.Cell(a).Format; g != w {44 t.Errorf("de-DE %s format %+v, want %+v", cell, g, w)45 }46 }47 got, err = Import(context.Background(), name, Options{Locale: locale.Canonical})48 if err != nil {49 t.Fatal(err)50 }51 for cell, want := range map[string]string{"A1": "1,234.50 €", "B1": "26.9.2026"} {52 a := addr(t, cell)53 c := got.Sheet.Cell(a)54 if g := sheet.FormatText(c.Value, c.Format); g != want {55 t.Errorf("en-US %s shows %q, want %q", cell, g, want)56 }57 }58}