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

Bug:Version v3 appendsheet is empty from the second sheet, v1.0.5 is normal #783

Closed
bondliu2020 opened this issue Oct 4, 2023 · 6 comments · Fixed by #786
Closed

Bug:Version v3 appendsheet is empty from the second sheet, v1.0.5 is normal #783

bondliu2020 opened this issue Oct 4, 2023 · 6 comments · Fixed by #786

Comments

@bondliu2020
Copy link

Bug:
Version v3 appendsheet is empty from the second sheet,but v1.0.5 is normal.

@bondliu2020
Copy link
Author

I tested v2 and it was normal. V3 is error.
wb.AppendSheet(*sheet, sheetName)

@tealeg
Copy link
Owner

tealeg commented Oct 13, 2023

@bondliu2020 how are you constructing the Sheet that you're passing to AppendSheet? The CellStores got added in V3 IIRC and you might have an issue around those.

@bondliu2020
Copy link
Author

func main() {
var out string
var inPath string
flag.StringVar(&inPath, "d", "tmp", "指定需要合并的工作薄目录")
flag.StringVar(&out, "o", "out.xlsx", "指定合并后文件的命名,格式:out.xlsx")
flag.Parse()
//设置路径,文件夹放在main的同级目录下
PthSep := string(os.PathSeparator)
PthDir := inPath

dir, err := os.ReadDir(PthDir)
if err != nil {
	fmt.Printf("open dir failed: %s\n", err.Error())
}

//申明合并后的文件
var newFile *xlsx.File
newFile = xlsx.NewFile()
sCount := 0
var sheetName string
var newErr error
//检索所有文件
for _, fi := range dir {
	fmt.Printf("open success: %s\n", PthDir+PthSep+fi.Name())
	if newErr != nil {
		fmt.Printf(newErr.Error())
	}
	//循环读取每个文件
	xlFile, err := xlsx.OpenFile(PthDir + PthSep + fi.Name())
	if err != nil {
		fmt.Printf("open failed: %s\n", err)
	}
	sCount++
	for _, sheet := range xlFile.Sheets {
		fmt.Printf("Sheet Name: %s\n", sheet.Name)
		//
		sheetName = "sheet" + strconv.Itoa(sCount)
		fmt.Println("marrow", sheet.MaxRow, "合并后sheetName:", sheetName)
		_, newErr := newFile.AppendSheet(*sheet, sheetName)
		if newErr != nil {
			fmt.Printf(newErr.Error())
		}
	}
}

//写入文件
newErr = newFile.Save(out)
if newErr != nil {
	fmt.Printf(newErr.Error())
} else {
	fmt.Printf("全部文件合并完成.")
}

}

@bondliu2020
Copy link
Author

@bondliu2020 how are you constructing the Sheet that you're passing to AppendSheet? The CellStores got added in V3 IIRC and you might have an issue around those.

Above is all the code.

@tealeg
Copy link
Owner

tealeg commented Oct 17, 2023

@bondliu2020 - thank you, I'll try and find time to look at this soon.

@tealeg
Copy link
Owner

tealeg commented Oct 24, 2023

OK @bondliu2020 - I now know why this is happening. When you append the sheets you give them new sheet names (perfectly correct!), but the backing row store uses the sheet name as part of its key, so it can no longer find the rows by name (even though it has all the data still). I'll try to work up a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants