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

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

Closed
tony-clariondoor opened this issue Jul 5, 2024 · 5 comments · Fixed by #1947
Labels
bug Something isn't working

Comments

@tony-clariondoor
Copy link

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

xuri commented Jul 6, 2024

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

@xuri xuri added the needs more info This issue can't reproduce, need more info label Jul 6, 2024
@tony-clariondoor
Copy link
Author

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

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 xuri added bug Something isn't working and removed needs more info This issue can't reproduce, need more info labels Jul 11, 2024
samkeke added a commit to samkeke/excelize that referenced this issue Jul 11, 2024
@xuri xuri linked a pull request Jul 11, 2024 that will close this issue
10 tasks
xuri pushed a commit that referenced this issue Jul 12, 2024
…ero numeric cell value (#1947)

- Updated unit tests
@xuri
Copy link
Member

xuri commented Jul 12, 2024

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.

@xuri xuri moved this to Bugfix in Excelize v2.9.0 Jul 12, 2024
@tony-clariondoor
Copy link
Author

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

zhangyimingdatiancai pushed a commit to zhangyimingdatiancai/excelize that referenced this issue Aug 6, 2024
… for zero numeric cell value (qax-os#1947)

- Updated unit tests
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
No open projects
Status: Bugfix
Development

Successfully merging a pull request may close this issue.

2 participants