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

Handling of Nested Objects, when exporting to xlsx #1

Open
anishshobithps opened this issue May 8, 2024 · 2 comments
Open

Handling of Nested Objects, when exporting to xlsx #1

anishshobithps opened this issue May 8, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@anishshobithps
Copy link

Firstly, thank you for this and example set, it helped me a lot.

I had a situation where, I was fetching details from the database, and it had a nested Object, but the export part would only export non-nested fields.

I was able to achieve exporting nested Object, with the help of by changing

for (const objKey in obj) {
if (Object.hasOwn(naming,objKey)) {
tempObj[naming[objKey]] = obj[objKey];
}
}

To

		Object.keys(obj).forEach(key => {
			if (typeof obj[key] === "object") {
				Object.keys(obj[key]).forEach(nestedKey => {
					if (naming[nestedKey]) {
						tempObj[naming[nestedKey]] = obj[key][nestedKey];
					}
				});
			} else {
				if (naming[key]) {
					tempObj[naming[key]] = obj[key];
				}
			}
		});
@mjm918
Copy link
Owner

mjm918 commented May 8, 2024

Hi @anishshobithps thanks for the improvement. can you create a PR? i will merge it. Your changes will help someone ☺️

@mjm918 mjm918 added the enhancement New feature or request label May 8, 2024
@anishshobithps
Copy link
Author

Firstly, thank you for this and example set, it helped me a lot.

I had a situation where, I was fetching details from the database, and it had a nested Object, but the export part would only export non-nested fields.

I was able to achieve exporting nested Object, with the help of by changing

for (const objKey in obj) {
if (Object.hasOwn(naming,objKey)) {
tempObj[naming[objKey]] = obj[objKey];
}
}

To

		Object.keys(obj).forEach(key => {
			if (typeof obj[key] === "object") {
				Object.keys(obj[key]).forEach(nestedKey => {
					if (naming[nestedKey]) {
						tempObj[naming[nestedKey]] = obj[key][nestedKey];
					}
				});
			} else {
				if (naming[key]) {
					tempObj[naming[key]] = obj[key];
				}
			}
		});

Found out that if something doesn't have a nested object it fails, I have a fix for it but I am busy this week with work, will have a PR for it by the end of the week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants