See readme.md

This commit is contained in:
jhoneill
2018-07-05 00:29:28 +01:00
parent a72d489707
commit 06986df83a
10 changed files with 118 additions and 59 deletions

Binary file not shown.

View File

@@ -690,7 +690,7 @@
} }
#If named range exists, update it, else create it #If named range exists, update it, else create it
if ($ws.Names[$RangeName]) { $ws.Names[$rangename].Address = $ws.Cells[$dataRange].FullAddressAbsolute } if ($ws.Names[$RangeName]) { $ws.Names[$rangename].Address = $ws.Cells[$dataRange].FullAddressAbsolute }
else {$ws.Names.Add($RangeName, $ws.Cells[$dataRange]) | Out-Null } else {$ws.Names.Add($RangeName, $ws.Cells[$0ange]) | Out-Null }
} }
} }
Catch { Write-Warning -Message "Failed adding range '$RangeName' to worksheet '$WorkSheetname': $_" } Catch { Write-Warning -Message "Failed adding range '$RangeName' to worksheet '$WorkSheetname': $_" }
@@ -822,8 +822,8 @@
foreach ($chartDef in $ExcelChartDefinition) { foreach ($chartDef in $ExcelChartDefinition) {
$params = @{} $params = @{}
$chardef.PSObject.Properties | ForEach-Object {if ($_.value -ne $null) {$params[$_.name] = $_.value}} $chartDef.PSObject.Properties | ForEach-Object {if ($_.value -ne $null) {$params[$_.name] = $_.value}}
Add-ExcelChart $params Add-ExcelChart @params
} }
foreach ($ct in $ConditionalText) { foreach ($ct in $ConditionalText) {
@@ -842,7 +842,7 @@
if ($CellStyleSB) { if ($CellStyleSB) {
try { try {
$TotalRows = $ws.Dimension.Rows $TotalRows = $ws.Dimension.Rows
$LastColumn = (Get-ExcelColumnName $ws.Dimension.Columns).ColumnName $LastColumn = $ws.Dimension.Address -replace "^.*:(\w*)\d+$" , '$1'
& $CellStyleSB $ws $TotalRows $LastColumn & $CellStyleSB $ws $TotalRows $LastColumn
} }
catch {Write-Warning -Message "Failed processing CellStyleSB in worksheet '$WorkSheetname': $_"} catch {Write-Warning -Message "Failed processing CellStyleSB in worksheet '$WorkSheetname': $_"}
@@ -1164,30 +1164,84 @@ function Add-PivotTable {
} }
} }
function Add-ExcelChart { function Add-ExcelChart {
[cmdletbinding()]
param( param(
$Worksheet, [OfficeOpenXml.ExcelWorksheet]$Worksheet,
$Title = "Chart Title", [String]$Title = "Chart Title",
$Header, #$Header, Not used but referenced previously
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = "ColumnStacked", [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = "ColumnStacked",
$XRange, $XRange,
$YRange, $YRange,
$Width = 500, [int]$Width = 500,
$Height = 350, [int]$Height = 350,
$Row = 0, [int]$Row = 0,
$RowOffSetPixels = 10, [int]$RowOffSetPixels = 10,
$Column = 6, [int]$Column = 6,
$ColumnOffSetPixels = 5, [int]$ColumnOffSetPixels = 5,
[OfficeOpenXml.Drawing.Chart.eLegendPosition]$LegendPostion,
$LegendSize,
[Switch]$legendBold,
[Switch]$NoLegend, [Switch]$NoLegend,
[Switch]$ShowCategory, [Switch]$ShowCategory,
[Switch]$ShowPercent, [Switch]$ShowPercent,
$SeriesHeader $SeriesHeader,
) [Switch]$TitleBold,
[Int]$TitleSize ,
[String]$XAxisTitleText,
[Switch]$XAxisTitleBold,
$XAxisTitleSize ,
[string]$XAxisNumberformat,
[double]$XMajorUnit,
[double]$XMinorUnit,
[double]$XMaxValue,
[double]$XMinValue,
[OfficeOpenXml.Drawing.Chart.eAxisPosition]$XAxisPosition ,
[String]$YAxisTitleText,
[Switch]$YAxisTitleBold,
$YAxisTitleSize,
[string]$YAxisNumberformat,
[double]$YMajorUnit,
[double]$YMinorUnit,
[double]$YMaxValue,
[double]$YMinValue,
[OfficeOpenXml.Drawing.Chart.eAxisPosition]$YAxisPosition )
try { try {
$ChartName = 'Chart' + (Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.', '' $ChartName = 'Chart' + (Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.', ''
$chart = $Worksheet.Drawings.AddChart($ChartName, $ChartType) $chart = $Worksheet.Drawings.AddChart($ChartName, $ChartType)
$chart.Title.Text = $Title $chart.Title.Text = $Title
if ($TitleBold) {$chart.Title.Font.Bold = $true}
if ($TitleSize) {$chart.Title.Font.Size = $TitleSize}
if ($NoLegend) { $chart.Legend.Remove() } if ($NoLegend) { $chart.Legend.Remove() }
else {
if ($LegendPostion) {$Chart.Legend.Position = $LegendPostion}
if ($LegendSize) {$chart.Legend.Font.Size = $LegendSize}
if ($legendBold) {$chart.Legend.Font.Bold = $legendBold}
}
if ($XAxisTitleText) {
$chart.XAxis.Title.Text = $XAxisTitleText
if ($XAxisTitleBold) {$chart.XAxis.Title.Font.Bold = $true}
if ($XAxisTitleSize) {$chart.XAxis.Title.Font.Size = $XAxisTitleSize}
}
if ($XAxisPosition) {$chart.XAxis.AxisPosition = $XAxisPosition}
if ($XMajorUnit) {$chart.XAxis.MajorUnit = $XMajorUnit}
if ($XMinorUnit) {$chart.XAxis.MinorUnit = $XMinorUnit}
if ($XMinValue) {$chart.XAxis.MinValue = $XMinValue}
if ($XMaxValue) {$chart.XAxis.MaxValue = $XMaxValue}
if ($XAxisNumberformat) {$chart.XAxis.Format = $XAxisNumberformat}
if ($YAxisTitleText) {
$chart.YAxis.Title.Text = $YAxisTitleText
if ($YAxisTitleBold) {$chart.YAxis.Title.Font.Bold = $true}
if ($YAxisTitleSize) {$chart.YAxis.Title.Font.Size = $YAxisTitleSize}
}
if ($YAxisPosition) {$chart.YAxis.AxisPosition = $YAxisPosition}
if ($YMajorUnit) {$chart.YAxis.MajorUnit = $YMajorUnit}
if ($YMinorUnit) {$chart.YAxis.MinorUnit = $YMinorUnit}
if ($YMinValue) {$chart.YAxis.MinValue = $YMinValue}
if ($YMaxValue) {$chart.YAxis.MaxValue = $YMaxValue}
if ($YAxisNumberformat) {$chart.YAxis.Format = $YAxisNumberformat}
if ($chart.Datalabel -ne $null) { if ($chart.Datalabel -ne $null) {
$chart.Datalabel.ShowCategory = [boolean]$ShowCategory $chart.Datalabel.ShowCategory = [boolean]$ShowCategory

View File

@@ -1,7 +1,7 @@
function Get-XYRange { function Get-XYRange {
param($targetData) param($targetData)
$record = $targetData| select -First 1 $record = $targetData| Select-Object -First 1
$p=$record.psobject.Properties.name $p=$record.psobject.Properties.name
$infer = for ($idx = 0; $idx -lt $p.Count; $idx++) { $infer = for ($idx = 0; $idx -lt $p.Count; $idx++) {
@@ -15,12 +15,12 @@ function Get-XYRange {
Name = $name Name = $name
Value = $value Value = $value
DataType = $result.DataType DataType = $result.DataType
ExcelColumn = (Get-ExcelColumnName ($idx+1)).ColumnName ExcelColumn = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1("R[1]C[$($idx+1)]", 0 , 0) -replace "\d+", "" #(Get-ExcelColumnName ($idx + 1)).ColumnName
} }
} }
[PSCustomObject]@{ [PSCustomObject]@{
XRange = $infer | ? {$_.datatype -match 'string'} | select -First 1 excelcolumn, name XRange = $infer | ? {$_.datatype -match 'string'} | Select-Object -First 1 excelcolumn, name
YRange = $infer | ? {$_.datatype -match 'int|double'} |select -First 1 excelcolumn, name YRange = $infer | ? {$_.datatype -match 'int|double'} |Select-Object -First 1 excelcolumn, name
} }
} }

View File

@@ -1,4 +1,6 @@
function New-ExcelChart { function New-ExcelChartDefinition {
[cmdletbinding()]
[Alias("New-ExcelChart")] #This was the former name. The new name reflects that we are defining a chart, not making one in the workbook.
param( param(
$Title = "Chart Title", $Title = "Chart Title",
$Header, $Header,
@@ -16,10 +18,9 @@ function New-ExcelChart {
[Switch]$ShowPercent, [Switch]$ShowPercent,
$SeriesHeader $SeriesHeader
) )
if ( $Header ) {Write-Warning "The header parameter is ignored."} #Nothing was done with it when creating a chart.
[PSCustomObject]@{ [PSCustomObject]@{
Title = $Title Title = $Title
Header=$Header
ChartType = $ChartType ChartType = $ChartType
XRange = $XRange XRange = $XRange
YRange = $YRange YRange = $YRange
@@ -29,9 +30,9 @@ function New-ExcelChart {
RowOffSetPixels = $RowOffSetPixels RowOffSetPixels = $RowOffSetPixels
Column = $Column Column = $Column
ColumnOffSetPixels = $ColumnOffSetPixels ColumnOffSetPixels = $ColumnOffSetPixels
NoLegend = if($NoLegend) {$true} else {$false} NoLegend = $NoLegend -as [Boolean]
ShowCategory = if($ShowCategory) {$true} else {$false} ShowCategory = $ShowCategory-as [Boolean]
ShowPercent = if($ShowPercent) {$true} else {$false} ShowPercent = $ShowPercent -as [Boolean]
SeriesHeader = $SeriesHeader SeriesHeader = $SeriesHeader
} }
} }

View File

@@ -32,8 +32,13 @@ To install to your personal modules folder (e.g. ~\Documents\WindowsPowerShell\M
iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfinke/ImportExcel/master/Install.ps1') iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfinke/ImportExcel/master/Install.ps1')
``` ```
# What's new # What's new to 4th July 18
- Moved chart creatation into its own function (Add-Excel chart) within Export-Excel.ps1. Renamed New-Excelchart to New-ExcelChartDefinition to make it clearer that it is not making anything in the workbook (but for compatiblity put an alias of New-ExcelChart in so existing code does not break). Found -Header does nothing, so removed it.
- Fixed a bug introduced into Compare-Worksheet by the change descibed in the June changes below, this meant the font color was only being set in one sheet, when a row was changed. Also found that the PowerShell ISE and shell return Compare-Object resuls in different sequences which broke some tests. Applied a sort to ensure things are in a predictable order.
- Added chart tests to Export-Excel.tests.ps1.
- Removed (2) calls to Get-ExcelColumnName
# New in June 18
- New commands - Diff , Merge and Join - New commands - Diff , Merge and Join
- `Compare-Worksheet` (introduced in 5.0) uses the built in `Compare-object` command, to output a command-line DIFF and/or colour the worksheet to show differences. For example, if my sheets are Windows services the *extra* rows or rows where the startup status has changed get highlighted - `Compare-Worksheet` (introduced in 5.0) uses the built in `Compare-object` command, to output a command-line DIFF and/or colour the worksheet to show differences. For example, if my sheets are Windows services the *extra* rows or rows where the startup status has changed get highlighted
- `Merge-Worksheet` (also introduced in 5.0) joins two lumps, side by highlighting the differences. So now I can have server A's services and Server Bs Services on the same page. I figured out a way to do multiple sheets. So I can have Server A,B,C,D on one page :-) that is `Merge-MultpleSheets` - `Merge-Worksheet` (also introduced in 5.0) joins two lumps, side by highlighting the differences. So now I can have server A's services and Server Bs Services on the same page. I figured out a way to do multiple sheets. So I can have Server A,B,C,D on one page :-) that is `Merge-MultpleSheets`

View File

@@ -25,7 +25,7 @@ Describe "Compare Worksheet" {
$s | Export-Excel -Path $env:temp\server2.xlsx $s | Export-Excel -Path $env:temp\server2.xlsx
#Assume default worksheet name, (sheet1) and column header for key ("name") #Assume default worksheet name, (sheet1) and column header for key ("name")
$comp = compare-WorkSheet "$env:temp\Server1.xlsx" "$env:temp\Server2.xlsx" $comp = compare-WorkSheet "$env:temp\Server1.xlsx" "$env:temp\Server2.xlsx" | Sort-Object -Property _row, _file
Context "Simple comparison output" { Context "Simple comparison output" {
it "Found the right number of differences " { it "Found the right number of differences " {
@@ -38,8 +38,8 @@ Describe "Compare Worksheet" {
$comp[0]._Row | should be 4 $comp[0]._Row | should be 4
$comp[1]._Row | should be 4 $comp[1]._Row | should be 4
$comp[1].Name | should be $comp[0].Name $comp[1].Name | should be $comp[0].Name
$comp[1].DisplayName | should be $row4Displayname $comp[0].DisplayName | should be $row4Displayname
$comp[0].DisplayName | should be "Changed from the orginal" $comp[1].DisplayName | should be "Changed from the orginal"
} }
it "Found the inserted data row " { it "Found the inserted data row " {
$comp | should not beNullOrEmpty $comp | should not beNullOrEmpty
@@ -125,14 +125,13 @@ Describe "Compare Worksheet" {
$s | Select-Object -Property ServiceName, DisplayName, StartType, ServiceType | Export-Excel -Path $env:temp\server2.xlsx -WorkSheetname server2 $s | Select-Object -Property ServiceName, DisplayName, StartType, ServiceType | Export-Excel -Path $env:temp\server2.xlsx -WorkSheetname server2
#Assume default worksheet name, (sheet1) and column header for key ("name") #Assume default worksheet name, (sheet1) and column header for key ("name")
$comp = compare-WorkSheet "$env:temp\Server1.xlsx" "$env:temp\Server2.xlsx" -WorkSheetName Server1,Server2 -Key ServiceName -Property DisplayName,StartType -AllDataBackgroundColor AliceBlue -BackgroundColor White -FontColor Red $comp = compare-WorkSheet "$env:temp\Server1.xlsx" "$env:temp\Server2.xlsx" -WorkSheetName Server1,Server2 -Key ServiceName -Property DisplayName,StartType -AllDataBackgroundColor AliceBlue -BackgroundColor White -FontColor Red | Sort-Object _row,_file
$xl1 = Open-ExcelPackage -Path "$env:temp\Server1.xlsx" $xl1 = Open-ExcelPackage -Path "$env:temp\Server1.xlsx"
$xl2 = Open-ExcelPackage -Path "$env:temp\Server2.xlsx" $xl2 = Open-ExcelPackage -Path "$env:temp\Server2.xlsx"
$s1Sheet = $xl1.Workbook.Worksheets["server1"] $s1Sheet = $xl1.Workbook.Worksheets["server1"]
$s2Sheet = $xl2.Workbook.Worksheets["server2"] $s2Sheet = $xl2.Workbook.Worksheets["server2"]
Context "More complex comparison output etc different worksheet names " { Context "More complex comparison: output check and different worksheet names " {
it "Found the right number of differences " { it "Found the right number of differences " {
$comp | should not beNullOrEmpty $comp | should not beNullOrEmpty
$comp.Count | should be 4 $comp.Count | should be 4
@@ -143,8 +142,8 @@ Describe "Compare Worksheet" {
$comp[0]._Row | should be 4 $comp[0]._Row | should be 4
$comp[1]._Row | should be 4 $comp[1]._Row | should be 4
$comp[1].ServiceName | should be $comp[0].ServiceName $comp[1].ServiceName | should be $comp[0].ServiceName
$comp[1].DisplayName | should be $row4Displayname $comp[0].DisplayName | should be $row4Displayname
$comp[0].DisplayName | should be "Changed from the orginal" $comp[1].DisplayName | should be "Changed from the orginal"
} }
it "Found the inserted data row " { it "Found the inserted data row " {
$comp | should not beNullOrEmpty $comp | should not beNullOrEmpty

Binary file not shown.

Binary file not shown.

Binary file not shown.