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

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

Open wushiling50 opened 2 months ago

wushiling50 commented 2 months ago

Dear maintainer:
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)
    }
}

After checking the related documentation (ISO/IEC 29500) and source code, it is found that the source code has a complete implementation of langNumFmt for other regions (such as zh-tw, ja-jp), but there is no corresponding function support in the getBuiltInNumFmtCode method.

const (
    CultureNameUnknown CultureName = iota
    CultureNameEnUS
    CultureNameZhCN
    // no more methods like CultureNameZhTW
)
// ...
func (f *File) getBuiltInNumFmtCode(numFmtID int) (string, bool) {
    if fmtCode, ok := builtInNumFmt[numFmtID]; ok {
        return fmtCode, true
    }
    if isLangNumFmt(numFmtID) {
        if f.options.CultureInfo == CultureNameEnUS {
            return f.langNumFmtFuncEnUS(numFmtID), true
        }
        if f.options.CultureInfo == CultureNameZhCN {
            return f.langNumFmtFuncZhCN(numFmtID), true
        }
        // no more methods like langNumFmtFuncZhTW
    }
    return "", false
}

I know that using CustomNumFmt is also an option, but I would still like to inquire if the maintainer have plans to add support for this part, and I can do that if you are willing.

Thank you for your time and consideration.

xuri commented 1 month ago

Thanks for your issue. Yes, that would be fine, contributions are welcome. I'll certainly accept that patch if somebody did that.

wushiling50 commented 1 month ago

Thanks for your issue. Yes, that would be fine, contributions are welcome. I'll certainly accept that patch if somebody did that.

ok, i will try to do it.