Skip to content

CalcCellValue does not always calculate percentages correctly #993

@stanim

Description

@stanim

Description

Percentages in spreadsheets causes two kind of errors for CalcCellValue:

  1. In calc_test.go it ignores the percentage char.
  2. With a spreadsheet made with WPS Office it causes a parsing error. It works fine with a spreadsheet made with LibreOffice.

Percentage char is ignored in calc_test.go

Steps to reproduce the issue:

  1. Add percentage calculation to calc_test.go, see :
	mathCalc := map[string]string{
                ...
		"=15%":       "0.0015",
		"=1+20%":     "1.0020",

I've done this in my percentage branch: https://github.com/stanim/excelize/blob/percentage/calc_test.go#L51

  1. Run go test.

Describe the results you received:

> go test 
--- FAIL: TestCalcCellValue (8.46s)
    calc_test.go:1218: 
        	Error Trace:	calc_test.go:1218
        	Error:      	Not equal: 
        	            	expected: "0.0015"
        	            	actual  : "15"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-0.0015
        	            	+15
        	Test:       	TestCalcCellValue
        	Messages:   	=15%
    calc_test.go:1218: 
        	Error Trace:	calc_test.go:1218
        	Error:      	Not equal: 
        	            	expected: "1.0020"
        	            	actual  : "21"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-1.0020
        	            	+21
        	Test:       	TestCalcCellValue
        	Messages:   	=1+20%

Describe the results you expected:

I expected the tests to pass.

Percentage causes parsing error with WPS Office

Steps to reproduce the issue:

  1. Download percentage.zip and unzip it

  2. Run the go file: go run ./percentage.go

Describe the results you received:

> go run ./percentage.go
percentage_libre.xlsx
15 

percentage_wps.xlsx
2021/08/18 18:59:17 strconv.ParseFloat: parsing "15%": invalid syntax
exit status 1

Describe the results you expected:

I expected the same correct result as percentage_libre.xlsx for percentage_wps.xlsx without parsing error.

Excelize version or commit ID:

> git log
commit 07e84b89cc080921f19c10f93f70a6b33ed6b7db (HEAD -> percentage, origin/percentage)
Author: Stani Michiels <[email protected]>
Date:   Wed Aug 18 13:19:07 2021 +0200

    Add percentage test to calc test

commit a55f354eb3d0c6c1b9a543ff8ff98227aa6063a6 (origin/master, origin/HEAD, master)
Author: xuri <[email protected]>
Date:   Tue Aug 17 00:01:44 2021 +0800

    This closes #989, closes #990
    
    New API: `SetRowStyle` support for set style for the rows
    Update documentation for the `GetRows`, `SetCellStyle` and `SetColStyle`

Configuration

Output of go version:

go version go1.16.7 linux/amd64

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

  • OS: Archlinux 5.13.10-arch1-1
  • WPS Spreadsheets (version wps-office 11.1.0.10702-1)
  • LibreCalc (version libreoffice-fresh 7.1.5-2)

Where should this be fixed? Would you accept a pull request? Maybe I can help if you point me to right source code. Should pull requests be made against the master or v2 branch?
percentage.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions