diff --git a/Examples/OpenExcelPackage/EnableFeatures.ps1 b/Examples/OpenExcelPackage/EnableFeatures.ps1 new file mode 100644 index 00000000..2d3d2634 --- /dev/null +++ b/Examples/OpenExcelPackage/EnableFeatures.ps1 @@ -0,0 +1,28 @@ +# How to use Enable-ExcelAutoFilter and Enable-ExcelAutofit + +try { Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 } catch { throw ; return } + +$data = ConvertFrom-Csv @" +RegionInfo,StateInfo,Units,Price +West,Texas,927,923.71 +North,Tennessee,466,770.67 +East,Florida,520,458.68 +East,Maine,828,661.24 +West,Virginia,465,053.58 +North,Missouri,436,235.67 +South,Kansas,214,992.47 +North,North Dakota,789,640.72 +South,Delaware,712,508.55 +"@ + +$xlfile = "$PSScriptRoot\enableFeatures.xlsx" +Remove-Item $xlfile -ErrorAction SilentlyContinue + +$data | Export-Excel $xlfile + +$excel = Open-ExcelPackage $xlfile + +Enable-ExcelAutoFilter -Worksheet $excel.Sheet1 +Enable-ExcelAutofit -Worksheet $excel.Sheet1 + +Close-ExcelPackage $excel -Show \ No newline at end of file diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index e6934d77..9cb72ffc 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -48,11 +48,14 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5 'ConvertTo-ExcelXlsx', 'Copy-ExcelWorksheet', 'DoChart', + 'Enable-ExcelAutoFilter', + 'Enable-ExcelAutofit', 'Expand-NumberFormat', 'Export-Excel', 'Export-ExcelSheet', 'Get-ExcelColumnName', - 'Get-ExcelFileSummary', + 'Get-ExcelFileSummary', + 'Get-ExcelSheetDimensionAddress', 'Get-ExcelSheetInfo', 'Get-ExcelWorkbookInfo', 'Get-HtmlTable', @@ -63,8 +66,8 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5 'Import-UPS', 'Import-USPS', 'Invoke-AllTests', - 'Invoke-Sum', 'Invoke-ExcelQuery', + 'Invoke-Sum', 'Join-Worksheet', 'LineChart', 'Merge-MultipleSheets', @@ -80,8 +83,8 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5 'PieChart', 'Pivot', 'Read-Clipboard', - 'ReadClipboardImpl', 'Read-OleDbData', + 'ReadClipboardImpl', 'Remove-Worksheet', 'Select-Worksheet', 'Send-SQLDataToExcel', diff --git a/Public/Enable-ExcelAutoFilter.ps1 b/Public/Enable-ExcelAutoFilter.ps1 new file mode 100644 index 00000000..0c4a3d58 --- /dev/null +++ b/Public/Enable-ExcelAutoFilter.ps1 @@ -0,0 +1,16 @@ +function Enable-ExcelAutoFilter { + <# + .SYNOPSIS + Enable the Excel AutoFilter + + .EXAMPLE + Enable-ExcelAutoFilter $targetSheet + #> + param( + [Parameter(Mandatory)] + [OfficeOpenXml.ExcelWorksheet]$Worksheet + ) + + $range = Get-ExcelSheetDimensionAddress $Worksheet + $Worksheet.Cells[$range].AutoFilter = $true +} \ No newline at end of file diff --git a/Public/Enable-ExcelAutofit.ps1 b/Public/Enable-ExcelAutofit.ps1 new file mode 100644 index 00000000..0d117d4a --- /dev/null +++ b/Public/Enable-ExcelAutofit.ps1 @@ -0,0 +1,16 @@ +function Enable-ExcelAutofit { + <# + .SYNOPSIS + Make all text fit the cells + + .EXAMPLE + Enable-ExcelAutofit $excel.Sheet1 + #> + param( + [Parameter(Mandatory)] + [OfficeOpenXml.ExcelWorksheet]$Worksheet + ) + + $range = Get-ExcelSheetDimensionAddress $Worksheet + $Worksheet.Cells[$range].AutoFitColumns() +} \ No newline at end of file diff --git a/Public/Get-ExcelSheetDimensionAddress.ps1 b/Public/Get-ExcelSheetDimensionAddress.ps1 new file mode 100644 index 00000000..993b7f97 --- /dev/null +++ b/Public/Get-ExcelSheetDimensionAddress.ps1 @@ -0,0 +1,15 @@ +function Get-ExcelSheetDimensionAddress { + <# + .SYNOPSIS + Get the Excel address of the dimension of a sheet + + .EXAMPLE + Get-ExcelSheetDimensionAddress $excel.Sheet1 + #> + param( + [Parameter(Mandatory)] + [OfficeOpenXml.ExcelWorksheet]$Worksheet + ) + + $Worksheet.Dimension.Address +} diff --git a/changelog.md b/changelog.md index 8959cff0..4d64cdae 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,4 @@ # v7.5.0 - ## Fixes - Importing multiple files with Import-Excel by pipeline uses only the first file for the row count https://github.com/dfinke/ImportExcel/issues/1172 @@ -9,6 +8,10 @@ - Import-Excel now supports importing multiple sheets. It can either return a dictionary of all sheets, or as a single array of all sheets combined. - `Import-Excel $xlfile *` # reads all sheets, returns all data in a dictionary - `Import-Excel $xlfile * -NotAsDictionary` # reads all sheets, returns all data in a single array +- Added helper functions. Useful for working with an Excel package via `Open-ExcelPackage` or `-PassThru` + - `Enable-ExcelAutoFilter` + - `Enable-ExcelAutofit` + - `Get-ExcelSheetDimensionAddress` # v7.4.2