[ConvertTo-ExcelXlsx] Accept a collection of paths #1605
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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:
This allows you to loop over a collection of paths that you want to convert, while only needing to create one COM object.
The $workbook object created by the Open method is used directly, instead of relying on the stateful ActiveWorkbook property of the $Excel object.
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.
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.