Merge pull request #153 from dfinke/tryscriptblock

Tryscriptblock
This commit is contained in:
Doug Finke
2016-12-22 12:02:29 -05:00
committed by GitHub
9 changed files with 100 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -22,6 +22,7 @@ $fileList = echo `
Get-Range.ps1 `
TrackingUtils.ps1 `
Copy-ExcelWorkSheet.ps1 `
Set-CellStyle.ps1 `
plot.ps1
if ('' -eq $InstallDirectory)

View File

@@ -27,6 +27,7 @@ $targetFiles = echo `
Get-Range.ps1 `
TrackingUtils.ps1 `
Copy-ExcelWorkSheet.ps1 `
Set-CellStyle.ps1 `
plot.ps1
ls $targetFiles |

View File

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

13
Set-CellStyle.ps1 Normal file
View File

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