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

SetCellValue with time.Time results in Excel "We found a problem with some content" #1464

Closed
koodimetsa opened this issue Feb 6, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@koodimetsa
Copy link

koodimetsa commented Feb 6, 2023

When converting a cell value to date(time.Time) the resulting file doesn't work in Excel.

Steps to reproduce the issue:

  1. Open a file with Excelice
  2. Read a cell value and convert it to time.Time. Then save the resulting time to cell.
  3. Save file and try to open it with Excel.

Describe the results you received:
Excel warns: "We found a problem with some content."

Describe the results you expected:
Valid xlsx-file.

Output of go version:

go version go1.20 darwin/amd64

Excelize version or commit ID:

v.2.7.0

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

OS: MacOS Ventura 13.2

Code to reproduce

package main

import (
	"fmt"
	"log"
	"strconv"
	"time"

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

func main() {
	f, err := excelize.OpenFile("_tests/test.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		// Close the spreadsheet.
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	const column = "A"
	const sheet = "Sheet1"
	row := 2

	for {
		cell := column + strconv.Itoa(row)

		value, err := f.GetCellValue(sheet, cell)
		if err != nil {
			log.Println(err)
			return
		}
		if value == "" {
			break
		}

		date, err := time.Parse("January 02 2006", value)
		if err != nil {
			log.Println(err)
                        row++
			continue
		}

		if err := f.SetCellValue(sheet, cell, date); err != nil {
			log.Println(err)
			return
		}

		row++
	}

	err = f.SaveAs("_tests/testDateFixed.xlsx")
	if err != nil {
		log.Println(err)
		return
	}
}

ps. Thank you for an amazing library!

@koodimetsa
Copy link
Author

test.xlsx
Example test-file.

@xuri xuri added bug Something isn't working in progress Working in progress labels Feb 6, 2023
@xuri xuri closed this as completed in 3e24060 Feb 6, 2023
@xuri
Copy link
Member

xuri commented Feb 6, 2023

Thanks for your issue. I have fixed it. Please try to upgrade to the master branch code, and this patch will be released in the next version.

@xuri xuri removed the in progress Working in progress label Feb 6, 2023
@koodimetsa
Copy link
Author

Thank you for the fix!

@xuri xuri mentioned this issue Mar 25, 2023
xuri added a commit to JDavidVR/excelize that referenced this issue Jul 11, 2023
- Support creating a conditional format with a "stop if true" rule
- Support set border color and create solid color for the color data bar
- Fix incorrect cell type when modifying string cell with the time number
- Update unit test for the add pivot table to avoid pivot table range overlap
jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
- Support creating a conditional format with a "stop if true" rule
- Support set border color and create solid color for the color data bar
- Fix incorrect cell type when modifying string cell with the time number
- Update unit test for the add pivot table to avoid pivot table range overlap
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