Merge pull request #1171 from dfinke/add-helper-functions

Add helper functions `Enable-ExcelAutoFilter`,  `Enable-ExcelAutofit`, `Get-ExcelSheetDimensionAddress`
This commit is contained in:
Doug Finke
2022-04-30 08:35:08 -04:00
committed by GitHub
6 changed files with 85 additions and 4 deletions

View File

@@ -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

View File

@@ -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',

View File

@@ -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
}

View File

@@ -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()
}

View File

@@ -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
}

View File

@@ -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