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

[ConvertTo-ExcelXlsx] Accept a collection of paths #1605

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

edwardmiller-mesirow
Copy link
Contributor

@edwardmiller-mesirow edwardmiller-mesirow commented May 21, 2024

If I loop through a big list of XLS files that I want to convert, it can throw an error at some point in the list:

Unable to get the SaveAs property of the Workbook class

This could be because of either file locking or issues performing round-trips to network locations. In a situation with network paths, it will make two full roundtrips per path entirely via Excel interop. And that could be a very error-prone situation. When converting the same list of files repeatedly, I was seeing it fail randomly each time on different files in that same list.

The changes in this PR:

  1. This allows you to loop over a collection of paths that you want to convert, while only needing to create one COM object.

    • This is noticeably faster. Processing a dozen items for me goes from 2 minutes to 1 minute.
  2. The $workbook object created by the Open method is used directly, instead of relying on the stateful ActiveWorkbook property of the $Excel object.

  3. This optionally allows caching a copy of each file to the local Temp directory first, which can avoid both locking and network roundtrips. I made the CacheDirectory configurable as well, to handle the case where the default Temp directory isn't writeable. The default behavior is the same as before.

  4. It also just uses ChangeExtension to set the extension since that will work a little better overall in cross-platform scenarios with different case sensitivity.

@edwardmiller-mesirow edwardmiller-mesirow changed the title [ConvertTo-ExcelXlsx] Prevent error: "Unable to get the SaveAs property of the Workbook class" [ConvertTo-ExcelXlsx] Accept a collection of paths May 21, 2024
@edwardmiller-mesirow edwardmiller-mesirow marked this pull request as ready for review May 22, 2024 18:38
@edwardmiller-mesirow
Copy link
Contributor Author

@dfinke this is ready and seems to be considerably better in the case of converting an entire collection of XLS files. But should still work exactly as before in the case of just a single path.

@dfinke dfinke self-assigned this May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants