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

Set style not applying custom format correctly #1677

Closed
abdelaziz-ouhammou opened this issue Oct 1, 2023 · 6 comments
Closed

Set style not applying custom format correctly #1677

abdelaziz-ouhammou opened this issue Oct 1, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@abdelaziz-ouhammou
Copy link
Contributor

abdelaziz-ouhammou commented Oct 1, 2023

Go: 1.21.1
Excelize: v2.8.1-0.20230917103058-744236b4b840

this is the raw value of the cell

dateStr, err := file.GetCellValue(mainSheet, "A3")
fmt.Println(dateStr)
// 45187

applying a style using the Excel buitin formats works

dateStyle, err := file.NewStyle(&excelize.Style{
		NumFmt: 14,
})
	if err != nil {
		return EntryVLR{}, err
	}
err = file.SetCellStyle(mainSheet, "A3", "A3", dateStyle)
	if err != nil {
		return EntryVLR{}, err
}
dateStr, err := file.GetCellValue(mainSheet, "A3")
fmt.Println(dateStr)
// 09-18-23

Applying a custom format treats the value like money instead of a date

dateformat := "yyyy-mm-dd"
dateStyle, err := file.NewStyle(&excelize.Style{
		CustomNumFmt: &dateformat ,
})
	if err != nil {
		return EntryVLR{}, err
	}
err = file.SetCellStyle(mainSheet, "A3", "A3", dateStyle)
	if err != nil {
		return EntryVLR{}, err
}
dateStr, err := file.GetCellValue(mainSheet, "A3")
fmt.Println(dateStr)
// $45,187.00
@xuri
Copy link
Member

xuri commented Oct 1, 2023

Thanks for your issue. I have tested with the following code and it works well. Do you open an existing workbook or create a new workbook? Could you provide more details about this, such as an attachment to reproduce this issue.

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)
        }
    }()
    f.SetCellValue("Sheet1", "A3", 45187)
    dateformat := "yyyy-mm-dd"
    dateStyle, err := f.NewStyle(&excelize.Style{
        CustomNumFmt: &dateformat,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    err = f.SetCellStyle("Sheet1", "A3", "A3", dateStyle)
    if err != nil {
        fmt.Println(err)
        return
    }
    dateStr, err := f.GetCellValue("Sheet1", "A3")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(dateStr) // 2023-09-18
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

@xuri xuri added the needs more info This issue can't reproduce, need more info label Oct 1, 2023
@abdelaziz-ouhammou
Copy link
Contributor Author

abdelaziz-ouhammou commented Oct 1, 2023

@xuri please find the following attachments. I removed some data. Just try to read the date from A3. This file is computer generated by a third party tool.
sample.xlsx

@xuri
Copy link
Member

xuri commented Oct 1, 2023

Thanks for your feedback. I have tested it by following the code with your attachment, and it works well. Could you show us a complete, standalone example program or reproducible demo?

package main

import (
    "fmt"

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

func main() {
    f, err := excelize.OpenFile("sample.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer func() {
        if err := f.Close(); err != nil {
            fmt.Println(err)
        }
    }()
    sheet := f.GetSheetList()[1]
    dateStr, err := f.GetCellValue(sheet, "A3")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(dateStr) // 09.18.2023

    dateformat := "yyyy-mm-dd"
    dateStyle, err := f.NewStyle(&excelize.Style{
        CustomNumFmt: &dateformat,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    err = f.SetCellStyle(sheet, "A3", "A3", dateStyle)
    if err != nil {
        fmt.Println(err)
        return
    }
    dateStr, err = f.GetCellValue(sheet, "A3")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(dateStr) // 2023-09-18
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
    }
}

@abdelaziz-ouhammou
Copy link
Contributor Author

abdelaziz-ouhammou commented Oct 1, 2023

I have copied your code exactly and Got a different result.

$ go run main.go
09.18.2023
$45,187.00

Please note that opening the excel file and clicking on save seems to fix this issue.
excelize.zip

@xuri xuri added bug Something isn't working in progress Working in progress and removed needs more info This issue can't reproduce, need more info labels Oct 1, 2023
@xuri xuri closed this as completed in 1c7c417 Oct 1, 2023
@xuri
Copy link
Member

xuri commented Oct 1, 2023

I have fixed it, please try to upgrade the master branch code, and this patch will be released in the next version.

@xuri xuri removed the in progress Working in progress label Oct 1, 2023
@abdelaziz-ouhammou
Copy link
Contributor Author

I have upgraded to the latest version. The issue is indeed fixed. Thank you very much @xuri.

jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
…located

- Improve compatibility with empty custom number format code
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