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

How To Check Whether a Sheet Exists? #312

Closed
fauder opened this issue Dec 19, 2024 · 5 comments
Closed

How To Check Whether a Sheet Exists? #312

fauder opened this issue Dec 19, 2024 · 5 comments
Assignees
Labels
enhancement New feature or request resolved This issue has been resolved.

Comments

@fauder
Copy link
Contributor

fauder commented Dec 19, 2024

I know that XLDocument has a bool operator to check for the result of the open() operation.

What to do for sheets? For example, if worksheetExists( sheet_name ) returns true, does that mean that the sheet opened successfully?

@aral-matrix
Copy link
Collaborator

Good question. Never considered because: If you try to get a worksheet from the workbook, you will get an exception if that fails, which you can catch.
But if you create an empty XLWorksheet yourself, indeed, such a method XLSheet::valid() would be helpful before you trigger a segmentation fault (I just tested that e.g. XLSheet::name() has no safeguard when the underlying XML is a null pointer).

I'll implement this once I am done with the current change.

@aral-matrix aral-matrix self-assigned this Dec 19, 2024
@aral-matrix aral-matrix added the enhancement New feature or request label Dec 19, 2024
@fauder
Copy link
Contributor Author

fauder commented Dec 19, 2024

Then, for now at least, the correct way to handle this is to use try-catch when attempting to get a worksheet from a workbook, right?

@aral-matrix
Copy link
Collaborator

Yes, you could do it like that when attempting to get a worksheet by name. Or - if unsure - you could iterate over the worksheets by index and test the names.

@aral-matrix
Copy link
Collaborator

And with 02f7d42#diff-1875efb653648b982a3e6d1344f92e3947645bac6865095c606a954c7b18d3feR109 you can now access XLWorksheet::valid() to determine whether the underlying XLXmlFile has a link to XML data (or is default initialized with a null pointer).
if XLXmlFile::valid() returns false, invoking any class member functions that access the underlying XML may result in a segmentation fault / undefined behavior.

@aral-matrix aral-matrix added testing Functionality has been implemented in development branch and is pending a merge into main ready to close Pull request has been answered or implemented & is pending closure labels Dec 20, 2024
@aral-matrix
Copy link
Collaborator

Patch has been merged into master

@aral-matrix aral-matrix added resolved This issue has been resolved. and removed testing Functionality has been implemented in development branch and is pending a merge into main ready to close Pull request has been answered or implemented & is pending closure labels Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request resolved This issue has been resolved.
Projects
None yet
Development

No branches or pull requests

2 participants