mirror of
https://github.com/dfinke/ImportExcel.git
synced 2026-01-09 12:03:21 +00:00
Compare worksheet test and bugfix
Bug when worksheets had different names and were in different files, sometimes colour was not set
This commit is contained in:
@@ -175,9 +175,17 @@
|
||||
else {$xl2 = Open-ExcelPackage -path $Differencefile }
|
||||
foreach ($u in $updates) {
|
||||
foreach ($p in $propList) {
|
||||
if ($u.group[0]._file -eq $Referencefile) {
|
||||
$ws1 = $xl1.Workbook.Worksheets[$u.Group[0]._sheet]
|
||||
$ws2 = $xl2.Workbook.Worksheets[$u.Group[1]._sheet]
|
||||
}
|
||||
else {
|
||||
$ws1 = $xl2.Workbook.Worksheets[$u.Group[0]._sheet]
|
||||
$ws2 = $xl1.Workbook.Worksheets[$u.Group[1]._sheet]
|
||||
}
|
||||
if($u.Group[0].$p -ne $u.Group[1].$p ) {
|
||||
Set-Format -WorkSheet $xl1.Workbook.Worksheets[$u.Group[0]._sheet] -Range ($Columns[$p] + $u.Group[0]._Row) -FontColor $FontColor
|
||||
Set-Format -WorkSheet $xl2.Workbook.Worksheets[$u.Group[1]._sheet] -Range ($Columns[$p] + $u.Group[1]._Row) -FontColor $FontColor
|
||||
Set-Format -WorkSheet $ws1 -Range ($Columns[$p] + $u.Group[0]._Row) -FontColor $FontColor
|
||||
Set-Format -WorkSheet $ws1 -Range ($Columns[$p] + $u.Group[1]._Row) -FontColor $FontColor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
182
Compare-WorkSheet.tests.ps1
Normal file
182
Compare-WorkSheet.tests.ps1
Normal file
@@ -0,0 +1,182 @@
|
||||
describe "Compare Worksheet" {
|
||||
|
||||
del "$env:temp\server*.xlsx"
|
||||
[System.Collections.ArrayList]$s = get-service | Select-Object -Property *
|
||||
|
||||
$s | Export-Excel -Path $env:temp\server1.xlsx
|
||||
|
||||
#$s is a zero based array, excel rows are 1 based and excel has a header row so Excel rows will be 2 + index in $s
|
||||
$row4Displayname = $s[2].DisplayName
|
||||
$s[2].DisplayName = "Changed from the orginal"
|
||||
|
||||
$d = $s[-1] | Select-Object -Property *
|
||||
$d.DisplayName = "Dummy Service"
|
||||
$d.Name = "Dummy"
|
||||
$s.Insert(3,$d)
|
||||
|
||||
$row6Name = $s[5].name
|
||||
$s.RemoveAt(5)
|
||||
|
||||
$s | Export-Excel -Path $env:temp\server2.xlsx
|
||||
#Assume default worksheet name, (sheet1) and column header for key ("name")
|
||||
$comp = compare-WorkSheet "$env:temp\Server1.xlsx" "$env:temp\Server2.xlsx"
|
||||
|
||||
Context "Simple comparison output" {
|
||||
it "Found the right number of differences " {
|
||||
$comp | should not beNullOrEmpty
|
||||
$comp.Count | should be 4
|
||||
}
|
||||
it "Found the data row with a changed property " {
|
||||
$comp | should not beNullOrEmpty
|
||||
$comp[0]._Side | should be '=>'
|
||||
$comp[1]._Side | should be '<='
|
||||
$comp[0]._Row | should be 4
|
||||
$comp[1]._Row | should be 4
|
||||
$comp[1].Name | should be $comp[0].Name
|
||||
$comp[1].DisplayName | should be $row4Displayname
|
||||
$comp[0].DisplayName | should be "Changed from the orginal"
|
||||
}
|
||||
it "Found the inserted data row " {
|
||||
$comp | should not beNullOrEmpty
|
||||
$comp[2]._Side | should be '=>'
|
||||
$comp[2]._Row | should be 5
|
||||
$comp[2].Name | should be "Dummy"
|
||||
}
|
||||
it "Found the deleted data row " {
|
||||
$comp | should not beNullOrEmpty
|
||||
$comp[3]._Side | should be '<='
|
||||
$comp[3]._Row | should be 6
|
||||
$comp[3].Name | should be $row6Name
|
||||
}
|
||||
}
|
||||
|
||||
$null = compare-WorkSheet "$env:temp\Server1.xlsx" "$env:temp\Server2.xlsx" -BackgroundColor LightGreen
|
||||
$xl1 = Open-ExcelPackage -Path "$env:temp\Server1.xlsx"
|
||||
$xl2 = Open-ExcelPackage -Path "$env:temp\Server2.xlsx"
|
||||
$s1Sheet = $xl1.Workbook.Worksheets[1]
|
||||
$s2Sheet = $xl2.Workbook.Worksheets[1]
|
||||
|
||||
Context "Setting the background to highlight different rows" {
|
||||
it "set the background on the right rows " {
|
||||
$s1Sheet.Cells["4:4"].Style.Fill.BackgroundColor.Rgb | should be "FF90EE90"
|
||||
$s1Sheet.Cells["6:6"].Style.Fill.BackgroundColor.Rgb | should be "FF90EE90"
|
||||
$s2Sheet.Cells["4:4"].Style.Fill.BackgroundColor.Rgb | should be "FF90EE90"
|
||||
$s2Sheet.Cells["5:5"].Style.Fill.BackgroundColor.Rgb | should be "FF90EE90"
|
||||
}
|
||||
it "Didn't set other cells " {
|
||||
$s1Sheet.Cells["3:3"].Style.Fill.BackgroundColor.Rgb | should not be "FF90EE90"
|
||||
$s1Sheet.Cells["F4"].Style.Font.Color.Rgb | should beNullOrEmpty
|
||||
$s2Sheet.Cells["F4"].Style.Font.Color.Rgb | should beNullOrEmpty
|
||||
$s2Sheet.Cells["3:3"].Style.Fill.BackgroundColor.Rgb | should not be "FF90EE90"
|
||||
}
|
||||
}
|
||||
|
||||
Close-ExcelPackage -ExcelPackage $xl1 -NoSave
|
||||
Close-ExcelPackage -ExcelPackage $xl2 -NoSave
|
||||
$null = compare-WorkSheet "$env:temp\Server1.xlsx" "$env:temp\Server2.xlsx" -AllDataBackgroundColor white -BackgroundColor LightGreen -FontColor DarkRed
|
||||
$xl1 = Open-ExcelPackage -Path "$env:temp\Server1.xlsx"
|
||||
$xl2 = Open-ExcelPackage -Path "$env:temp\Server2.xlsx"
|
||||
$s1Sheet = $xl1.Workbook.Worksheets[1]
|
||||
$s2Sheet = $xl2.Workbook.Worksheets[1]
|
||||
|
||||
Context "Setting the forgound to highlight changed properties" {
|
||||
it "Added foreground colour to the right cells " {
|
||||
$s1Sheet.Cells["4:4"].Style.Fill.BackgroundColor.Rgb | should be "FF90EE90"
|
||||
$s1Sheet.Cells["6:6"].Style.Fill.BackgroundColor.Rgb | should be "FF90EE90"
|
||||
$s2Sheet.Cells["4:4"].Style.Fill.BackgroundColor.Rgb | should be "FF90EE90"
|
||||
$s2Sheet.Cells["5:5"].Style.Fill.BackgroundColor.Rgb | should be "FF90EE90"
|
||||
$s1Sheet.Cells["F4"].Style.Font.Color.Rgb | should be "FF8B0000"
|
||||
$s2Sheet.Cells["F4"].Style.Font.Color.Rgb | should be "FF8B0000"
|
||||
}
|
||||
it "Didn't set the foreground on other cells " {
|
||||
$s1Sheet.Cells["F5"].Style.Font.Color.Rgb | should beNullOrEmpty
|
||||
$s2Sheet.Cells["F5"].Style.Font.Color.Rgb | should beNullOrEmpty
|
||||
$s1Sheet.Cells["G4"].Style.Font.Color.Rgb | should beNullOrEmpty
|
||||
$s2Sheet.Cells["G4"].Style.Font.Color.Rgb | should beNullOrEmpty
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Close-ExcelPackage -ExcelPackage $xl1 -NoSave
|
||||
Close-ExcelPackage -ExcelPackage $xl2 -NoSave
|
||||
|
||||
[System.Collections.ArrayList]$s = get-service | Select-Object -Property * -ExcludeProperty Name
|
||||
|
||||
$s | Export-Excel -Path $env:temp\server1.xlsx -WorkSheetname Server1
|
||||
|
||||
#$s is a zero based array, excel rows are 1 based and excel has a header row so Excel rows will be 2 + index in $s
|
||||
$row4Displayname = $s[2].DisplayName
|
||||
$s[2].DisplayName = "Changed from the orginal"
|
||||
|
||||
$d = $s[-1] | Select-Object -Property *
|
||||
$d.DisplayName = "Dummy Service"
|
||||
$d.ServiceName = "Dummy"
|
||||
$s.Insert(3,$d)
|
||||
|
||||
$row6Name = $s[5].ServiceName
|
||||
$s.RemoveAt(5)
|
||||
|
||||
$s[10].ServiceType = "Changed should not matter"
|
||||
|
||||
$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")
|
||||
$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
|
||||
|
||||
$xl1 = Open-ExcelPackage -Path "$env:temp\Server1.xlsx"
|
||||
$xl2 = Open-ExcelPackage -Path "$env:temp\Server2.xlsx"
|
||||
|
||||
$s1Sheet = $xl1.Workbook.Worksheets["server1"]
|
||||
$s2Sheet = $xl2.Workbook.Worksheets["server2"]
|
||||
Context "More complex comparison output etc different worksheet names " {
|
||||
it "Found the right number of differences " {
|
||||
$comp | should not beNullOrEmpty
|
||||
$comp.Count | should be 4
|
||||
}
|
||||
it "Found the data row with a changed property " {
|
||||
$comp | should not beNullOrEmpty
|
||||
$comp[0]._Side | should be '=>'
|
||||
$comp[1]._Side | should be '<='
|
||||
$comp[0]._Row | should be 4
|
||||
$comp[1]._Row | should be 4
|
||||
$comp[1].ServiceName | should be $comp[0].ServiceName
|
||||
$comp[1].DisplayName | should be $row4Displayname
|
||||
$comp[0].DisplayName | should be "Changed from the orginal"
|
||||
}
|
||||
it "Found the inserted data row " {
|
||||
$comp | should not beNullOrEmpty
|
||||
$comp[2]._Side | should be '=>'
|
||||
$comp[2]._Row | should be 5
|
||||
$comp[2].ServiceName | should be "Dummy"
|
||||
}
|
||||
it "Found the deleted data row " {
|
||||
$comp | should not beNullOrEmpty
|
||||
$comp[3]._Side | should be '<='
|
||||
$comp[3]._Row | should be 6
|
||||
$comp[3].ServiceName | should be $row6Name
|
||||
}
|
||||
|
||||
it "set the background on the right rows " {
|
||||
$s1Sheet.Cells["4:4"].Style.Fill.BackgroundColor.Rgb | should be "FFFFFFFF"
|
||||
$s1Sheet.Cells["6:6"].Style.Fill.BackgroundColor.Rgb | should be "FFFFFFFF"
|
||||
$s2Sheet.Cells["4:4"].Style.Fill.BackgroundColor.Rgb | should be "FFFFFFFF"
|
||||
$s2Sheet.Cells["5:5"].Style.Fill.BackgroundColor.Rgb | should be "FFFFFFFF"
|
||||
|
||||
$s1Sheet.Cells["E4"].Style.Font.Color.Rgb | should be "FFFF0000"
|
||||
$s2Sheet.Cells["E4"].Style.Font.Color.Rgb | should be "FFFF0000"
|
||||
}
|
||||
it "Didn't set other cells " {
|
||||
$s1Sheet.Cells["3:3"].Style.Fill.BackgroundColor.Rgb | should not be "FFFFFFFF"
|
||||
$s2Sheet.Cells["3:3"].Style.Fill.BackgroundColor.Rgb | should not be "FFFFFFFF"
|
||||
$s1Sheet.Cells["E5"].Style.Font.Color.Rgb | should beNullOrEmpty
|
||||
$s2Sheet.Cells["E5"].Style.Font.Color.Rgb | should beNullOrEmpty
|
||||
$s1Sheet.Cells["F4"].Style.Font.Color.Rgb | should beNullOrEmpty
|
||||
$s2Sheet.Cells["F4"].Style.Font.Color.Rgb | should beNullOrEmpty
|
||||
}
|
||||
|
||||
}
|
||||
Close-ExcelPackage -ExcelPackage $xl1 -NoSave -Show
|
||||
Close-ExcelPackage -ExcelPackage $xl2 -NoSave -Show
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfi
|
||||
- `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`
|
||||
For this release I've fixed heaven only knows how many typos and proof reading errors in the help for these two, but the code is unchanged - although correcting the spelling of Merge-MultipleSheets is potentially a breaking change (and it is still plural!)
|
||||
also fixed a bug in compare worksheet where color might not be applied correctly when the worksheets came from different files and had different name.
|
||||
- `Join-Worksheet` is **new** for ths release. At it's simplest it copies all the data in Worksheet A to the end of Worksheet B
|
||||
- Add-Worksheet
|
||||
- I have moved this from ImportExcel.psm1 to ExportExcel.ps1 and it now can move a new worksheet to the right place, and can copy an existing worksheet (from the same or a different workbook) to a new one, and I set the Set return-type to aid intellisense
|
||||
@@ -68,6 +69,8 @@ iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfi
|
||||
- Added an option to only set the number format if doesn't match the default for the sheet.
|
||||
-Export-Excel Pester Tests
|
||||
- I have converted examples 1-9, 11 and 13 from Export-Excel help into tests and have added some additional tests, and extra parameters to the example command to ge better test coverage. The test so far has 184 "should" conditions grouped as 58 "IT" statements; but is still a work in progress.
|
||||
-Compare-Worksheet pester tests
|
||||
|
||||
---
|
||||
|
||||
|
||||
|
||||
@@ -175,9 +175,17 @@
|
||||
else {$xl2 = Open-ExcelPackage -path $Differencefile }
|
||||
foreach ($u in $updates) {
|
||||
foreach ($p in $propList) {
|
||||
if ($u.group[0]._file -eq $Referencefile) {
|
||||
$ws1 = $xl1.Workbook.Worksheets[$u.Group[0]._sheet]
|
||||
$ws2 = $xl2.Workbook.Worksheets[$u.Group[1]._sheet]
|
||||
}
|
||||
else {
|
||||
$ws1 = $xl2.Workbook.Worksheets[$u.Group[0]._sheet]
|
||||
$ws2 = $xl1.Workbook.Worksheets[$u.Group[1]._sheet]
|
||||
}
|
||||
if($u.Group[0].$p -ne $u.Group[1].$p ) {
|
||||
Set-Format -WorkSheet $xl1.Workbook.Worksheets[$u.Group[0]._sheet] -Range ($Columns[$p] + $u.Group[0]._Row) -FontColor $FontColor
|
||||
Set-Format -WorkSheet $xl2.Workbook.Worksheets[$u.Group[1]._sheet] -Range ($Columns[$p] + $u.Group[1]._Row) -FontColor $FontColor
|
||||
Set-Format -WorkSheet $ws1 -Range ($Columns[$p] + $u.Group[0]._Row) -FontColor $FontColor
|
||||
Set-Format -WorkSheet $ws1 -Range ($Columns[$p] + $u.Group[1]._Row) -FontColor $FontColor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user