excelize icon indicating copy to clipboard operation
excelize copied to clipboard

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

Open wushiling50 opened this issue 9 months ago • 2 comments

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.

wushiling50 avatar Apr 26 '24 11:04 wushiling50

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

xuri avatar May 12 '24 06:05 xuri

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.

wushiling50 avatar May 12 '24 11:05 wushiling50