| 1 | package xlsx_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/xuri/excelize/v2" |
| 8 | |
| 9 | "amadan.net/oficina/sheets/internal/sheets/format" |
| 10 | "amadan.net/oficina/sheets/internal/sheets/xlsx" |
| 11 | ) |
| 12 | |
| 13 | // Synthetic workbook: retain Google's currency code and numeric value |
| 14 | // across import/export, while displaying the symbol rather than its syntax. |
| 15 | func TestImportedGoogleCurrencyDisplayAndRoundTrip(t *testing.T) { |
| 16 | f := excelize.NewFile() |
| 17 | defer f.Close() |
| 18 | code := `[$€]#,##0.00` |
| 19 | style, err := f.NewStyle(&excelize.Style{CustomNumFmt: &code}) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if err := f.SetCellValue("Sheet1", "A1", 1234.5); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | if err := f.SetCellStyle("Sheet1", "A1", "A1", style); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | buf, err := f.WriteToBuffer() |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | first := importReader(t, buf) |
| 34 | var exported bytes.Buffer |
| 35 | if err := xlsx.Export(first, &exported); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | second := importReader(t, &exported) |
| 39 | if diff := diffTabs(t, first, second); diff != "" { |
| 40 | t.Fatalf("currency round trip changed cells: %s", diff) |
| 41 | } |
| 42 | styleOut := formatOf(t, second, 0, 0) |
| 43 | if styleOut.NumberFmt != code { |
| 44 | t.Fatalf("currency format changed: %q", styleOut.NumberFmt) |
| 45 | } |
| 46 | row, col, ok := cellAt(second, 0, 0) |
| 47 | if !ok { |
| 48 | t.Fatal("missing imported cell") |
| 49 | } |
| 50 | value := second.Value(row, col) |
| 51 | if value.Num != 1234.5 || format.Display(value, styleOut.NumberFmt) != "€1,234.50" { |
| 52 | t.Fatalf("currency value or display changed: %+v", value) |
| 53 | } |
| 54 | } |
| 55 | |