qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
17.62k stars 1.68k forks source link

Add new langNumFmtFunc in `numfmt` #1895

Open wushiling50 opened 1 month ago

wushiling50 commented 1 month ago

PR Details

Support for more built-in langNumFmt allows GetCellValue to fetch dates and times in more localizations

Description

Related Issue

1885

Motivation and Context

When I using the following code, I found that the current CultureInfo type only supports CultureNameZhCN and CultureNameEnUS, which makes it impossible for me to obtain the time and date formats of other regions through the GetCellValue method.

package main

import (
    "fmt"

    "github.com/xuri/excelize/v2"
)

func main() {
    f := excelize.NewFile(excelize.Options{
        CultureInfo: excelize.CultureNameZhCN, // or excelize.CultureNameEnUS
    })

    style1, err := f.NewStyle(&excelize.Style{
        NumFmt: 27,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    f.SetCellStyle("Sheet1", "A2", "A2", style1)
    f.SetCellValue("Sheet1", "A2", 45405)

    date, err := f.GetCellValue("Sheet1", "A2")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(date)

    if err := f.SaveAs("./Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

How Has This Been Tested

Add Unit Test

Types of changes

Checklist

wushiling50 commented 1 month ago

Thanks for your PR. Could you add unit test for this changes?

No problem, it was my oversight, I'll add unit tests for these changes

wushiling50 commented 1 month ago
  1. The results of parsing expressions with "e" in Excel and Exelize are different: demo:

    func main() {
    f := excelize.NewFile()
    
    numfmt1 := "e\"年\"m\"月\"d\"日\""
    style1, err := f.NewStyle(&excelize.Style{
        CustomNumFmt: &numfmt1,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    f.SetCellStyle("Sheet1", "A2", "A2", style1)
    f.SetCellValue("Sheet1", "A2", 45050)
    cellValue1, _ := f.GetCellValue("Sheet1", "A2")
    fmt.Printf("1:%v\n", cellValue1)
    
    numfmt2 := "yyyy\"年\"m\"月\"d\"日\""
    style2, err := f.NewStyle(&excelize.Style{
        CustomNumFmt: &numfmt2,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    f.SetCellStyle("Sheet1", "B2", "B2", style2)
    f.SetCellValue("Sheet1", "B2", 45050)
    cellValue2, _ := f.GetCellValue("Sheet1", "B2")
    fmt.Printf("2:%v\n", cellValue2)
    
    numfmt3 := "[$-411]ggge\"年\"m\"月\"d\"日\""
    style3, err := f.NewStyle(&excelize.Style{
        CustomNumFmt: &numfmt3,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    f.SetCellStyle("Sheet1", "C2", "C2", style3)
    f.SetCellValue("Sheet1", "C2", 45050)
    
    cellValue3, _ := f.GetCellValue("Sheet1", "C2")
    fmt.Printf("3:%v\n", cellValue3)
    
    if err := f.SaveAs("demo/Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
    }

    Results in Excel:
    Excel

Results in Exelize:
Excelize

  1. Not supported for Thai language parsing
    demo:

    func main() {
    f := excelize.NewFile()
    
        numfmt4 := "ว-ดดด-ปป"
    style4, err := f.NewStyle(&excelize.Style{
        CustomNumFmt: &numfmt4,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    f.SetCellStyle("Sheet1", "D2", "D2", style4)
    f.SetCellValue("Sheet1", "D2", 45050)
    
    cellValue4, _ := f.GetCellValue("Sheet1", "D2")
    fmt.Printf("4:%v\n", cellValue4)
    
        if err := f.SaveAs("demo/Book1.xlsx"); err != nil {
        fmt.Println(err)
    }

    Results in Excel:
    Excel-Thai

Results in Exelize:
Thai

These problems are leading to the failure of the "zh-tw" and "th-th" related tests, and if you are agreeable, I can provide the code for the "ja-jp" and "ko-kr" parts first, and within the getBuiltInNumFmtCode function, I can set a TODO comment. Both "ja-jp" part and ko-kr" part have passed the tests successfully.

xuri commented 1 month ago

Good job. I think we need to resolve this number format code parse or evaluate the issue before merging this. Maybe it will be related to the NFP library, I'm not sure.