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

RemoveRow can not remove the last row|RemoveRow 无法删除最后一行 #686

Closed
liserjrqlxue opened this issue Aug 11, 2020 · 4 comments
Closed

Comments

@liserjrqlxue
Copy link
Contributor

Description

RemoveRow can not remove the last row|RemoveRow 无法删除最后一行:

  1. RemoveRow(sheetName,0) report error: invalid row number 0 []
  2. RemoveRow(sheetName,i+1) remove the i+1 th row as rows[i] from GetRows(sheetName)
  3. RemoveRow(sheetName,len(row)) report error : slice bounds out of range [94:93] (94:93 is example)

Steps to reproduce the issue:

  1. var excel,err1=excelize.OpenFile(excelFile)
  2. var rows,err2=excel.GetRows(sheetName)
  3. excel.RemoveRow(sheetName,len(rows))

Describe the results you received:

panic: runtime error: slice bounds out of range [94:93]

goroutine 1 [running]:
github.com/360EntSecGroup-Skylar/excelize/v2.(*File).adjustHyperlinks(0xc000126500, 0xc00007c000, 0x5a4327, 0x11, 0x1, 0x11, 0xffffffffffffffff)
	/home/wangyaoshen/go/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/v2@v2.2.0/adjust.go:117 +0x5a1
github.com/360EntSecGroup-Skylar/excelize/v2.(*File).adjustHelper(0xc000126500, 0x5a4327, 0x11, 0xc00007c001, 0x11, 0xffffffffffffffff, 0xc0000100f0, 0x2)
	/home/wangyaoshen/go/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/v2@v2.2.0/adjust.go:45 +0xdd
github.com/360EntSecGroup-Skylar/excelize/v2.(*File).RemoveRow(0xc000126500, 0x5a4327, 0x11, 0x11, 0x4, 0x0)
	/home/wangyaoshen/go/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/v2@v2.2.0/rows.go:436 +0x199
main.main()
	/zfsyt1/B2C_RD_P2/USER/wangyaoshen/pipeline/filterExcel/main.go:146 +0x13dc

Describe the results you expected:

RemoveRow(sheetName,len(rows)) remove the len(rows) th row (the last row) as rows[len(rows)-1]

Output of go version:

go version go1.14.3 linux/amd64

Excelize version or commit ID:

v2.2.0

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

@liserjrqlxue
Copy link
Contributor Author

I add a fix #687

@xuri
Copy link
Member

xuri commented Aug 12, 2020

Hi @liserjrqlxue, thanks for your issue and contribution. Could you provide a spreadsheet xlsx attachment without confidential info? I need to investigate more details about this issue.

@liserjrqlxue
Copy link
Contributor Author

liserjrqlxue commented Aug 13, 2020

debug.xlsx

I wrote a code to build test xlsx and reproduce this issue:

package main

import (
	"fmt"

	"github.com/360EntSecGroup-Skylar/excelize/v2"
	"github.com/liserjrqlxue/goUtil/simpleUtil"
)

func main() {
	var excel =excelize.NewFile()
	var sheet1= excel.GetSheetName(0)
	simpleUtil.CheckErr(excel.SetCellValue(sheet1,"A1",""))
	simpleUtil.CheckErr(excel.SetCellValue(sheet1,"A2",""))
	simpleUtil.CheckErr(excel.SetCellValue(sheet1,"B1",""))
	simpleUtil.CheckErr(excel.SetCellValue(sheet1,"B2",""))
	simpleUtil.CheckErr(excel.SetCellHyperLink(sheet1,"A1","","External"))
	simpleUtil.CheckErr(excel.SetCellHyperLink(sheet1,"A2","","External"))
	simpleUtil.CheckErr(excel.SetCellHyperLink(sheet1,"B2","","External"))
	simpleUtil.CheckErr(excel.SaveAs("debug.xlsx"))
	var rows,err= excel.GetRows(sheet1)
	simpleUtil.CheckErr(err)
	fmt.Printf("len(rows)=%d\n",len(rows))
	simpleUtil.CheckErr(excel.RemoveRow(sheet1,len(rows)))
}

After build and run, result may be like below:
捕获

I also wrote a test code which you can run in this repo use go test -run TestRemoveRowDebug:

func TestRemoveRowDebug(t *testing.T){
	f := NewFile()
	sheet1 := f.GetSheetName(0)
	assert.NoError(t, f.SetCellValue(sheet1,"A1",""))
	assert.NoError(t, f.SetCellValue(sheet1,"A2",""))
	assert.NoError(t, f.SetCellValue(sheet1,"B1",""))
	assert.NoError(t, f.SetCellValue(sheet1,"B2",""))
	assert.NoError(t, f.SetCellHyperLink(sheet1,"A1","","External"))
	assert.NoError(t, f.SetCellHyperLink(sheet1,"A2","","External"))
	assert.NoError(t, f.SetCellHyperLink(sheet1,"B2","","External"))
	assert.NoError(t, f.RemoveRow(sheet1,2))
}

The result may be like below:
捕获

xuri pushed a commit that referenced this issue Aug 14, 2020
@xuri
Copy link
Member

xuri commented Aug 14, 2020

Thanks for your PR.

@xuri xuri closed this as completed Aug 14, 2020
nullfy pushed a commit to nullfy/excelize that referenced this issue Oct 23, 2020
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

No branches or pull requests

2 participants