mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Set-Format,row and column renamed; definitions as hashtables,
This commit is contained in:
@@ -149,7 +149,7 @@
|
||||
[switch]$Passthru
|
||||
)
|
||||
|
||||
#Allow conditional formatting to work like Set-Format (with single ADDRESS parameter), split it to get worksheet and range of cells.
|
||||
#Allow conditional formatting to work like Set-ExcelRange (with single ADDRESS parameter), split it to get worksheet and range of cells.
|
||||
If ($Address -and -not $WorkSheet -and -not $Range) {
|
||||
$WorkSheet = $Address.Worksheet[0]
|
||||
$Range = $Address.Address
|
||||
|
||||
@@ -26,14 +26,14 @@ if (Get-Command -Name register-argumentCompleter -ErrorAction SilentlyContinue)
|
||||
Register-ArgumentCompleter -CommandName New-ConditionalText -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName New-ConditionalText -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName New-ConditionalText -ParameterName ConditionalTextColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Format -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Format -ParameterName FontColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Format -ParameterName BorderColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Format -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Column -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Column -ParameterName FontColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Column -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Row -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Row -ParameterName FontColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Row -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelRange -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelRange -ParameterName FontColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelRange -ParameterName BorderColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelRange -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelColumn -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelColumn -ParameterName FontColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelColumn -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelRow -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelRow -ParameterName FontColor -ScriptBlock $Function:ColorCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelRow -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
del ~\documents\music.xlsx
|
||||
$excel = Get-IndexedItem "itemtype='.mp3'","AlbumArtist like '%'","RatingText <> '1 star'" -NoFiles -orderby AlbumArtist,AlbumTitle,TrackNumber -path c:\users -Recurse -Property AlbumArtist,Duration,title,EncodingBitrate,SampleRate,AlbumTitle,TrackNumber, Size |
|
||||
Select-Object -Property AlbumArtist, AlbumTitle, TrackNumber, Title, Duration, SampleRate, EncodingBitRate, Size | Export-excel -path ~\documents\music.xlsx -WorksheetName Music -AutoNameRange -AutoSize -BoldTopRow -FreezeTopRow -PassThru
|
||||
Remove-item -path ~\documents\music.xlsx
|
||||
$excel = Get-IndexedItem "itemtype='.mp3'","AlbumArtist like '%'","RatingText <> '1 star'" -NoFiles -orderby AlbumArtist,AlbumTitle,TrackNumber -path c:\users -Recurse -Property AlbumArtist,Duration,title,EncodingBitrate,SampleRate,AlbumTitle,TrackNumber, Size |
|
||||
Select-Object -Property AlbumArtist, AlbumTitle, TrackNumber, Title, Duration, SampleRate, EncodingBitRate, Size | Export-excel -path ~\documents\music.xlsx -WorksheetName Music -AutoNameRange -AutoSize -BoldTopRow -FreezeTopRow -PassThru
|
||||
$ws = $excel.Workbook.Worksheets[1]
|
||||
Set-Column -Worksheet $ws -Column 6 -NumberFormat '0,"KHz"'
|
||||
Set-Column -Worksheet $ws -Column 7 -NumberFormat '0,"Kbits/Sec"' -Width 18
|
||||
Set-Column -Worksheet $ws -Column 8 -NumberFormat '#.#,,"MB"' -Width 7
|
||||
Set-ExcelColumn -Worksheet $ws -Column 6 -NumberFormat '0,"KHz"'
|
||||
Set-ExcelColumn -Worksheet $ws -Column 7 -NumberFormat '0,"Kbits/Sec"' -Width 18
|
||||
Set-ExcelColumn -Worksheet $ws -Column 8 -NumberFormat '#.#,,"MB"' -Width 7
|
||||
$pt = Add-PivotTable -PivotTableName SpaceUsedByMusic -ExcelPackage $excel -SourceWorkSheet $ws -PivotRows ALBUMARTIST -PivotData @{"Size"="Sum"} -PivotNumberFormat '#.#,,"MB"' -Activate -PassThru
|
||||
$pt.RowFields[0].Sort = [OfficeOpenXml.Table.PivotTable.eSortType]::Ascending
|
||||
|
||||
|
||||
@@ -352,18 +352,18 @@
|
||||
|
||||
$excel = Get-Process | Select-Object -Property Name,Company,Handles,CPU,PM,NPM,WS | Export-Excel -Path .\test.xlsx -ClearSheet -WorksheetName "Processes" -PassThru
|
||||
$sheet = $excel.Workbook.Worksheets["Processes"]
|
||||
$sheet.Column(1) | Set-Format -Bold -AutoFit
|
||||
$sheet.Column(2) | Set-Format -Width 29 -WrapText
|
||||
$sheet.Column(3) | Set-Format -HorizontalAlignment Right -NFormat "#,###"
|
||||
Set-Format -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NFormat "#,###"
|
||||
Set-Format -Address $sheet.Column(4) -HorizontalAlignment Right -NFormat "#,##0.0" -Bold
|
||||
Set-Format -Address $sheet.Row(1) -Bold -HorizontalAlignment Center
|
||||
$sheet.Column(1) | Set-ExcelRange -Bold -AutoFit
|
||||
$sheet.Column(2) | Set-ExcelRange -Width 29 -WrapText
|
||||
$sheet.Column(3) | Set-ExcelRange -HorizontalAlignment Right -NFormat "#,###"
|
||||
Set-ExcelRange -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NFormat "#,###"
|
||||
Set-ExcelRange -Address $sheet.Column(4) -HorizontalAlignment Right -NFormat "#,##0.0" -Bold
|
||||
Set-ExcelRange -Address $sheet.Row(1) -Bold -HorizontalAlignment Center
|
||||
Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red
|
||||
Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor Red
|
||||
foreach ($c in 5..9) {Set-Format -Address $sheet.Column($c) -AutoFit }
|
||||
foreach ($c in 5..9) {Set-ExcelRange -Address $sheet.Column($c) -AutoFit }
|
||||
Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -IncludePivotChart -ChartType ColumnClustered -NoLegend -PivotRows company -PivotData @{'Name'='Count'} -Show
|
||||
|
||||
This a more sophisticated version of the previous example showing different ways of using Set-Format, and also adding conditional formatting.
|
||||
This a more sophisticated version of the previous example showing different ways of using Set-ExcelRange, and also adding conditional formatting.
|
||||
In the final command a Pivot chart is added and the workbook is opened in Excel.
|
||||
.EXAMPLE
|
||||
0..360 | ForEach-Object {[pscustomobject][ordered]@{X=$_; Sinx="=Sin(Radians(x)) "} } | Export-Excel -now -LineChart -AutoNameRange
|
||||
@@ -1147,6 +1147,10 @@ function Add-WorkSheet {
|
||||
}
|
||||
#endregion
|
||||
if ($Activate) {Select-Worksheet -ExcelWorksheet $ws }
|
||||
if (-not (Get-Member -InputObject $ExcelPackage -Name $ws.Name)) {
|
||||
$sb = [scriptblock]::Create(('$this.workbook.Worksheets["{0}"]' -f $ws.name))
|
||||
Add-Member -InputObject $ExcelPackage -MemberType ScriptProperty -Name $ws.name -Value $sb
|
||||
}
|
||||
return $ws
|
||||
}
|
||||
|
||||
|
||||
@@ -256,25 +256,25 @@
|
||||
$ws = $xl.Workbook.Worksheets[$OutputSheetName]
|
||||
for ($i = 0; $i -lt $expandedDiff.Count; $i++ ) {
|
||||
if ( $expandedDiff[$i].side -ne "==" ) {
|
||||
Set-Format -WorkSheet $ws -Range ("A" + ($i + 2 )) -FontColor $KeyFontColor
|
||||
Set-ExcelRange -WorkSheet $ws -Range ("A" + ($i + 2 )) -FontColor $KeyFontColor
|
||||
}
|
||||
elseif ( $HideEqual ) {$ws.row($i+2).hidden = $true }
|
||||
if ( $expandedDiff[$i].side -eq "<>" ) {
|
||||
$range = $ws.Dimension -replace "\d+", ($i + 2 )
|
||||
Set-Format -WorkSheet $ws -Range $range -BackgroundColor $ChangeBackgroundColor
|
||||
Set-ExcelRange -WorkSheet $ws -Range $range -BackgroundColor $ChangeBackgroundColor
|
||||
}
|
||||
elseif ( $expandedDiff[$i].side -eq "<=" ) {
|
||||
$rangeR1C1 = "R[{0}]C[1]:R[{0}]C[{1}]" -f ($i + 2 ) , $lastRefColNo
|
||||
$range = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1($rangeR1C1,0,0)
|
||||
Set-Format -WorkSheet $ws -Range $range -BackgroundColor $DeleteBackgroundColor
|
||||
Set-ExcelRange -WorkSheet $ws -Range $range -BackgroundColor $DeleteBackgroundColor
|
||||
}
|
||||
elseif ( $expandedDiff[$i].side -eq "=>" ) {
|
||||
if ($propList.count -gt 1) {
|
||||
$rangeR1C1 = "R[{0}]C[{1}]:R[{0}]C[{2}]" -f ($i + 2 ) , $FirstDiffColNo , $lastDiffColNo
|
||||
$range = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1($rangeR1C1,0,0)
|
||||
Set-Format -WorkSheet $ws -Range $range -BackgroundColor $AddBackgroundColor
|
||||
Set-ExcelRange -WorkSheet $ws -Range $range -BackgroundColor $AddBackgroundColor
|
||||
}
|
||||
Set-Format -WorkSheet $ws -Range ("A" + ($i + 2 )) -BackgroundColor $AddBackgroundColor
|
||||
Set-ExcelRange -WorkSheet $ws -Range ("A" + ($i + 2 )) -BackgroundColor $AddBackgroundColor
|
||||
}
|
||||
}
|
||||
Close-ExcelPackage -ExcelPackage $xl -Show:$Show
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
.Example
|
||||
$excel = Open-ExcelPackage -path $xlPath
|
||||
$sheet1 = $excel.Workbook.Worksheets["sheet1"]
|
||||
Set-Format -Address $sheet1.Cells["E1:S1048576"], $sheet1.Cells["V1:V1048576"] -NFormat ([cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern)
|
||||
Set-ExcelRange -Range $sheet1.Cells["E1:S1048576"], $sheet1.Cells["V1:V1048576"] -NFormat ([cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern)
|
||||
Close-ExcelPackage $excel -Show
|
||||
|
||||
This will open the file at $xlPath, select sheet1 apply formatting to two blocks of the sheet and save the package, and launch it in Excel.
|
||||
@@ -46,8 +46,15 @@
|
||||
New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path
|
||||
}
|
||||
elseif (Test-Path -Path $path) {
|
||||
if ($Password) {New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path , $Password }
|
||||
else {New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path }
|
||||
if ($Password) {$pkgobj = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path , $Password }
|
||||
else {$pkgobj = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path }
|
||||
if ($pkgobj) {
|
||||
foreach ($w in $pkgobj.Workbook.Worksheets) {
|
||||
$sb = [scriptblock]::Create(('$this.workbook.Worksheets["{0}"]' -f $w.name))
|
||||
Add-Member -InputObject $pkgobj -MemberType ScriptProperty -Name $w.name -Value $sb
|
||||
}
|
||||
return $pkgobj
|
||||
}
|
||||
}
|
||||
else {Write-Warning "Could not find $path" }
|
||||
}
|
||||
|
||||
@@ -4,6 +4,13 @@
|
||||
Adds a Pivot table (and optional pivot chart) to a workbook
|
||||
.Description
|
||||
If the pivot table already exists, the source data will be updated.
|
||||
.Example
|
||||
$excel = Get-Service | Export-Excel -Path test.xlsx -WorksheetName Services -PassThru -AutoSize -DisplayPropertySet -TableName ServiceTable -Title "Services on $Env:COMPUTERNAME"
|
||||
Add-PivotTable -ExcelPackage $excel -PivotTableName ServiceSummary -SourceRange $excel.Workbook.Worksheets[1].Tables[0].Address -PivotRows Status -PivotData Name -NoTotalsInPivot -Activate
|
||||
Close-ExcelPackage $excel -Show
|
||||
|
||||
This exports data to new workbook and creates a table with the data in.
|
||||
The Pivot table is added on its own page, the table created in the first command is used as the source for the PivotTable; which counts the service names in for each Status. At the end the Pivot page is made active.
|
||||
.Example
|
||||
$chartdef = New-ExcelChartDefinition -Title "Gross and net by city and product" -ChartType ColumnClustered `
|
||||
-Column 11 -Width 500 -Height 360 -YMajorUnit 500 -YMinorUnit 100 -YAxisNumberformat "$#,##0" -LegendPostion Bottom
|
||||
@@ -117,26 +124,40 @@
|
||||
[OfficeOpenXml.ExcelWorksheet]$wsPivot = $address.Worksheet
|
||||
}
|
||||
else {
|
||||
[OfficeOpenXml.ExcelWorksheet]$wsPivot = Add-WorkSheet -ExcelPackage $ExcelPackage -WorksheetName $pivotTableName -Activate:$Activate
|
||||
if ($wsPivot.Name -ne $PivotTableName) {Write-Warning -Message "The Worksheet name for the pivot table does not match the table name '$PivotTableName'; probably because excess or illegal characters were removed." }
|
||||
if ($PivotFilter) {$Address = $wsPivot.Cells["A3"]} else { $Address = $wsPivot.Cells["A1"]}
|
||||
try {
|
||||
if (-not $ExcelPackage) {Write-Warning -message "This combination of Parameters needs to include the ExcelPackage." ; return }
|
||||
[OfficeOpenXml.ExcelWorksheet]$wsPivot = Add-WorkSheet -ExcelPackage $ExcelPackage -WorksheetName $pivotTableName -Activate:$Activate
|
||||
if ($wsPivot.Name -ne $PivotTableName) {Write-Warning -Message "The Worksheet name for the pivot table does not match the table name '$PivotTableName'; probably because excess or illegal characters were removed." }
|
||||
if ($PivotFilter) {$Address = $wsPivot.Cells["A3"]} else { $Address = $wsPivot.Cells["A1"]}
|
||||
}
|
||||
catch {throw "Could not create the sheet for the Pivot table. $_" }
|
||||
}
|
||||
#if the pivot doesn't exist, create it.
|
||||
if (-not $wsPivot) {throw "There was a problem getting the worksheet for the pivot table"}
|
||||
if (-not $wsPivot.PivotTables[$pivotTableName] ) {
|
||||
try {
|
||||
#Accept a string or a worksheet object as $Source Worksheet.
|
||||
if ($SourceWorkSheet -is [string]) {
|
||||
$SourceWorkSheet = $ExcelPackage.Workbook.Worksheets.where( {$_.name -Like $SourceWorkSheet})[0]
|
||||
#Accept a string or a worksheet object as $SourceWorksheet - we don't need a worksheet if we have a Rangebase .
|
||||
if ( $SourceWorkSheet -is [string]) {
|
||||
$SourceWorkSheet = $ExcelPackage.Workbook.Worksheets.where( {$_.name -Like $SourceWorkSheet})[0]
|
||||
}
|
||||
elseif ($SourceWorkSheet -is [int]) {
|
||||
$SourceWorkSheet = $ExcelPackage.Workbook.Worksheets[$SourceWorkSheet]
|
||||
elseif ( $SourceWorkSheet -is [int] ) {
|
||||
$SourceWorkSheet = $ExcelPackage.Workbook.Worksheets[$SourceWorkSheet]
|
||||
}
|
||||
if ($SourceWorkSheet -isnot [OfficeOpenXml.ExcelWorksheet]) {Write-Warning -Message "Could not find source Worksheet for pivot-table '$pivotTableName'." ; return }
|
||||
else {
|
||||
|
||||
if (-not $SourceRange) { $SourceRange = $SourceWorkSheet.Dimension.Address}
|
||||
$pivotTable = $wsPivot.PivotTables.Add($Address, $SourceWorkSheet.Cells[ $SourceRange], $pivotTableName)
|
||||
if ( $SourceRange -is [OfficeOpenXml.Table.ExcelTable]) {$SourceRange = $SourceRange.Address }
|
||||
if ( $sourceRange -is [OfficeOpenXml.ExcelRange] -or
|
||||
$SourceRange -is [OfficeOpenXml.ExcelAddress]) {
|
||||
$pivotTable = $wsPivot.PivotTables.Add($Address, $SourceRange, $pivotTableName)
|
||||
}
|
||||
elseif (-not $SourceRange) {
|
||||
$pivotTable = $wsPivot.PivotTables.Add($Address, $SourceWorkSheet.cells[$SourceWorkSheet.Dimension.Address], $pivotTableName)
|
||||
}
|
||||
elseif ($SourceWorkSheet -isnot [OfficeOpenXml.ExcelWorksheet] ) {
|
||||
Write-Warning -Message "Could not find source Worksheet for pivot-table '$pivotTableName'." ; return
|
||||
}
|
||||
elseif ( $SourceRange -is [String] -or $SourceRange -is [OfficeOpenXml.ExcelAddress]) {
|
||||
$pivotTable = $wsPivot.PivotTables.Add($Address,$SourceWorkSheet.Cells[$SourceRange], $pivotTableName)
|
||||
}
|
||||
else {Write-warning "Could not create a pivot table with the Source Range provided."; return}
|
||||
foreach ($Row in $PivotRows) {
|
||||
try {$null = $pivotTable.RowFields.Add($pivotTable.Fields[$Row]) }
|
||||
catch {Write-Warning -message "Could not add '$row' to Rows in PivotTable $pivotTableName." }
|
||||
|
||||
@@ -51,14 +51,17 @@ Install-Module ImportExcel -scope CurrentUser
|
||||
```PowerShell
|
||||
Install-Module ImportExcel
|
||||
```
|
||||
- Lots of help improvements and some tidying up of examples
|
||||
- Lots of help improvements and some tidying up of example
|
||||
- Renamed Set-Column to Set-ExcelColum, Set-Row to Set-ExcelRow, and Set-Format, to Set-ExcelRange. Added aliases so the old names still work
|
||||
- Set-ExcelRange (or set-Format) used "Address" and "Range" incorrectly. There is now a single parameter "Range", with an alias of "Address" and the code beneath ensures that the function accepts a string specifying cells ("A1:Z10") or a range name; and worksheet. OR A named range, Or Table, or a tables's address, or part of the worksheet.cells collection.
|
||||
- Open-Excel Package and Add-Worksheet now add worksheets as script properties so if you do $Excel = open-excelpackage -path test.xlsx ; $excel.sheet1 ; it will return the sheet named sheet 1 Excel.sheetName is a script property which returns $this.workbook.worksheets["Sheetname"]
|
||||
- `Add-ConditionalFormatting`: Now wraps those strings in quotes when needed (for = <= >= string needs to be in double quotes). Parameter intellisense has been improved. There are new parameters: -StopIfTrue and -Priority and support for using the -Reverse parameter with Color-scale rules. Booleans in the sheet are now supported as the value for a condition. Also brought the two different kinds of condition together inside export-excel, and fixed a bug where named-ranges didn't work in some places. In `New-ConditionalText`, more types of conditional format are supported, and -ConditionalTextColor now has an argument completer
|
||||
- Improved handling of hyperlinks in `Export-Excel`.
|
||||
- `Export-Excel` has better checking of Table and PivotTable names (for uniqueness) and a new test in quick charts that there is suitable data for charting.
|
||||
- `Export-Excel` has better checking of Table and PivotTable names (for uniqueness) and a new test in quick charts that there is suitable data for charting. It also accepts hash tables for chart, pivot table and conditional formatting parameters which are splatted into the functions which add these.
|
||||
- Moved logic for adding a named range out of Export-Excel and into a new function named `Add-ExcelName`, and logic for adding a table into a function named `Add-ExcelTable`; this is to make it easier to do these things independently of Export-Excel, but minimize duplication. The Add-ExcelTable command has extra parameters to toggle the options from table tools toolbar (show totals etc) and set options in the totals row.
|
||||
- Moved PivotTable Functions out of Export-Excel.PS1 into their own file and moved Add-ExcelChart out of Export-Excel.ps1 into New-ExcelChart.ps1
|
||||
- Fixed issues where formatting could be reset when using Export-Excel to manipulate an existing sheet without appending data; this applied to number-formats and tables.
|
||||
- `Add-PivotTable` has some new parameters -PassThru returns the pivot table (e.g. to allow names /sort orders of data series to be tweaked ) -Address parameter allows Pivot to be placed on an existing sheet; -PivotTableStyle allows a change from "Medium6", -PivotNumberFormat sets data cells. Chart creation has been moved out to Add-ExcelChart, and -PivotChartDefinition allows a defintion created with New-ExcelChartDefinition to be used with a PivotTable. This opens up all the things that Add-ExcelChart can do without duplicating the parameters on Add-Pivot table and Export-Excel. Definition, TableStyle, Numberformat and ChartDefiniton can be used in `New-PivotTableDefinition` .
|
||||
- `Add-PivotTable` has some new parameters -PassThru returns the pivot table (e.g. to allow names /sort orders of data series to be tweaked ) -Address parameter allows Pivot to be placed on an existing sheet; -PivotTableStyle allows a change from "Medium6", -PivotNumberFormat formats data cells. It is more flexible about how the source data is specified. Chart creation has been moved out to Add-ExcelChart, and -PivotChartDefinition allows a defintion created with New-ExcelChartDefinition to be used with a PivotTable. This opens up all the things that Add-ExcelChart can do without duplicating the parameters on Add-Pivot table and Export-Excel. Definition, TableStyle, Numberformat and ChartDefiniton can be used in `New-PivotTableDefinition` .
|
||||
- `Add-ExcelChart` now supports -PassThru to return the chart for tweaking after creation; there is now a -PivotTable parameter to allow Add-PivotTable to call the code in Add-ExcelChart. And in `New-ExcelChartDefinition` Legend parameters (for size, bold & position ) are now supported
|
||||
- ChartDefinition and conditional formatting parameters can now be hashtables - if it would will splat into Add-ExcelChart or Add-ConditionalFormatting, it should be acceptable as a definition.
|
||||
|
||||
|
||||
@@ -1,28 +1,29 @@
|
||||
Function Set-Column {
|
||||
Function Set-ExcelColumn {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Adds a column to the existing data area in an Excel sheet, fills values and sets formatting
|
||||
.DESCRIPTION
|
||||
Set-Column takes a value which is either string containing a value or formula or a scriptblock
|
||||
Set-ExcelColumn takes a value which is either string containing a value or formula or a scriptblock
|
||||
which evaluates to a string, and optionally a column number and fills that value down the column.
|
||||
A column name can be specified and the new column can be made a named range.
|
||||
The column can be formatted.
|
||||
.EXAMPLE
|
||||
C:\>Set-Column -Worksheet $ws -Column 5 -NumberFormat 'Currency'
|
||||
C:\> Set-ExcelColumn -Worksheet $ws -Column 5 -NumberFormat 'Currency'
|
||||
$ws contains a worksheet object - and column E is set to use the local currecy format.
|
||||
Intelisense will complete predefined number formats. Expand-NumberFormat currency will show you how currency is interpreted on the local computer.
|
||||
.EXAMPLE
|
||||
C:\> Set-Column -Worksheet $ws -Heading "WinsToFastLaps" -Value {"=E$row/C$row"} -Column 7 -AutoSize -AutoNameRange
|
||||
C:\> Set-ExcelColumn -Worksheet $ws -Heading "WinsToFastLaps" -Value {"=E$row/C$row"} -Column 7 -AutoSize -AutoNameRange
|
||||
Here $WS already contains a worksheet which contains counts of races won and fastest laps recorded by racing drivers (in columns C and E)
|
||||
Set-Column specifies that Column 7 should have a heading of "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3
|
||||
Set-ExcelColumn specifies that Column 7 should have a heading of "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3
|
||||
the data cells should become a named range, which will also be "WinsToFastLaps" the column width will be set automatically
|
||||
.EXAMPLE
|
||||
C:\> Set-Column -Worksheet $ws -Heading "Link" -Value {"https://en.wikipedia.org" + $worksheet.cells["B$Row"].value } -AutoSize
|
||||
C:\> Set-ExcelColumn -Worksheet $ws -Heading "Link" -Value {"https://en.wikipedia.org" + $worksheet.cells["B$Row"].value } -AutoSize
|
||||
In this example, the worksheet in $ws has partial links to wikipedia pages in column B.
|
||||
Value is is a script block and it outputs a string which begins https... and ends with the value of cell at column B in the current row.
|
||||
When given a valid URI set-Column makes it a hyperlink The column will be autosized to fit the links.
|
||||
When given a valid URI Set-ExcelColumn makes it a hyperlink The column will be autosized to fit the links.
|
||||
#>
|
||||
[cmdletbinding()]
|
||||
[Alias(" Set-Column")]
|
||||
[OutputType([OfficeOpenXml.ExcelColumn],[String])]
|
||||
Param (
|
||||
#If specifing the worksheet by name the ExcelPackage object which contains it needs to be passed
|
||||
@@ -151,11 +152,11 @@
|
||||
}
|
||||
$theRange = "$columnName$StartRow`:$columnName$endRow"
|
||||
if ($params.Count) {
|
||||
Set-Format -WorkSheet $Worksheet -Range $theRange @params
|
||||
Set-ExcelRange -WorkSheet $Worksheet -Range $theRange @params
|
||||
}
|
||||
#endregion
|
||||
if ($PSBoundParameters["Hide"]) {$workSheet.Column($Column).Hidden = [bool]$Hide}
|
||||
#return the new data if -passthru was specified.
|
||||
if ($passThru) { $Worksheet.Column( $Column)}
|
||||
if ($passThru) { $Worksheet.Column($Column)}
|
||||
elseif ($ReturnRange) { $theRange}
|
||||
}
|
||||
13
Set-Row.ps1
13
Set-Row.ps1
@@ -1,22 +1,23 @@
|
||||
Function Set-Row {
|
||||
Function Set-ExcelRow {
|
||||
<#
|
||||
.Synopsis
|
||||
Fills values into a [new] row in an Excel spreadsheet. To format a row without setting values, use Set-Format.
|
||||
Fills values into a [new] row in an Excel spreadsheet. And sets row formmats.
|
||||
.Description
|
||||
Set-Row accepts either a Worksheet object or an Excel package object returned by Export-Excel and the name of a sheet,
|
||||
Set-ExcelRow accepts either a Worksheet object or an Excel package object returned by Export-Excel and the name of a sheet,
|
||||
and inserts the chosen contents into a row of the sheet.
|
||||
The contents can be a constant "42" , a formula or a script block which is converted into a constant or formula.
|
||||
The first cell of the row can optional be given a heading.
|
||||
.Example
|
||||
Set-row -Worksheet $ws -Heading Total -Value {"=sum($columnName`2:$columnName$endrow)" }
|
||||
Set-ExcelRow -Worksheet $ws -Heading Total -Value {"=sum($columnName`2:$columnName$endrow)" }
|
||||
|
||||
$Ws contains a worksheet object, and no Row number is specified so Set-Row will select the next row after the end of the data in the sheet
|
||||
$Ws contains a worksheet object, and no Row number is specified so Set-ExcelRow will select the next row after the end of the data in the sheet
|
||||
The first cell will contain "Total", and each other cell will contain
|
||||
=Sum(xx2:xx99) - where xx is the column name, and 99 is the last row of data.
|
||||
Note the use of `2 to Prevent 2 becoming part of the variable "ColumnName"
|
||||
The script block can use $row, $column, $ColumnName, $startRow/Column $endRow/Column
|
||||
#>
|
||||
[cmdletbinding()]
|
||||
[Alias(" Set-Row")]
|
||||
[OutputType([OfficeOpenXml.ExcelRow],[String])]
|
||||
Param (
|
||||
#An Excel package object - e.g. from Export-Excel -passthru - requires a sheet name
|
||||
@@ -137,7 +138,7 @@
|
||||
}
|
||||
$theRange = [OfficeOpenXml.ExcelAddress]::New($Row, $StartColumn, $Row, $endColumn)
|
||||
if ($params.Count) {
|
||||
Set-Format -WorkSheet $Worksheet -Range $theRange @params
|
||||
Set-ExcelRange -WorkSheet $Worksheet -Range $theRange @params
|
||||
}
|
||||
#endregion
|
||||
if ($PSBoundParameters["Hide"]) {$workSheet.Row($Row).Hidden = [bool]$Hide}
|
||||
|
||||
156
SetFormat.ps1
156
SetFormat.ps1
@@ -1,31 +1,29 @@
|
||||
Function Set-Format {
|
||||
Function Set-ExcelRange {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Applies Number, font, alignment and colour formatting to a range of Excel Cells
|
||||
Applies Number, font, alignment and colour formatting, values or formulas to a range of Excel Cells
|
||||
.DESCRIPTION
|
||||
Set-Format was created to set the style elements for a range of cells, this includes autosizing and hiding, setting
|
||||
Set-ExcelRange was created to set the style elements for a range of cells, this includes autosizing and hiding, setting
|
||||
font elements (Name, Size, Bold, Italic, Underline & UnderlineStyle and Subscript & SuperScript), font and background colors,
|
||||
borders, text wrapping, rotation, aliginment within cells, and number format.
|
||||
borders, text wrapping, rotation, aliginment within cells, and number format. It was orignally named "Set-ExcelRange"
|
||||
It has been extended to set Values, Formulas and set ArrayFormulas (sometimes called Ctrl-shift-Enter [CSE] formulas); because of this
|
||||
it has an alias of Set-ExcelRange - in the future the Alias and Canonical name may swapped.
|
||||
the name has become Set-ExcelRange - but the old name of Set-Format is preserved as an alias name may swapped.
|
||||
.EXAMPLE
|
||||
$sheet.Column(3) | Set-Format -HorizontalAlignment Right -NumberFormat "#,###"
|
||||
Selects column 3 from a sheet object (within a workbook object, which is a child of the ExcelPackage object) and passes it to Set-Format which formats as an integer with comma seperated groups
|
||||
$sheet.Column(3) | Set-ExcelRange -HorizontalAlignment Right -NumberFormat "#,###"
|
||||
Selects column 3 from a sheet object (within a workbook object, which is a child of the ExcelPackage object) and passes it to Set-ExcelRange which formats as an integer with comma seperated groups
|
||||
.EXAMPLE
|
||||
Set-Format -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NumberFormat "#,###"
|
||||
Set-ExcelRange -Range $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NumberFormat "#,###"
|
||||
Instead of piping the address in this version specifies a block of cells and applies similar formatting
|
||||
#>
|
||||
[cmdletbinding()]
|
||||
[Alias("Set-Format")]
|
||||
Param (
|
||||
#One or more row(s), Column(s) and/or block(s) of cells to format
|
||||
[Parameter(ValueFromPipeline = $true,ParameterSetName="Address",Mandatory=$True,Position=0)]
|
||||
$Address ,
|
||||
[Parameter(ValueFromPipeline = $true,Position=0)]
|
||||
[Alias("Address")]
|
||||
$Range ,
|
||||
#The worksheet where the format is to be applied
|
||||
[Parameter(ParameterSetName="SheetAndRange",Mandatory=$True)]
|
||||
[OfficeOpenXml.ExcelWorksheet]$WorkSheet ,
|
||||
#The area of the worksheet where the format is to be applied
|
||||
[Parameter(ParameterSetName="SheetAndRange",Mandatory=$True)]
|
||||
[OfficeOpenXml.ExcelAddress]$Range,
|
||||
#Number format to apply to cells e.g. "dd/MM/yyyy HH:mm", "£#,##0.00;[Red]-£#,##0.00", "0.00%" , "##/##" , "0.0E+0" etc
|
||||
[Alias("NFormat")]
|
||||
$NumberFormat,
|
||||
@@ -93,131 +91,133 @@
|
||||
#Hide a row or column (not a range); use -Hidden:$false to unhide
|
||||
[Switch]$Hidden
|
||||
)
|
||||
begin {
|
||||
#Allow Set-Format to take Worksheet and range parameters (like Add Contitional formatting) - convert them to an address
|
||||
if ($WorkSheet -and $Range) {$Address = $WorkSheet.Cells[$Range] }
|
||||
}
|
||||
|
||||
process {
|
||||
if ($Address -is [Array]) {
|
||||
[void]$PSBoundParameters.Remove("Address")
|
||||
$Address | Set-Format @PSBoundParameters
|
||||
if ($Range -is [Array]) {
|
||||
[void]$PSBoundParameters.Remove("Range")
|
||||
$Range | Set-ExcelRange @PSBoundParameters
|
||||
}
|
||||
else {
|
||||
#We should accept, a worksheet and a name of a range or a cell address; a table; the address of a table; a named range; a row, a column or .Cells[ ]
|
||||
if ($Range -is [OfficeOpenXml.Table.ExcelTable]) {$Range = $Range.Address}
|
||||
elseif ($WorkSheet -and ($Range -is [string] -or $Range -is [OfficeOpenXml.ExcelAddress])) {
|
||||
$Range = $WorkSheet.Cells[$Range]
|
||||
}
|
||||
elseif ($Range -is [string]) {Write-Warning -Message "The range pararameter you have specified also needs a worksheet parameter."}
|
||||
|
||||
if ($ResetFont) {
|
||||
$Address.Style.Font.Color.SetColor("Black")
|
||||
$Address.Style.Font.Bold = $false
|
||||
$Address.Style.Font.Italic = $false
|
||||
$Address.Style.Font.UnderLine = $false
|
||||
$Address.Style.Font.Strike = $false
|
||||
$Address.Style.Font.VerticalAlign = [OfficeOpenXml.Style.ExcelVerticalAlignmentFont]::None
|
||||
$Range.Style.Font.Color.SetColor("Black")
|
||||
$Range.Style.Font.Bold = $false
|
||||
$Range.Style.Font.Italic = $false
|
||||
$Range.Style.Font.UnderLine = $false
|
||||
$Range.Style.Font.Strike = $false
|
||||
$Range.Style.Font.VerticalAlign = [OfficeOpenXml.Style.ExcelVerticalAlignmentFont]::None
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('Underline')) {
|
||||
$Address.Style.Font.UnderLine = [boolean]$Underline
|
||||
$Address.Style.Font.UnderLineType = $UnderLineType
|
||||
$Range.Style.Font.UnderLine = [boolean]$Underline
|
||||
$Range.Style.Font.UnderLineType = $UnderLineType
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('Bold')) {
|
||||
$Address.Style.Font.Bold = [boolean]$bold
|
||||
$Range.Style.Font.Bold = [boolean]$bold
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('Italic')) {
|
||||
$Address.Style.Font.Italic = [boolean]$italic
|
||||
$Range.Style.Font.Italic = [boolean]$italic
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('StrikeThru')) {
|
||||
$Address.Style.Font.Strike = [boolean]$StrikeThru
|
||||
$Range.Style.Font.Strike = [boolean]$StrikeThru
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('FontSize')){
|
||||
$Address.Style.Font.Size = $FontSize
|
||||
$Range.Style.Font.Size = $FontSize
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('FontShift')){
|
||||
$Address.Style.Font.VerticalAlign = $FontShift
|
||||
$Range.Style.Font.VerticalAlign = $FontShift
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('FontColor')){
|
||||
$Address.Style.Font.Color.SetColor( $FontColor)
|
||||
$Range.Style.Font.Color.SetColor( $FontColor)
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('TextRotation')) {
|
||||
$Address.Style.TextRotation = $TextRotation
|
||||
$Range.Style.TextRotation = $TextRotation
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('WrapText')) {
|
||||
$Address.Style.WrapText = [boolean]$WrapText
|
||||
$Range.Style.WrapText = [boolean]$WrapText
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('HorizontalAlignment')) {
|
||||
$Address.Style.HorizontalAlignment = $HorizontalAlignment
|
||||
$Range.Style.HorizontalAlignment = $HorizontalAlignment
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('VerticalAlignment')) {
|
||||
$Address.Style.VerticalAlignment = $VerticalAlignment
|
||||
$Range.Style.VerticalAlignment = $VerticalAlignment
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('Value')) {
|
||||
if ($Value -match '^=') {$PSBoundParameters["Formula"] = $Value -replace '^=','' }
|
||||
else {
|
||||
$Address.Value = $Value
|
||||
if ($Value -is [datetime]) { $Address.Style.Numberformat.Format = 'm/d/yy h:mm' }# This is not a custom format, but a preset recognized as date and localized. It might be overwritten in a moment
|
||||
if ($Value -is [timespan]) { $Address.Style.Numberformat.Format = '[h]:mm:ss' }
|
||||
$Range.Value = $Value
|
||||
if ($Value -is [datetime]) { $Range.Style.Numberformat.Format = 'm/d/yy h:mm' }# This is not a custom format, but a preset recognized as date and localized. It might be overwritten in a moment
|
||||
if ($Value -is [timespan]) { $Range.Style.Numberformat.Format = '[h]:mm:ss' }
|
||||
}
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('Formula')) {
|
||||
if ($ArrayFormula) {$Address.CreateArrayFormula(($Formula -replace '^=','')) }
|
||||
else {$Address.Formula = ($Formula -replace '^=','') }
|
||||
if ($ArrayFormula) {$Range.CreateArrayFormula(($Formula -replace '^=','')) }
|
||||
else {$Range.Formula = ($Formula -replace '^=','') }
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('NumberFormat')) {
|
||||
$Address.Style.Numberformat.Format = (Expand-NumberFormat $NumberFormat)
|
||||
$Range.Style.Numberformat.Format = (Expand-NumberFormat $NumberFormat)
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('BorderAround')) {
|
||||
$Address.Style.Border.BorderAround($BorderAround, $BorderColor)
|
||||
$Range.Style.Border.BorderAround($BorderAround, $BorderColor)
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('BorderBottom')) {
|
||||
$Address.Style.Border.Bottom.Style=$BorderBottom
|
||||
$Address.Style.Border.Bottom.Color.SetColor($BorderColor)
|
||||
$Range.Style.Border.Bottom.Style=$BorderBottom
|
||||
$Range.Style.Border.Bottom.Color.SetColor($BorderColor)
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('BorderTop')) {
|
||||
$Address.Style.Border.Top.Style=$BorderTop
|
||||
$Address.Style.Border.Top.Color.SetColor($BorderColor)
|
||||
$Range.Style.Border.Top.Style=$BorderTop
|
||||
$Range.Style.Border.Top.Color.SetColor($BorderColor)
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('BorderLeft')) {
|
||||
$Address.Style.Border.Left.Style=$BorderLeft
|
||||
$Address.Style.Border.Left.Color.SetColor($BorderColor)
|
||||
$Range.Style.Border.Left.Style=$BorderLeft
|
||||
$Range.Style.Border.Left.Color.SetColor($BorderColor)
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('BorderRight')) {
|
||||
$Address.Style.Border.Right.Style=$BorderRight
|
||||
$Address.Style.Border.Right.Color.SetColor($BorderColor)
|
||||
$Range.Style.Border.Right.Style=$BorderRight
|
||||
$Range.Style.Border.Right.Color.SetColor($BorderColor)
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('BackgroundColor')) {
|
||||
$Address.Style.Fill.PatternType = $BackgroundPattern
|
||||
$Address.Style.Fill.BackgroundColor.SetColor($BackgroundColor)
|
||||
$Range.Style.Fill.PatternType = $BackgroundPattern
|
||||
$Range.Style.Fill.BackgroundColor.SetColor($BackgroundColor)
|
||||
if ($PatternColor) {
|
||||
$Address.Style.Fill.PatternColor.SetColor( $PatternColor)
|
||||
$Range.Style.Fill.PatternColor.SetColor( $PatternColor)
|
||||
}
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('Height')) {
|
||||
if ($Address -is [OfficeOpenXml.ExcelRow] ) {$Address.Height = $Height }
|
||||
elseif ($Address -is [OfficeOpenXml.ExcelRange] ) {
|
||||
($Address.Start.Row)..($Address.Start.Row + $Address.Rows) |
|
||||
ForEach-Object {$Address.WorkSheet.Row($_).Height = $Height }
|
||||
if ($Range -is [OfficeOpenXml.ExcelRow] ) {$Range.Height = $Height }
|
||||
elseif ($Range -is [OfficeOpenXml.ExcelRange] ) {
|
||||
($Range.Start.Row)..($Range.Start.Row + $Range.Rows) |
|
||||
ForEach-Object {$Range.WorkSheet.Row($_).Height = $Height }
|
||||
}
|
||||
else {Write-Warning -Message ("Can set the height of a row or a range but not a {0} object" -f ($Address.GetType().name)) }
|
||||
else {Write-Warning -Message ("Can set the height of a row or a range but not a {0} object" -f ($Range.GetType().name)) }
|
||||
}
|
||||
if ($Autosize) {
|
||||
if ($Address -is [OfficeOpenXml.ExcelColumn]) {$Address.AutoFit() }
|
||||
elseif ($Address -is [OfficeOpenXml.ExcelRange] ) {
|
||||
$Address.AutoFitColumns()
|
||||
if ($Range -is [OfficeOpenXml.ExcelColumn]) {$Range.AutoFit() }
|
||||
elseif ($Range -is [OfficeOpenXml.ExcelRange] ) {
|
||||
$Range.AutoFitColumns()
|
||||
}
|
||||
else {Write-Warning -Message ("Can autofit a column or a range but not a {0} object" -f ($Address.GetType().name)) }
|
||||
else {Write-Warning -Message ("Can autofit a column or a range but not a {0} object" -f ($Range.GetType().name)) }
|
||||
|
||||
}
|
||||
elseif ($PSBoundParameters.ContainsKey('Width')) {
|
||||
if ($Address -is [OfficeOpenXml.ExcelColumn]) {$Address.Width = $Width}
|
||||
elseif ($Address -is [OfficeOpenXml.ExcelRange] ) {
|
||||
($Address.Start.Column)..($Address.Start.Column + $Address.Columns - 1) |
|
||||
if ($Range -is [OfficeOpenXml.ExcelColumn]) {$Range.Width = $Width}
|
||||
elseif ($Range -is [OfficeOpenXml.ExcelRange] ) {
|
||||
($Range.Start.Column)..($Range.Start.Column + $Range.Columns - 1) |
|
||||
ForEach-Object {
|
||||
#$ws.Column($_).Width = $Width
|
||||
$Address.Worksheet.Column($_).Width = $Width
|
||||
$Range.Worksheet.Column($_).Width = $Width
|
||||
}
|
||||
}
|
||||
else {Write-Warning -Message ("Can set the width of a column or a range but not a {0} object" -f ($Address.GetType().name)) }
|
||||
else {Write-Warning -Message ("Can set the width of a column or a range but not a {0} object" -f ($Range.GetType().name)) }
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey('Hidden')) {
|
||||
if ($Address -is [OfficeOpenXml.ExcelRow] -or
|
||||
$Address -is [OfficeOpenXml.ExcelColumn] ) {$Address.Hidden = [boolean]$Hidden}
|
||||
else {Write-Warning -Message ("Can hide a row or a column but not a {0} object" -f ($Address.GetType().name)) }
|
||||
if ($Range -is [OfficeOpenXml.ExcelRow] -or
|
||||
$Range -is [OfficeOpenXml.ExcelColumn] ) {$Range.Hidden = [boolean]$Hidden}
|
||||
else {Write-Warning -Message ("Can hide a row or a column but not a {0} object" -f ($Range.GetType().name)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -266,9 +266,9 @@ Function NumberFormatCompletion {
|
||||
if (Get-Command -ErrorAction SilentlyContinue -name Register-ArgumentCompleter) {
|
||||
Register-ArgumentCompleter -CommandName Add-ConditionalFormatting -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
|
||||
Register-ArgumentCompleter -CommandName Export-Excel -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Format -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Column -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-Row -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelRange -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelColumn -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
|
||||
Register-ArgumentCompleter -CommandName Set-ExcelRow -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
|
||||
Register-ArgumentCompleter -CommandName Add-PivotTable -ParameterName PivotNumberFormat -ScriptBlock $Function:NumberFormatCompletion
|
||||
Register-ArgumentCompleter -CommandName New-PivotTableDefinition -ParameterName PivotNumberFormat -ScriptBlock $Function:NumberFormatCompletion
|
||||
Register-ArgumentCompleter -CommandName New-ExcelChartDefinition -ParameterName XAxisNumberformat -ScriptBlock $Function:NumberFormatCompletion
|
||||
|
||||
@@ -696,16 +696,16 @@ Describe ExportExcel {
|
||||
#Test freezing top row/first column, adding formats and a pivot table - from Add-Pivot table not a specification variable - after the export
|
||||
$excel = Get-Process | Select-Object -Property Name, Company, Handles, CPU, PM, NPM, WS | Export-Excel -Path $path -ClearSheet -WorkSheetname "Processes" -FreezeTopRowFirstColumn -PassThru
|
||||
$sheet = $excel.Workbook.Worksheets["Processes"]
|
||||
$sheet.Column(1) | Set-Format -Bold -AutoFit
|
||||
$sheet.Column(2) | Set-Format -Width 29 -WrapText
|
||||
$sheet.Column(3) | Set-Format -HorizontalAlignment Right -NFormat "#,###"
|
||||
Set-Format -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NFormat "#,###"
|
||||
Set-Format -Address $sheet.Column(4) -HorizontalAlignment Right -NFormat "#,##0.0" -Bold
|
||||
Set-Format -Address $sheet.Row(1) -Bold -HorizontalAlignment Center
|
||||
$sheet.Column(1) | Set-ExcelRange -Bold -AutoFit
|
||||
$sheet.Column(2) | Set-ExcelRange -Width 29 -WrapText
|
||||
$sheet.Column(3) | Set-ExcelRange -HorizontalAlignment Right -NFormat "#,###"
|
||||
Set-ExcelRange -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NFormat "#,###"
|
||||
Set-ExcelRange -Address $sheet.Column(4) -HorizontalAlignment Right -NFormat "#,##0.0" -Bold
|
||||
Set-ExcelRange -Address $sheet.Row(1) -Bold -HorizontalAlignment Center
|
||||
Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red
|
||||
$rule = Add-ConditionalFormatting -passthru -Address $sheet.cells["C:C"] -RuleType TopPercent -ConditionValue 20 -Bold -StrikeThru
|
||||
Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor Red -Bold -Italic -Underline -BackgroundColor Beige -BackgroundPattern LightUp -PatternColor Gray
|
||||
foreach ($c in 5..9) {Set-Format $sheet.Column($c) -AutoFit }
|
||||
foreach ($c in 5..9) {Set-ExcelRange $sheet.Column($c) -AutoFit }
|
||||
Add-PivotTable -PivotTableName "PT_Procs" -ExcelPackage $excel -SourceWorkSheet 1 -PivotRows Company -PivotData @{'Name' = 'Count'} -IncludePivotChart -ChartType ColumnClustered -NoLegend
|
||||
Close-ExcelPackage $excel
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ Describe "Creating small named ranges with hyperlinks" {
|
||||
1..$columns | foreach {Add-ExcelName -Range $worksheet.cells[$topRow,$_,$lastDataRow,$_]} #Test Add-Excel Name on its own (outside Export-Excel)
|
||||
|
||||
$scwarnVar = $null
|
||||
Set-Column -Worksheet $worksheet -StartRow $topRow -Heading "PlacesGained/Lost" `
|
||||
Set-ExcelColumn -Worksheet $worksheet -StartRow $topRow -Heading "PlacesGained/Lost" `
|
||||
-Value "=GridPosition-FinishPosition" -AutoNameRange -WarningVariable scWarnVar -WarningAction SilentlyContinue #Test as many set column options as possible.
|
||||
$columns ++
|
||||
|
||||
|
||||
@@ -56,37 +56,37 @@ Describe "Number format expansion and setting" {
|
||||
|
||||
$excel = 1..32 | ForEach-Object {$n} | Export-Excel -Path $path -show -WorksheetName s2 -PassThru
|
||||
$ws = $excel.Workbook.Worksheets[1]
|
||||
Set-Format -WorkSheet $ws -Range "A1" -numberFormat 'General'
|
||||
Set-Format -WorkSheet $ws -Range "A2" -numberFormat 'Number'
|
||||
Set-Format -WorkSheet $ws -Range "A3" -numberFormat 'Percentage'
|
||||
Set-Format -WorkSheet $ws -Range "A4" -numberFormat 'Scientific'
|
||||
Set-Format -WorkSheet $ws -Range "A5" -numberFormat 'Fraction'
|
||||
Set-Format -WorkSheet $ws -Range "A6" -numberFormat 'Short Date'
|
||||
Set-Format -WorkSheet $ws -Range "A7" -numberFormat 'Short Time'
|
||||
Set-Format -WorkSheet $ws -Range "A8" -numberFormat 'Long Time'
|
||||
Set-Format -WorkSheet $ws -Range "A9" -numberFormat 'Date-Time'
|
||||
Set-Format -WorkSheet $ws -Range "A10" -numberFormat 'Currency'
|
||||
Set-Format -WorkSheet $ws -Range "A11" -numberFormat 'Text'
|
||||
Set-Format -WorkSheet $ws -Range "A12" -numberFormat 'h:mm AM/PM'
|
||||
Set-Format -WorkSheet $ws -Range "A13" -numberFormat 'h:mm:ss AM/PM'
|
||||
Set-Format -WorkSheet $ws -Range "A14" -numberFormat 'mm:ss'
|
||||
Set-Format -WorkSheet $ws -Range "A15" -numberFormat '[h]:mm:ss'
|
||||
Set-Format -WorkSheet $ws -Range "A16" -numberFormat 'mmss.0'
|
||||
Set-Format -WorkSheet $ws -Range "A17" -numberFormat 'd-mmm-yy'
|
||||
Set-Format -WorkSheet $ws -Range "A18" -numberFormat 'd-mmm'
|
||||
Set-Format -WorkSheet $ws -Range "A19" -numberFormat 'mmm-yy'
|
||||
Set-Format -WorkSheet $ws -Range "A20" -numberFormat '0'
|
||||
Set-Format -WorkSheet $ws -Range "A21" -numberFormat '0.00'
|
||||
Set-Format -Address $ws.Cells[ "A22"] -NumberFormat '#,##0'
|
||||
Set-Format -Address $ws.Cells[ "A23"] -NumberFormat '#,##0.00'
|
||||
Set-Format -Address $ws.Cells[ "A24"] -NumberFormat '#,'
|
||||
Set-Format -Address $ws.Cells[ "A25"] -NumberFormat '#.0,,'
|
||||
Set-Format -Address $ws.Cells[ "A26"] -NumberFormat '0%'
|
||||
Set-Format -Address $ws.Cells[ "A27"] -NumberFormat '0.00%'
|
||||
Set-Format -Address $ws.Cells[ "A28"] -NumberFormat '0.00E+00'
|
||||
Set-Format -Address $ws.Cells[ "A29"] -NumberFormat '# ?/?'
|
||||
Set-Format -Address $ws.Cells[ "A30"] -NumberFormat '# ??/??'
|
||||
Set-Format -Address $ws.Cells[ "A31"] -NumberFormat '@'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A1" -numberFormat 'General'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A2" -numberFormat 'Number'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A3" -numberFormat 'Percentage'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A4" -numberFormat 'Scientific'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A5" -numberFormat 'Fraction'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A6" -numberFormat 'Short Date'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A7" -numberFormat 'Short Time'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A8" -numberFormat 'Long Time'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A9" -numberFormat 'Date-Time'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A10" -numberFormat 'Currency'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A11" -numberFormat 'Text'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A12" -numberFormat 'h:mm AM/PM'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A13" -numberFormat 'h:mm:ss AM/PM'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A14" -numberFormat 'mm:ss'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A15" -numberFormat '[h]:mm:ss'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A16" -numberFormat 'mmss.0'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A17" -numberFormat 'd-mmm-yy'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A18" -numberFormat 'd-mmm'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A19" -numberFormat 'mmm-yy'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A20" -numberFormat '0'
|
||||
Set-ExcelRange -WorkSheet $ws -Range "A21" -numberFormat '0.00'
|
||||
Set-ExcelRange -Address $ws.Cells[ "A22"] -NumberFormat '#,##0'
|
||||
Set-ExcelRange -Address $ws.Cells[ "A23"] -NumberFormat '#,##0.00'
|
||||
Set-ExcelRange -Address $ws.Cells[ "A24"] -NumberFormat '#,'
|
||||
Set-ExcelRange -Address $ws.Cells[ "A25"] -NumberFormat '#.0,,'
|
||||
Set-ExcelRange -Address $ws.Cells[ "A26"] -NumberFormat '0%'
|
||||
Set-ExcelRange -Address $ws.Cells[ "A27"] -NumberFormat '0.00%'
|
||||
Set-ExcelRange -Address $ws.Cells[ "A28"] -NumberFormat '0.00E+00'
|
||||
Set-ExcelRange -Address $ws.Cells[ "A29"] -NumberFormat '# ?/?'
|
||||
Set-ExcelRange -Address $ws.Cells[ "A30"] -NumberFormat '# ??/??'
|
||||
Set-ExcelRange -Address $ws.Cells[ "A31"] -NumberFormat '@'
|
||||
|
||||
Close-ExcelPackage -ExcelPackage $excel
|
||||
|
||||
@@ -129,36 +129,36 @@ Describe "Number format expansion and setting" {
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Set-Column, Set-Row and Set Format" {
|
||||
Describe "Set-ExcelColumn, Set-ExcelRow and Set-ExcelRange" {
|
||||
BeforeAll {
|
||||
Remove-Item -Path $path -ErrorAction SilentlyContinue
|
||||
$excel = $data| Export-Excel -Path $path -AutoNameRange -PassThru
|
||||
$ws = $excel.Workbook.Worksheets["Sheet1"]
|
||||
|
||||
$c = Set-Column -PassThru -Worksheet $ws -Heading "Total" -Value "=Quantity*Price" -NumberFormat "£#,###.00" -FontColor Blue -Bold -HorizontalAlignment Right -VerticalAlignment Top
|
||||
$r = Set-Row -PassThru -Worksheet $ws -StartColumn 3 -BorderAround Thin -Italic -Underline -FontSize 14 -Value {"=sum($columnName`2:$columnName$endrow)" } -VerticalAlignment Bottom
|
||||
Set-Format -Address $excel.Workbook.Worksheets["Sheet1"].cells["b3"] -HorizontalAlignment Right -VerticalAlignment Center -BorderAround Thick -BorderColor Red -StrikeThru
|
||||
Set-Format -Address $excel.Workbook.Worksheets["Sheet1"].cells["c3"] -BorderColor Red -BorderTop DashDot -BorderLeft DashDotDot -BorderBottom Dashed -BorderRight Dotted
|
||||
Set-Format -WorkSheet $ws -Range "E3" -Bold:$false -FontShift Superscript -HorizontalAlignment Left
|
||||
Set-Format -WorkSheet $ws -Range "E1" -ResetFont -HorizontalAlignment General
|
||||
Set-Format -Address $ws.cells["E7"] -ResetFont -WrapText -BackgroundColor AliceBlue -BackgroundPattern DarkTrellis -PatternColor Red -NumberFormat "£#,###.00"
|
||||
Set-Format -Address $ws.Column(1) -Width 0
|
||||
Set-Format -Address $ws.Column(2) -AutoFit
|
||||
Set-Format -Address $ws.Cells["E:E"] -AutoFit
|
||||
Set-Format -Address $ws.row(5) -Height 0
|
||||
$c = Set-ExcelColumn -PassThru -Worksheet $ws -Heading "Total" -Value "=Quantity*Price" -NumberFormat "£#,###.00" -FontColor Blue -Bold -HorizontalAlignment Right -VerticalAlignment Top
|
||||
$r = Set-ExcelRow -PassThru -Worksheet $ws -StartColumn 3 -BorderAround Thin -Italic -Underline -FontSize 14 -Value {"=sum($columnName`2:$columnName$endrow)" } -VerticalAlignment Bottom
|
||||
Set-ExcelRange -Address $excel.Workbook.Worksheets["Sheet1"].cells["b3"] -HorizontalAlignment Right -VerticalAlignment Center -BorderAround Thick -BorderColor Red -StrikeThru
|
||||
Set-ExcelRange -Address $excel.Workbook.Worksheets["Sheet1"].cells["c3"] -BorderColor Red -BorderTop DashDot -BorderLeft DashDotDot -BorderBottom Dashed -BorderRight Dotted
|
||||
Set-ExcelRange -WorkSheet $ws -Range "E3" -Bold:$false -FontShift Superscript -HorizontalAlignment Left
|
||||
Set-ExcelRange -WorkSheet $ws -Range "E1" -ResetFont -HorizontalAlignment General
|
||||
Set-ExcelRange -Address $ws.cells["E7"] -ResetFont -WrapText -BackgroundColor AliceBlue -BackgroundPattern DarkTrellis -PatternColor Red -NumberFormat "£#,###.00"
|
||||
Set-ExcelRange -Address $ws.Column(1) -Width 0
|
||||
Set-ExcelRange -Address $ws.Column(2) -AutoFit
|
||||
Set-ExcelRange -Address $ws.Cells["E:E"] -AutoFit
|
||||
Set-ExcelRange -Address $ws.row(5) -Height 0
|
||||
$rr = $r.row
|
||||
Set-Format -WorkSheet $ws -Range "B$rr" -Value "Total"
|
||||
Set-ExcelRange -WorkSheet $ws -Range "B$rr" -Value "Total"
|
||||
$BadHideWarnvar = $null
|
||||
Set-Format -WorkSheet $ws -Range "D$rr" -Formula "=E$rr/C$rr" -Hidden -WarningVariable "BadHideWarnvar" -WarningAction SilentlyContinue
|
||||
Set-ExcelRange -WorkSheet $ws -Range "D$rr" -Formula "=E$rr/C$rr" -Hidden -WarningVariable "BadHideWarnvar" -WarningAction SilentlyContinue
|
||||
$rr ++
|
||||
Set-Format -WorkSheet $ws -Range "B$rr" -Value ([datetime]::Now)
|
||||
Set-ExcelRange -WorkSheet $ws -Range "B$rr" -Value ([datetime]::Now)
|
||||
Close-ExcelPackage $excel -Calculate
|
||||
|
||||
|
||||
$excel = Open-ExcelPackage $path
|
||||
$ws = $excel.Workbook.Worksheets["Sheet1"]
|
||||
}
|
||||
Context "Set-Row and Set-Column" {
|
||||
Context "Set-ExcelRow and Set-ExcelColumn" {
|
||||
it "Set a row and a column to have zero width/height " {
|
||||
$r | Should not beNullorEmpty
|
||||
# $c | Should not beNullorEmpty ## can't see why but this test breaks in appveyor
|
||||
@@ -213,7 +213,7 @@ Describe "Set-Column, Set-Row and Set Format" {
|
||||
}
|
||||
}
|
||||
|
||||
Context "Set-Format value setting " {
|
||||
Context "Set-ExcelRange value setting " {
|
||||
it "Inserted a formula " {
|
||||
$ws.Cells["D7"].Formula | Should be "E7/C7"
|
||||
}
|
||||
@@ -225,15 +225,15 @@ Describe "Set-Column, Set-Row and Set Format" {
|
||||
}
|
||||
}
|
||||
|
||||
Context "Set-Column Value Setting" {
|
||||
Context "Set-ExcelColumn Value Setting" {
|
||||
BeforeAll {
|
||||
Remove-Item -Path $path -ErrorAction SilentlyContinue
|
||||
|
||||
$excel = $DriverData | Export-Excel -PassThru -Path $path -AutoSize -AutoNameRange
|
||||
$ws = $excel.Workbook.Worksheets[1]
|
||||
|
||||
Set-Column -Worksheet $ws -Heading "Link" -AutoSize -Value {"https://en.wikipedia.org" + $worksheet.cells["B$Row"].value }
|
||||
$c = Set-Column -PassThru -Worksheet $ws -Heading "NextBirthday" -Value {
|
||||
Set-ExcelColumn -Worksheet $ws -Heading "Link" -AutoSize -Value {"https://en.wikipedia.org" + $worksheet.cells["B$Row"].value }
|
||||
$c = Set-ExcelColumn -PassThru -Worksheet $ws -Heading "NextBirthday" -Value {
|
||||
$bmonth = $worksheet.cells["C$Row"].value.month ; $bDay = $worksheet.cells["C$Row"].value.day
|
||||
$cMonth = [datetime]::Now.Month ; $cday = [datetime]::Now.day ; $cyear = [datetime]::Now.Year
|
||||
if (($cmonth -gt $bmonth) -or (($cMonth -eq $bmonth) -and ($cday -ge $bDay))){
|
||||
@@ -241,8 +241,8 @@ Describe "Set-Column, Set-Row and Set Format" {
|
||||
}
|
||||
else {[datetime]::new($cyear, $bmonth, $bday) }
|
||||
}
|
||||
Set-Column -Worksheet $ws -Heading "Age" -Value "=INT((NOW()-DateOfBirth)/365)"
|
||||
Set-Format -Address $c,$ws.column(3) -NumberFormat 'Short Date' -AutoSize
|
||||
Set-ExcelColumn -Worksheet $ws -Heading "Age" -Value "=INT((NOW()-DateOfBirth)/365)"
|
||||
Set-ExcelRange -Address $c,$ws.column(3) -NumberFormat 'Short Date' -AutoSize
|
||||
|
||||
Close-ExcelPackage -ExcelPackage $excel -Calculate
|
||||
$excel = Open-ExcelPackage $path
|
||||
@@ -309,8 +309,8 @@ Describe "Table Formatting" {
|
||||
$ws = $excel.Workbook.Worksheets[1]
|
||||
Add-ExcelTable -Range $ws.cells[$($ws.Dimension.address)] -TableStyle Light1 -TableName HardwareTable -TotalSettings @{"Total"="Sum"} -ShowFirstColumn -ShowFilter:$false
|
||||
|
||||
Set-Column -Worksheet $ws -Column 4 -NumberFormat 'Currency'
|
||||
Set-Column -Worksheet $ws -Column 5 -NumberFormat 'Currency'
|
||||
Set-ExcelColumn -Worksheet $ws -Column 4 -NumberFormat 'Currency'
|
||||
Set-ExcelColumn -Worksheet $ws -Column 5 -NumberFormat 'Currency'
|
||||
|
||||
$PtDef =New-PivotTableDefinition -PivotTableName Totals -PivotRows Product -PivotData @{"Total"="Sum"} -PivotNumberFormat Currency -PivotTotals None -PivotTableSyle Dark2
|
||||
Export-excel -ExcelPackage $excel -WorksheetName Hardware -PivotTableDefinition $PtDef
|
||||
@@ -320,8 +320,8 @@ Describe "Table Formatting" {
|
||||
#test showfilter & TotalSettings
|
||||
$Table = Add-ExcelTable -PassThru -Range $ws.cells[$($ws.Dimension.address)] -TableStyle Light1 -TableName HardwareTable -TotalSettings @{"Total"="Sum"} -ShowFirstColumn -ShowFilter:$false
|
||||
#test expnading named number formats
|
||||
Set-Column -Worksheet $ws -Column 4 -NumberFormat 'Currency'
|
||||
Set-Column -Worksheet $ws -Column 5 -NumberFormat 'Currency'
|
||||
Set-ExcelColumn -Worksheet $ws -Column 4 -NumberFormat 'Currency'
|
||||
Set-ExcelColumn -Worksheet $ws -Column 5 -NumberFormat 'Currency'
|
||||
$PtDef =New-PivotTableDefinition -PivotTableName Totals -PivotRows Product -PivotData @{"Total"="Sum"} -PivotNumberFormat Currency -PivotTotals None -PivotTableSyle Dark2
|
||||
Export-excel -ExcelPackage $excel -WorksheetName Hardware -PivotTableDefinition $PtDef
|
||||
$excel= Open-ExcelPackage -Path $path
|
||||
|
||||
Reference in New Issue
Block a user