qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets

Home Page:https://xuri.me/excelize

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setting Cell to NaN leads to "problem with some content" dialog in Excel

MattBrittan opened this issue · comments

Description

Setting a cell value to NaN (i.e. math.NaN()) leads to an error when the resulting file is opened in Excel.

Note: Old issue #119 looks pretty much identical, xuri mentioned the library handled this OK in xlsx files but that does not appear to be the case (or an issue has been introduced in the interim). Excel does not really support NaN values so the solution to this is not obvious.

Steps to reproduce the issue:

Run code below:

package main

import (
	"fmt"
	excelize "github.com/xuri/excelize/v2"
	"math"
)

func main() {
	f := excelize.NewFile()

	if err := f.SetCellValue("Sheet1", "A1", math.NaN()); err != nil {
		panic(err)
	}

	if err := f.SaveAs("Book1.xlsx"); err != nil {
		fmt.Println(err)
	}
}

Describe the results you received:.

Error when opening resulting file in Excel.

Excel: We found a problem dialog

Excel : removed unreadable content

Describe the results you expected:

Either:

  1. An error from SetCellValue
  2. Excel file that opens and the cell contains either (not a perfect option here as Excel does not really support NaN)
    • The text NaN
    • #N/A error (perhaps =NA() formula).

I wonder if the first option is better as that leaves it up to the developer to resolve this.

Output of go version:

go version go1.22.4 windows/amd64

Excelize version or commit ID:

v2.8.1

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

Windows 11, Excel for Microsoft 365 (Version 2406 build 16.0.17726.20078) 64-Bit

commented

Thanks for your feedback. This issue has been fixed, now cell value will be represent in string or inline string for any IEEE 754 "not-a-number" value or infinity numeric. 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.