Skip to content

[BUG] spliceRows doesn't work on the last row #2125

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

Closed
ak99372 opened this issue Aug 30, 2022 · 4 comments
Closed

[BUG] spliceRows doesn't work on the last row #2125

ak99372 opened this issue Aug 30, 2022 · 4 comments

Comments

@ak99372
Copy link

ak99372 commented Aug 30, 2022

🐛 Bug Report

Removing rows in worksheet with spliceRows works except for the last row.

Lib version: 4.3.0

Steps To Reproduce

worksheet.spliceRows(worksheet.rowCount, 1)

The expected behaviour:

Last row to be removed

Is there another way to delete/remove (last) row than using spliceRow?

@MCSulphate
Copy link

Hey! I've been having this issue among others recently, I have found/developed a workaround for this. You can use this function to delete rows from a worksheet:

function deleteRows(worksheet, start, count, rowCount) {
	for (let i = 0; i < count; i++) {
		if (rowCount && start + i === rowCount) {
			// Workaround fix for deleting the last row.
			worksheet.spliceRows(start, 1, []);
		}
		else {
			worksheet.spliceRows(start, 1);
		}
	}
}

With this, rowCount is optional, pass it in if you may be deleting the last row - it will detect when the last row is being deleted; the workaround is to insert a blank row in place of the last row (which works!). I found that deleting multiple rows at once doesn't usually work, so my function deletes them one at a time.

Hope this helps.

@k8w
Copy link

k8w commented Feb 7, 2023

Still exists at exceljs@4.3.0

@blue32a
Copy link

blue32a commented Apr 4, 2023

I have the same problem. exceljs@4.3.0

@blue32a
Copy link

blue32a commented Apr 4, 2023

This code has allowed me to work around the problem.
Note, however, that worksheet.rowCount will not be reduced because a blank row is inserted at the end.

test('rowCount will not be reduced', () => {
  const wb = new ExcelJS.workbook();
  const ws = wb.addWorksheet();
  ws.addRows([['1st'], ['2nd'], ['3rd']);
  
  deleteRows(ws, 2, 2, ws.rowCount);
  
  expect(ws.rowCount).not.toBe(1);
});
function deleteRows(worksheet, start, count, rowCount) {
	for (let i = 0; i < count; i++) {
		if (rowCount && start + i === rowCount) {
			// Workaround fix for deleting the last row.
			worksheet.spliceRows(start, 1, []);
		}
		else {
			worksheet.spliceRows(start, 1);
		}
	}
}

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

4 participants