Skip to content

Commit

Permalink
Merge pull request #1171 from dfinke/add-helper-functions
Browse files Browse the repository at this point in the history
Add helper functions `Enable-ExcelAutoFilter`,  `Enable-ExcelAutofit`, `Get-ExcelSheetDimensionAddress`
  • Loading branch information
dfinke authored Apr 30, 2022
2 parents 8d56a35 + ba14511 commit 6ece448
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
28 changes: 28 additions & 0 deletions Examples/OpenExcelPackage/EnableFeatures.ps1
Original file line number Diff line number Diff line change
@@ -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
9 changes: 6 additions & 3 deletions ImportExcel.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
16 changes: 16 additions & 0 deletions Public/Enable-ExcelAutoFilter.ps1
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions Public/Enable-ExcelAutofit.ps1
Original file line number Diff line number Diff line change
@@ -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()
}
15 changes: 15 additions & 0 deletions Public/Get-ExcelSheetDimensionAddress.ps1
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 4 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down

0 comments on commit 6ece448

Please sign in to comment.