From d830278cbc82cb5c326b2e761302cbc7f49913eb Mon Sep 17 00:00:00 2001 From: DarkLite1 Date: Wed, 8 Feb 2017 09:58:01 +0100 Subject: [PATCH] Improved ' Get-ExcelSheetInfo ' Added options to retrieve information about the sheet or about the whole workbook. Usefull in case you want to know who last saved the file or other information. --- Get-ExcelSheetInfo.ps1 | 70 ++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/Get-ExcelSheetInfo.ps1 b/Get-ExcelSheetInfo.ps1 index e60e9e5..1e9f4eb 100644 --- a/Get-ExcelSheetInfo.ps1 +++ b/Get-ExcelSheetInfo.ps1 @@ -1,16 +1,19 @@ Function Get-ExcelSheetInfo { - <# - .SYNOPSIS + <# + .SYNOPSIS Get worksheet names and their indices of an Excel workbook. - - .DESCRIPTION + + .DESCRIPTION The Get-ExcelSheetInfo cmdlet gets worksheet names and their indices of an Excel workbook. - + .PARAMETER Path Specifies the path to the Excel file. This parameter is required. + .PARAMETER Type + Specifies which information to get, the one from the workbook or the one from the sheets. + .EXAMPLE - Get-ExcelSheetInfo .\Test.xlsx + Get-ExcelSheetInfo .\Test.xlsx .NOTES CHANGELOG @@ -18,27 +21,48 @@ Function Get-ExcelSheetInfo { .LINK https://github.com/dfinke/ImportExcel - - #> - + #> + [CmdletBinding()] - param( + Param ( [Alias("FullName")] - [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Mandatory=$true)] - $Path + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline, Mandatory)] + [String]$Path, + [ValidateSet('Sheets', 'Workbook')] + [String]$Type = 'Workbook' ) - process { - $Path = (Resolve-Path $Path).ProviderPath - write-debug "target excel file $Path" - $stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path,"Open","Read","ReadWrite" - $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream - $workbook = $xl.Workbook - if($workbook -and $workbook.Worksheets) { - $workbook.Worksheets | - Select-Object -Property name,index,hidden,@{ - Label = "Path" - Expression = {$Path} + + Process { + Try { + $Path = (Resolve-Path $Path).ProviderPath + + Write-Debug "target excel file $Path" + $stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path,"Open","Read","ReadWrite" + $xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $stream + $workbook = $xl.Workbook + + Switch ($Type) { + 'Workbook' { + if ($workbook) { + $workbook.Properties + } } + 'Sheets' { + if ($workbook -and $workbook.Worksheets) { + $workbook.Worksheets | + Select-Object -Property name,index,hidden,@{ + Label = "Path" + Expression = {$Path} + } + } + } + Default { + Write-Error 'Unrecogrnized type' + } + } + } + Catch { + throw "Failed retrieving Excel sheet information for '$Path': $_" } } } \ No newline at end of file