Skip to content

Out of range in RemoveRow() #437

@artsb

Description

@artsb

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

Activity

xuri

xuri commented on Jul 16, 2019

@xuri
Member

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

artsb

artsb commented on Jul 18, 2019

@artsb
Author

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 commented on Jul 20, 2019

@xuri
Member

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

artsb

artsb commented on Jul 21, 2019

@artsb
Author

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

added a commit that references this issue on Oct 23, 2020
89c3bb9
added a commit that references this issue on Mar 14, 2024
855c360
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedThis issue can be reproduced

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @xuri@artsb

        Issue actions

          Out of range in RemoveRow() · Issue #437 · qax-os/excelize