From 29e44e0aa1067f586e2a5f306c0cd8794974c9b3 Mon Sep 17 00:00:00 2001 From: dfinke Date: Thu, 22 Dec 2016 11:24:18 -0500 Subject: [PATCH 1/2] Add scriptblock for Cell Fill Style --- .../ApplyFormatInScriptBlock.ps1 | 19 +++++++++++++++++++ .../FormatCellStyles/PassInScriptBlock.ps1 | 15 +++++++++++++++ Export-Excel.ps1 | 8 ++++++++ ImportExcel.psm1 | 1 + Install.ps1 | 1 + InstallModule.ps1 | 1 + Set-CellStyle.ps1 | 13 +++++++++++++ 7 files changed, 58 insertions(+) create mode 100644 Examples/FormatCellStyles/ApplyFormatInScriptBlock.ps1 create mode 100644 Examples/FormatCellStyles/PassInScriptBlock.ps1 create mode 100644 Set-CellStyle.ps1 diff --git a/Examples/FormatCellStyles/ApplyFormatInScriptBlock.ps1 b/Examples/FormatCellStyles/ApplyFormatInScriptBlock.ps1 new file mode 100644 index 0000000..f158277 --- /dev/null +++ b/Examples/FormatCellStyles/ApplyFormatInScriptBlock.ps1 @@ -0,0 +1,19 @@ +Get-Process | + Select-Object Company,Handles,PM, NPM| + Export-Excel $xlfile -Show -AutoSize -CellStyleSB { + param( + $workSheet, + $totalRows, + $lastColumn + ) + + Set-CellStyle $workSheet 1 $LastColumn Solid Cyan + + foreach($row in (2..$totalRows | Where-Object {$_ % 2 -eq 0})) { + Set-CellStyle $workSheet $row $LastColumn Solid Gray + } + + foreach($row in (2..$totalRows | Where-Object {$_ % 2 -eq 1})) { + Set-CellStyle $workSheet $row $LastColumn Solid LightGray + } + } \ No newline at end of file diff --git a/Examples/FormatCellStyles/PassInScriptBlock.ps1 b/Examples/FormatCellStyles/PassInScriptBlock.ps1 new file mode 100644 index 0000000..044fff9 --- /dev/null +++ b/Examples/FormatCellStyles/PassInScriptBlock.ps1 @@ -0,0 +1,15 @@ +$RandomStyle = { + param( + $workSheet, + $totalRows, + $lastColumn + ) + + 2..$totalRows | ForEach-Object{ + Set-CellStyle $workSheet $_ $LastColumn Solid (Write-Output LightGreen Gray Red|Get-Random) + } +} + +Get-Process | + Select-Object Company,Handles,PM, NPM| + Export-Excel $xlfile -Show -AutoSize -CellStyleSB $RandomStyle diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index fb92df8..a39717a 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -51,6 +51,8 @@ function Export-Excel { [Object[]]$ConditionalFormat, [Object[]]$ConditionalText, [Object[]]$ExcelChartDefinition, + # [Object[]]$CellStyle, + [ScriptBlock]$CellStyleSB, [string[]]$HideSheet, [Switch]$KillExcel, [Switch]$AutoNameRange, @@ -408,6 +410,12 @@ function Export-Excel { } } + if($CellStyleSB) { + $TotalRows=$ws.Dimension.Rows + $LastColumn=(Get-ExcelColumnName $ws.Dimension.Columns).ColumnName + & $CellStyleSB $ws $TotalRows $LastColumn + } + if($PassThru) { $pkg } else { diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 3ddd453..6ec2b28 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -18,6 +18,7 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll" . $PSScriptRoot\Get-Range.ps1 . $PSScriptRoot\TrackingUtils.ps1 . $PSScriptRoot\Copy-ExcelWorkSheet.ps1 +. $PSScriptRoot\Set-CellStyle.ps1 if($PSVersionTable.PSVersion.Major -ge 5) { . $PSScriptRoot\plot.ps1 diff --git a/Install.ps1 b/Install.ps1 index e5fa13c..7955b22 100644 --- a/Install.ps1 +++ b/Install.ps1 @@ -22,6 +22,7 @@ $fileList = echo ` Get-Range.ps1 ` TrackingUtils.ps1 ` Copy-ExcelWorkSheet.ps1 ` + Set-CellStyle.ps1 ` plot.ps1 if ('' -eq $InstallDirectory) diff --git a/InstallModule.ps1 b/InstallModule.ps1 index 1a3884f..162214b 100644 --- a/InstallModule.ps1 +++ b/InstallModule.ps1 @@ -27,6 +27,7 @@ $targetFiles = echo ` Get-Range.ps1 ` TrackingUtils.ps1 ` Copy-ExcelWorkSheet.ps1 ` + Set-CellStyle.ps1 ` plot.ps1 ls $targetFiles | diff --git a/Set-CellStyle.ps1 b/Set-CellStyle.ps1 new file mode 100644 index 0000000..43cef70 --- /dev/null +++ b/Set-CellStyle.ps1 @@ -0,0 +1,13 @@ +function Set-CellStyle { + param( + $WorkSheet, + $Row, + $LastColumn, + [OfficeOpenXml.Style.ExcelFillStyle]$Pattern, + [System.Drawing.Color]$Color + ) + + $t=$WorkSheet.Cells["A$($Row):$($LastColumn)$($Row)"] + $t.Style.Fill.PatternType=$Pattern + $t.Style.Fill.BackgroundColor.SetColor($Color) +} \ No newline at end of file From 221bc5d21521504e827d9491b56f5ca09274d50a Mon Sep 17 00:00:00 2001 From: dfinke Date: Thu, 22 Dec 2016 12:01:16 -0500 Subject: [PATCH 2/2] updated --- Export-Excel.ps1 | 14 +++++++++++--- ImportExcel.psd1 | 2 +- README.md | 31 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index a39717a..2df98bb 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -14,7 +14,7 @@ function Export-Excel { Get-Service | Export-Excel "c:\temp\test.xlsx" -Show -IncludePivotTable -PivotRows status -PivotData @{status='count'} #> param( - [Parameter(Mandatory=$true)] + #[Parameter(Mandatory=$true)] $Path, [Parameter(ValueFromPipeline=$true)] $TargetData, @@ -51,7 +51,6 @@ function Export-Excel { [Object[]]$ConditionalFormat, [Object[]]$ConditionalText, [Object[]]$ExcelChartDefinition, - # [Object[]]$CellStyle, [ScriptBlock]$CellStyleSB, [string[]]$HideSheet, [Switch]$KillExcel, @@ -59,7 +58,8 @@ function Export-Excel { $StartRow=1, $StartColumn=1, [Switch]$PassThru, - [string]$Numberformat="General" + [string]$Numberformat="General", + [Switch]$Now ) Begin { @@ -70,7 +70,15 @@ function Export-Excel { } try { + if($Now) { + $Path=[System.IO.Path]::GetTempFileName() -replace "\.tmp",".xlsx" + $Show=$true + $AutoSize=$true + $AutoFilter=$true + } + $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) + if (Test-Path $path) { Write-Debug "File `"$Path`" already exists" } diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index 4273cfb..ee3037b 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -4,7 +4,7 @@ RootModule = 'ImportExcel.psm1' # Version number of this module. -ModuleVersion = '2.2.9' +ModuleVersion = '2.2.10' # ID used to uniquely identify this module GUID = '60dd4136-feff-401a-ba27-a84458c57ede' diff --git a/README.md b/README.md index 70e74ed..bb00262 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,37 @@ iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfi # What's new +#### 12/22/2016 +- Added `-Now` switch. This short cuts the process, automatically creating a temp file and enables the `-Show`, `-AutoFilter`, `-AutoSize` switches. + +```powershell +Get-Process | Select Company, Handles | Export-Excel -Now +``` + +- Added ScriptBlocks for coloring cells. Check out + +```powershell +Get-Process | + Select-Object Company,Handles,PM, NPM| + Export-Excel $xlfile -Show -AutoSize -CellStyleSB { + param( + $workSheet, + $totalRows, + $lastColumn + ) + + Set-CellStyle $workSheet 1 $LastColumn Solid Cyan + + foreach($row in (2..$totalRows | Where-Object {$_ % 2 -eq 0})) { + Set-CellStyle $workSheet $row $LastColumn Solid Gray + } + + foreach($row in (2..$totalRows | Where-Object {$_ % 2 -eq 1})) { + Set-CellStyle $workSheet $row $LastColumn Solid LightGray + } + } +``` + #### 9/28/2016 [Fixed](https://github.com/dfinke/ImportExcel/pull/126) Powershell 3.0 compatibility. Thanks to [headsphere](https://github.com/headsphere). He used `$obj.PSObject.Methods[$target]` snytax to make it backward compatible. PS v4.0 and later allow `$obj.$target`.