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

Add new langNumFmtFunc in numfmt #1895

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

wushiling50
Copy link

@wushiling50 wushiling50 commented May 12, 2024

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

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Signed-off-by: wushiling50 <2531010934@qq.com>
@xuri xuri added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 12, 2024
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@wushiling50
Copy link
Author

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

xuri and others added 3 commits May 13, 2024 23:14
Signed-off-by: wushiling50 <2531010934@qq.com>
Signed-off-by: wushiling50 <2531010934@qq.com>
Signed-off-by: wushiling50 <2531010934@qq.com>
@@ -3613,3 +3613,74 @@ func TestNumFmt(t *testing.T) {
assert.Equal(t, ErrUnsupportedNumberFormat, err)
assert.False(t, changeNumFmtCode)
}

func TestLangNumFmtFunc(t *testing.T) {
Copy link
Member

@xuri xuri May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test each built-in number format with different data time pattern settings on real Office application? The cells in these workbooks are formatted with built-in and built-in language related number format.

numfmt.xlsx
builtin.xlsx

We can tested by open it in spreadsheet application with different language and data time pattern settings, and get some test cases just like this:

Experimental evaluation: date time number format
Built-in number format code & built-in language number format code ID range 27 ~ 36, 50 ~ 81:

Experimental evaluation: date time number format

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, what i should do is:

  1. Place the numfmt.xlsx file inside the ./test directory.
  2. In the tests, set the langNumFmt for the cells in sequence, with each column corresponding to a different region.
  3. Retrieve the cell values and compare them to ensure they are correct.

It's not just about the new additions i have made, but also about including zh-cn and en-us as well,right?
In addition to langNumFmt, I also need to do the part of Built-in number format code, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You needn't add new workbook to the test directory, the workbook just for test, open the workbook in the real spreadsheet apps to observe formats result in different data time pattern settings, and using the formatted result as expected result in the unit test.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, thank you.

@xuri xuri added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 14, 2024
@wushiling50
Copy link
Author

  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
Copy link
Member

xuri commented May 19, 2024

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.

@xuri xuri force-pushed the master branch 2 times, most recently from 79958aa to 0c3dfb1 Compare May 25, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants