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] Typescript error from getColumn.eachCell #1120

Closed
weelillad opened this issue Feb 12, 2020 · 9 comments · Fixed by #1488
Closed

[BUG] Typescript error from getColumn.eachCell #1120

weelillad opened this issue Feb 12, 2020 · 9 comments · Fixed by #1488

Comments

@weelillad
Copy link
Contributor

weelillad commented Feb 12, 2020

🐛 Bug Report

Typescript error when using Worksheet.getColumn().eachCell - TS2722: Cannot invoke an object which is possibly 'undefined'.

Lib version: 3.8.0

Steps To Reproduce

Code example

const wb = new ExcelJS.Workbook();
const ws = wb.addWorksheet('XYZ');
...
ws.getColumn(1).eachCell((cell, rowNum) => {
  cell.fill = headerFill;
});

The expected behaviour:

There should be no error.

Possible solution (optional, but very helpful):

Is it possible to clearly define the functions and properties that are available from getColumn?

@weelillad weelillad added the bug label Feb 12, 2020
@Siemienik
Copy link
Member

you have . before (

could you try do like below?:

ws.getColumn(1).eachCell((cell, rowNum) => {
  cell.fill = headerFill;
});

@weelillad
Copy link
Contributor Author

Sorry, there was a typo when copying the code. I checked and my code is like what you posted, and the TS error still shows. I have edited my original post to fix my code snippet.

@MetaMmodern
Copy link

MetaMmodern commented Jun 15, 2021

Sorry, is this in latest version of your library? Cause I currently have the same problem.
@alubbe and @Siemienik do you have info about it?

Here is the code where I get error:

const setColumnWidthForSheet = (ws: Worksheet) => {
	for (let i = 0; i < ws.columns.length; i++) {
		const column = ws.columns[i];
		let dataMax = 0;
		if (!column) {
			continue;
		} else {
			column.eachCell({ includeEmpty: true }, (cell) => {
				let columnLength = 0;
				if (cell.value instanceof Date) {
					columnLength = cell.value
						.toISOString()
						.slice(0, 10)
						.replace(/-/g, '.').length;
				} else {
					columnLength = String(cell.value).length;
				}
				if (columnLength > dataMax) {
					dataMax = columnLength;
				}
			});
			column.width = dataMax < 10 ? 15 : dataMax + 8;
		}
	}
};

And the error:
image

Also, I am using version ^4.2.1

@nywleswoey
Copy link

@MetaMmodern ws.columns[i] return the type of Partial<Column> which just means that the properties are not guaranteed to be there. !column doesn't help since the object is present so it'll still return true.

You can try to use ws.getColumn(i) instead, which would return the Column type.

@MetaMmodern
Copy link

MetaMmodern commented Jun 16, 2021

@MetaMmodern ws.columns[i] return the type of Partial<Column> which just means that the properties are not guaranteed to be there. !column doesn't help since the object is present so it'll still return true.

You can try to use ws.getColumn(i) instead, which would return the Column type.

Ok, thank you, I'm gonna try it later.

Update: yep,that helped. Thanks))
Update is below.

@MetaMmodern
Copy link

@nywleswoey Sorry, the thing you provided does not work in runtime. The types are somehow broken.
I've changed as you said to getColumn(i) but here what I het after that:
image

@nywleswoey
Copy link

@MetaMmodern column number starts from 1. From your above code you will try to access column 0.

@MetaMmodern
Copy link

@MetaMmodern column number starts from 1. From your above code you will try to access column 0.

Oopsie, my bad😅
Thanks again

@nywleswoey
Copy link

No problem, glad to have been of help to you.

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

Successfully merging a pull request may close this issue.

5 participants