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

GetStyle does not return DecimalPlaces #1777

Closed
mlorenz-tug opened this issue Jan 8, 2024 · 1 comment
Closed

GetStyle does not return DecimalPlaces #1777

mlorenz-tug opened this issue Jan 8, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@mlorenz-tug
Copy link

Description

Currently getting a style using GetStyle does not return the previously set DecimalPlaces property. If this setting is retained in the Excel file this should also be read in the GetStyle method.

Steps to reproduce the issue:
1.

style1 := &excelize.Style{
  Font: &excelize.Font{
    Bold: true,
  },
}
x := 2
style2 := &excelize.Style{
  DecimalPlaces: &x,
  NumFmt:        2,
}

f := excelize.NewFile()

s1, _ := f.NewStyle(style1)
s2, _ := f.NewStyle(style2)

ss1, _ := f.GetStyle(s1)
ss2, _ := f.GetStyle(s2)

fmt.Printf("%+v\n", ss1)
fmt.Printf("%+v\n", ss2)

Describe the results you received:

&{Border:[] Fill:{Type: Pattern:0 Color:[] Shading:0} Font:0xc00027eb60 Alignment:<nil> Protection:<nil> NumFmt:0 DecimalPlaces:<nil> CustomNumFmt:<nil> NegRed:false}
&{Border:[] Fill:{Type: Pattern:0 Color:[] Shading:0} Font:<nil> Alignment:<nil> Protection:<nil> NumFmt:2 DecimalPlaces:<nil> CustomNumFmt:<nil> NegRed:false}

DecimalPlaces is nil in both outputs. It makes sense in the first style, since no decimal places were set there.

Describe the results you expected:

In the second output one would expect DecimalPlaces to be set to 2 (pointer to variable set to 2 to be specific).

Output of go version:

go version go1.21.5 linux/amd64

Excelize version or commit ID:

github.com/xuri/excelize/v2 v2.8.0

Environment details (OS, Microsoft Excel™ version, physical, etc.):

@xuri xuri added the bug Something isn't working label Jan 8, 2024
@xuri xuri added the in progress Working in progress label Jan 9, 2024
@xuri xuri closed this as completed in 7926565 Jan 9, 2024
@xuri
Copy link
Member

xuri commented Jan 9, 2024

Thanks for your issue. I have fixed it. Please upgrade to the master branch code. This patch will be released in the next version. In addition, there are some special cases for using the NewStyle and GetStyle functions:

package main

import (
    "fmt"

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

func main() {
    f := excelize.NewFile()
    defer func() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()

    // Example 1: DecimalPlaces doesn't work if we have specified NumFmt with
    // built-in all languages formats or built-in language
    decimalPlaces := 4
    styleID, err := f.NewStyle(&excelize.Style{
        NumFmt:        2, // 0.00
        DecimalPlaces: &decimalPlaces,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    style, err := f.GetStyle(styleID)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("DecimalPlaces: %d\r\n", *style.DecimalPlaces)
    // DecimalPlaces: 2

    // Example 2: DecimalPlaces doesn't work if we have specified CustomNumFmt
    customNumFmt := "0.0"
    styleID, err = f.NewStyle(&excelize.Style{
        CustomNumFmt:  &customNumFmt,
        DecimalPlaces: &decimalPlaces,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    style, err = f.GetStyle(styleID)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("DecimalPlaces: %d\r\n", *style.DecimalPlaces)
    // DecimalPlaces: 1

    // Example 3: get style definition by the GetStyle or GetConditionalStyle
    // function, the DecimalPlaces is nil if a number format code has the
    // different decimal places in the positive part and negative part
    customNumFmt = "0.0;0.00"
    styleID, err = f.NewStyle(&excelize.Style{
        CustomNumFmt: &customNumFmt,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    style, err = f.GetStyle(styleID)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("DecimalPlaces: %v\r\n", style.DecimalPlaces)
    // DecimalPlaces: <nil>

    // Example 4: get style definition by the GetStyle or GetConditionalStyle
    // function, the DecimalPlaces is nil if a number format code only have the
    // negative part
    customNumFmt = ";0.00"
    styleID, err = f.NewStyle(&excelize.Style{
        CustomNumFmt: &customNumFmt,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    style, err = f.GetStyle(styleID)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("DecimalPlaces: %v\r\n", style.DecimalPlaces)
    // DecimalPlaces: <nil>

    // Example 5: get style definition by the GetStyle or GetConditionalStyle
    // function, the NumFmt is not equal with the value when create the style
    decimalPlaces = 4
    styleID, err = f.NewStyle(&excelize.Style{
        NumFmt:        165,            // [$$-409]#,##0.00
        DecimalPlaces: &decimalPlaces, // change [$$-409]#,##0.00 to [$$-409]#,##0.0000
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    style, err = f.GetStyle(styleID)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("NumFmt: %d\r\n", style.NumFmt)
    // NumFmt: 0
}

@xuri xuri removed the in progress Working in progress label Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants