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

Out of range in RemoveRow() #437

Closed
artsb opened this issue Jul 13, 2019 · 4 comments
Closed

Out of range in RemoveRow() #437

artsb opened this issue Jul 13, 2019 · 4 comments
Labels
confirmed This issue can be reproduced

Comments

@artsb
Copy link

artsb commented Jul 13, 2019

Description

I have complicated accounting document with many rows. I was update package Excelize.
And now, if i calling RemoveRow() in cycle, i receive an error - out of range.
Error location:

RemoveRow() -> adjustHelper() -> adjustMergeCells() -> deleteMergeCell()

Row:

sheet.MergeCells.Cells = append(sheet.MergeCells.Cells[:idx], sheet.MergeCells.Cells[idx+1:]...)

I think you need to replace row:
for i, areaData := range xlsx.MergeCells.Cells {
with:
for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
in method adjustMergeCells(). And decrement index after each calling method deleteMergeCell().

Like this:

func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
	if xlsx.MergeCells == nil {
		return nil
	}

	// for i, areaData := range xlsx.MergeCells.Cells { // ---
	for i := 0; i < len(xlsx.MergeCells.Cells); i++ { // +++
		areaData := xlsx.MergeCells.Cells[i] // +++
		coordinates, err := f.areaRefToCoordinates(areaData.Ref)
		if err != nil {
			return err
		}
		x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
		if dir == rows {
			if y1 == num && y2 == num && offset < 0 {
				f.deleteMergeCell(xlsx, i)
				i-- // +++
			}
			y1 = f.adjustMergeCellsHelper(y1, num, offset)
			y2 = f.adjustMergeCellsHelper(y2, num, offset)
		} else {
			if x1 == num && x2 == num && offset < 0 {
				f.deleteMergeCell(xlsx, i)
				i-- // +++
			}
			x1 = f.adjustMergeCellsHelper(x1, num, offset)
			x2 = f.adjustMergeCellsHelper(x2, num, offset)
		}
		if x1 == x2 && y1 == y2 {
			f.deleteMergeCell(xlsx, i)
			i-- // +++
		}
		if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
			return err
		}
	}
	return nil
}

This was solve problem for me. But i did not go deep into solving this problem.

Excelize version or commit ID:
last update

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

@xuri
Copy link
Member

xuri commented Jul 16, 2019

Hi @artsb, thanks for your feedback. Please provide code and testing file attachment if you can.

@artsb
Copy link
Author

artsb commented Jul 18, 2019

upd.xlsx

Template code:
dealElems - slice with data.

dealElemIndex := -1

rows, err := xlsx.Rows(sheetName)
if err != nil {
	return err
}

for rows.Next() {
	startCell, err := xlsx.GetCellValue(sheetName, cursor.getAxis())
	if err != nil {
		return err
	}

	if startCell == "{{#EOS}}" {
		xlsx.SetCellValue(sheetName, cursor.getAxis(), "")
		break
	}

	if strings.Contains(startCell, "{{#rowsDE}}") {
		dealElemIndex++
		if dealElemIndex >= len(dealElems) {
			err = xlsx.RemoveRow(sheetName, cursor.row+1) // <-- ERROR
			if err != nil {
				return err
			}
			continue
		}
	}

	// Do Work...
}

@xuri xuri added confirmed This issue can be reproduced in progress Working in progress labels Jul 20, 2019
@xuri xuri closed this as completed in 855c360 Jul 20, 2019
@xuri xuri removed the in progress Working in progress label Jul 20, 2019
@xuri
Copy link
Member

xuri commented Jul 20, 2019

Hi @artsb, thanks for your issue. I have fixed it. Please try to upgrade the library with the master branch code.

@artsb
Copy link
Author

artsb commented Jul 21, 2019

I'm glad to help you ;) Thanks for your work!

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
confirmed This issue can be reproduced
Projects
None yet
Development

No branches or pull requests

2 participants