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..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,13 +51,15 @@ function Export-Excel { [Object[]]$ConditionalFormat, [Object[]]$ConditionalText, [Object[]]$ExcelChartDefinition, + [ScriptBlock]$CellStyleSB, [string[]]$HideSheet, [Switch]$KillExcel, [Switch]$AutoNameRange, $StartRow=1, $StartColumn=1, [Switch]$PassThru, - [string]$Numberformat="General" + [string]$Numberformat="General", + [Switch]$Now ) Begin { @@ -68,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" } @@ -408,6 +418,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.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/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/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`. 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