Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wushiling50 opened this issue Apr 26, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@wushiling50
Copy link

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 xuri added the enhancement New feature or request label May 12, 2024
@xuri
Copy link
Member

xuri commented May 12, 2024

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

@wushiling50
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants