qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
18.05k stars 1.71k forks source link

Cell formatted as percentage does not return percent sign using GetCellValue if value is zero #1942

Closed tony-clariondoor closed 2 months ago

tony-clariondoor commented 3 months ago

Description When reading a spreadsheet I am having problems getting the percent sign to show up for a cell that is formatted as Percentage when the value is zero. I had this failure with 0, 0.0, 0.00, etc. I am hoping that I am just doing something wrong in my code.

Steps to reproduce the issue: See Below

Describe the results you received: In this snippet of code here I am checking two cells that are formatted in Excel as Percentage:

if cellID == "B214" || cellID == "B213" {
    value, err := File.GetCellValue(sheetName, cellID, excelize.Options{RawCellValue: false})
    if err != nil {
        log.Printf("error getting cell value: %s", err)
    }
    testValue, _ := xlsx.File.GetCellStyle(sheetName, cellID)
    log.Printf("Cell %s style: %+v -- Value: %v\n", cellID, testValue, value)
}

and I get these results:

test-percent  | 2024/07/05 13:38:53 Cell B213 style: 10 -- Value: 0
test-percent  | 2024/07/05 13:38:53 Cell B214 style: 10 -- Value: 9%

When I set RawCellValue to true, I get these results:

test-percent  | 2024/07/05 13:42:47 Cell B213 style: 10 -- Value: 0.00
test-percent  | 2024/07/05 13:42:47 Cell B214 style: 10 -- Value: 0.09

Describe the results you expected: I expected the percent to be displayed when I get the cell value without the RawCellValue option set to true.

Output of go version:

go version go1.22.4 darwin/arm64

Excelize version or commit ID:

2.8.1

Environment details (OS, Microsoft Excel™ version, physical, etc.): MacOS is where I performed this test, but it also fails in different docker containers (Alpine Linux, Debian). Excel document was created in version 16.86, but other versions failed as well.

xuri commented 3 months ago

Thanks for your feedback. Could you provide the workbook as file attachment without confidential info?

tony-clariondoor commented 2 months ago

That workbook is fairly long and complex, so I tried to recreate it using a fresh .xlsx file and I got the same results.

Using this code:

package main

import (
    "fmt"

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

func main() {
    f, err := excelize.OpenFile("test.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    sheetName := f.GetSheetName(f.GetActiveSheetIndex())

    rows, err := f.GetRows(sheetName)
    if err != nil {
        fmt.Println(err)
        return
    }

    for rowNumber, row := range rows {
        for columnPosition := range row {
            cellID, _ := excelize.CoordinatesToCellName(columnPosition+1, rowNumber+1)
            cellStyle, _ := f.GetCellStyle(sheetName, cellID)
            colCell, _ := f.GetCellValue(sheetName, cellID)
            rawCell, _ := f.GetCellValue(sheetName, cellID, excelize.Options{RawCellValue: true})
            fmt.Printf("Cell ID: %s -- Cell Styling: %v -- Cell Value: %s -- Raw Cell Value: %s\n", cellID, cellStyle, colCell, rawCell)
        }
    }

    if err := f.Close(); err != nil {
        fmt.Println(err)
    }
}

on the attached workbook

test.xlsx

I got the following results:

Cell ID: A1 -- Cell Styling: 4 -- Cell Value: 0 -- Raw Cell Value: 0
Cell ID: A2 -- Cell Styling: 3 -- Cell Value: 0 -- Raw Cell Value: 0
Cell ID: A3 -- Cell Styling: 1 -- Cell Value: 0 -- Raw Cell Value: 0
Cell ID: A4 -- Cell Styling: 2 -- Cell Value: 0 -- Raw Cell Value: 0
Cell ID: A5 -- Cell Styling: 4 -- Cell Value: 0% -- Raw Cell Value: 0.00001
Cell ID: A6 -- Cell Styling: 1 -- Cell Value: 1.23% -- Raw Cell Value: 0.01234
Cell ID: A7 -- Cell Styling: 1 -- Cell Value: 12.34% -- Raw Cell Value: 0.1234
Cell ID: A8 -- Cell Styling: 1 -- Cell Value: 123.40% -- Raw Cell Value: 1.234
Cell ID: A9 -- Cell Styling: 1 -- Cell Value: 1234.00% -- Raw Cell Value: 12.34

which leads me to believe that either I am getting the cell value wrong or there is something in there that is not liking the idea of dividing by zero so it is just returning a raw zero.

tony-clariondoor commented 2 months ago

Additionally, I did check the loop for an error condition on any of the cell actions, and none of them produced errors, so I removed that check in the code for the sake of brevity.

xuri commented 2 months ago

Thanks for your issue. This issue has been fixed, please upgrade to the master branch code by go get -u github.com/xuri/excelize/v2@master, and this patch will be released in the next version.

tony-clariondoor commented 2 months ago

Thank you very much! I'll pull this down and test it out.