From 8f2dd982c5bc75065f5e2c9e1f6b2ceda0beb8e3 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Wed, 2 May 2018 12:38:13 +0100 Subject: [PATCH] Lots of changes to the compare-worksheet module --- ColorCompletion.ps1 | 33 ++++---- compare-WorkSheet.ps1 | 181 ++++++++++++++++++++++++------------------ 2 files changed, 120 insertions(+), 94 deletions(-) diff --git a/ColorCompletion.ps1 b/ColorCompletion.ps1 index 17f80aa..b771a4d 100644 --- a/ColorCompletion.ps1 +++ b/ColorCompletion.ps1 @@ -6,20 +6,21 @@ } if (Get-Command -Name register-argumentCompleter -ErrorAction SilentlyContinue) { - Register-ArgumentCompleter -CommandName Export-Excel -ParameterName TitleBackgroundColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Add-ConditionalFormatting -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Add-ConditionalFormatting -ParameterName DataBarColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Add-ConditionalFormatting -ParameterName ForeGroundColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Compare-Worksheet -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Compare-Worksheet -ParameterName FontColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Compare-Worksheet -ParameterName FontColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Set-Format -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Set-Format -ParameterName FontColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Set-Format -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Set-Column -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Set-Column -ParameterName FontColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Set-Column -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Set-Row -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Set-Row -ParameterName FontColor -ScriptBlock $Function:ColorCompletion - Register-ArgumentCompleter -CommandName Set-Row -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Export-Excel -ParameterName TitleBackgroundColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Add-ConditionalFormatting -ParameterName AllDataBackgroundColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Add-ConditionalFormatting -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Add-ConditionalFormatting -ParameterName DataBarColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Add-ConditionalFormatting -ParameterName ForeGroundColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Compare-Worksheet -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Compare-Worksheet -ParameterName FontColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Compare-Worksheet -ParameterName FontColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Set-Format -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Set-Format -ParameterName FontColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Set-Format -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Set-Column -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Set-Column -ParameterName FontColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Set-Column -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Set-Row -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Set-Row -ParameterName FontColor -ScriptBlock $Function:ColorCompletion + Register-ArgumentCompleter -CommandName Set-Row -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion } \ No newline at end of file diff --git a/compare-WorkSheet.ps1 b/compare-WorkSheet.ps1 index 89886c9..c938f6f 100644 --- a/compare-WorkSheet.ps1 +++ b/compare-WorkSheet.ps1 @@ -1,4 +1,4 @@ -Function Compare-Worksheet { +Function Compare-WorkSheet { <# .Synopsis Compares two worksheets with the same name in different files. @@ -26,27 +26,30 @@ This builds on the previous example: this time Where two rows in the services have the same name, this will also highlight the changed cells in red. This example will open the Excel files and omits the -key parameter because "Name" will be assumed to the label for the key column .Example - Compare-WorkSheet 'Pester-tests.xlsx' 'Pester-tests.xlsx' -WorkSheetName 'Server1','Server2' -Property "full Description","Executed","Result" -Key "full Description" -FontColor Red -TabColor Yellow -Show + Compare-WorkSheet 'Pester-tests.xlsx' 'Pester-tests.xlsx' -WorkSheetName 'Server1','Server2' -Property "full Description","Executed","Result" -Key "full Description" This time the reference file and the difference file are the same file and two different sheets are used. Because the tests include the machine name and time the test was run only a limited set of columns. .Example - Compare-WorkSheet - 'Server1.xlsx' 'Server2.xlsx' -WorkSheetName general -Startrow 2 -Headername Label,value -Key Label -GridView - The "General" page has a title and two unlabelled columns with the CPU, Memory, Domain, Disk and so on - So this version starts at row 2 to skip the tiltle and labels the first column "label" and the Second "Value"; the label acts as the key - and the result is display on using grid view. Note that grid view works best when the number of columns is small. + Compare-WorkSheet 'Server1.xlsx' 'Server2.xlsx' -WorkSheetName general -Startrow 2 -Headername Label,value -Key Label -GridView -ExcludeDifferent + The "General" page has a title and two unlabelled columns with a row forCPU, Memory, Domain, Disk and so on + So the command is instructed to starts at row 2 to skip the title and to name the columns: the first is "label" and the Second "Value"; + the label acts as the key. This time we interested the rows which are the same in both sheets, + and the result is displayed using grid view. Note that grid view works best when the number of columns is small. + .Example + Compare-WorkSheet 'Server1.xlsx' 'Server2.xlsx' -WorkSheetName general -Startrow 2 -Headername Label,value -Key Label -BackgroundColor White -Show -AllDataBackgroundColor LightGray + This version of the previous command lightlights all the cells in lightgray and then sets the changed rows back to white; only + the unchanged rows are highlighted #> [cmdletbinding(DefaultParameterSetName)] Param( #First file to compare - [parameter(Mandatory=$true)] + [parameter(Mandatory=$true,Position=0)] $Referencefile , #Second file to compare - [parameter(Mandatory=$true)] + [parameter(Mandatory=$true,Position=1)] $Differencefile , #Name(s) of worksheets to compare. $WorkSheetName = "Sheet1", - #Name of a column which is unique and will be used to add a row to the DIFF object, default is "Name" - $Key = "Name" , #Properties to include in the DIFF - supports wildcards, default is "*" $Property = "*" , #Properties to exclude from the the search - supports wildcards @@ -58,82 +61,93 @@ [Parameter(ParameterSetName='C', Mandatory)] [switch]$NoHeader, #The row from where we start to import data, all rows above the StartRow are disregarded. By default this is the first row. - [int]$Startrow, + [int]$Startrow = 1, + #If specified, highlights all the cells - so you can make Equal cells one colour, and Diff cells another. + [System.Drawing.Color]$AllDataBackgroundColor, #If specified, highlights the DIFF rows [System.Drawing.Color]$BackgroundColor, #If specified identifies the tabs which contain DIFF rows (ignored if -backgroundColor is omitted) [System.Drawing.Color]$TabColor, + #Name of a column which is unique and will be used to add a row to the DIFF object, default is "Name" + $Key = "Name" , #If specified, highlights the DIFF columns in rows which have the same key. [System.Drawing.Color]$FontColor, - #If specified opens the Excel workbooks instead of outputting the diff to the console - [Switch]$Show, - #If specified, tries to the show the DIFF in a gridview. (Works best with few columns) - [switch]$GridView + #If specified opens the Excel workbooks instead of outputting the diff to the console (unless -passthru is also specified) + [Switch]$Show, + #If specified, the command tries to the show the DIFF in a Gridview and not on the console. (unless-Passthru is also specified). This Works best with few columns selected, and requires a key + [switch]$GridView, + #If specified -Passthrough full set of diff data is returned without filtering to the specified properties + [Switch]$PassThru, + #If specified the result will include equal rows as well. By default only different rows are returned + [Switch]$IncludeEqual, + #If Specified the result includes only the rows where both are equal + [Switch]$ExcludeDifferent ) - - $oneFile = ((Resolve-Path -Path $Referencefile).path -eq (Resolve-Path -Path $Differencefile).path) - - if ($Key -eq "Name" -and $NoHeader) {$key = "p1"} - + + #if the filenames don't resolve, give up now. + try { $oneFile = ((Resolve-Path -Path $Referencefile -ErrorAction Stop).path -eq (Resolve-Path -Path $Differencefile -ErrorAction Stop).path)} + Catch { Write-Warning -Message "Could not Resolve the filenames." ; return } + #If we have one file , we mush have two different worksheet names. If we have two files we can a single string or two strings. if ($onefile -and ( ($WorkSheetName.count -ne 2) -or $WorkSheetName[0] -eq $WorkSheetName[1] ) ) { Write-Warning -Message "If both the Reference and difference file are the same then worksheet name must provide 2 different names" return } - if ($WorkSheetName.count -eq 2) {$worksheet1 = $WorkSheetName[0] ; $WorkSheet2 = $WorkSheetName[1]} - elseif ($WorkSheetName -is [string]) {$worksheet1 = $WorkSheet2 = $WorkSheetName} + if ($WorkSheetName.count -eq 2) {$worksheet1 = $WorkSheetName[0] ; $WorkSheet2 = $WorkSheetName[1]} + elseif ($WorkSheetName -is [string]) {$worksheet1 = $WorkSheet2 = $WorkSheetName} else {Write-Warning -Message "You must provide either a single worksheet name or two names." ; return } - #If the paths are wrong, files are locked or the worksheet names are wrong we won't be able to continue $params= @{ ErrorAction = [System.Management.Automation.ActionPreference]::Stop } foreach ($p in @("HeaderName","NoHeader","StartRow")) {if ($PSBoundParameters[$p]) {$params[$p] = $PSBoundParameters[$p]}} - try { + try { $Sheet1 = Import-Excel -Path $Referencefile -WorksheetName $WorkSheet1 @params $Sheet2 = Import-Excel -Path $Differencefile -WorksheetName $WorkSheet2 @Params } - Catch {Write-Warning -Message "Could not read the worksheet from $Referencefile and/or $Differencefile." ; return } - + Catch {Write-Warning -Message "Could not read the worksheet from $Referencefile and/or $Differencefile." ; return } + #Get Column headings and create a hash table of Name to column letter. $headings = $Sheet1[-1].psobject.Properties.name # This preserves the sequence - using get-member would sort them alphabetically! - $Columns = @{} - $i = 65 ; foreach ($h in $headings) {$Columns[$h] = [char]($i ++) } + $headings | ForEach-Object -Begin {$columns = @{} ; } -Process {$Columns[$_] = [char]($i ++) } + + #Make a list of property headings using the Property (default "*") and ExcludeProperty parameters + if ($Key -eq "Name" -and $NoHeader) {$key = "p1"} + $propList = @() + foreach ($p in $Property) {$propList += ($headings.where({$_ -like $p}) )} + foreach ($p in $ExcludeProperty) {$propList = $propList.where({$_ -notlike $p}) } + if (($headings -contains $key) -and ($propList -notcontains $Key)) {$propList += $Key} + $propList = $propList | Select-Object -Unique + if ($propList.Count -eq 0) {Write-Warning -Message "No Columns are selected with -Property = '$Property' and -excludeProperty = '$ExcludeProperty'." ; return} - #Make a list of properties headings using the Property (default "*") and ExcludeProperty parameters - $PropList = @() - foreach ($p in $Property) {$PropList += ($headings.where({$_ -like $p}) )} - foreach ($p in $ExcludeProperty) {$PropList = $PropList.where({$_ -notlike $p}) } - $PropList = $PropList | Select-Object -Unique - if (($headings -contains $key) -and ($PropList -notcontains $Key)) {$PropList += $Key} - if ($PropList.Count -eq 0) {Write-Warning -Message "No Columns are selected with -Property = '$Property' and -excludeProperty = '$ExcludeProperty'." ; return} - - #If we add the row numbes to data and include them in the diff, inserting a row will mean all subsequent rows are different so instead ... - #... build hash tables with the "key" column as the key and the row in the spreadsheet where it appears as the value. Row 1 is headers so the first data row is 2 - $rows1 = @{} ; - $rows2 = @{} ; - if ($PropList -contains $Key) { - $i = 2 ; foreach ($row in $Sheet1) {$rows1[$row.$key] = ($i ++) } - $i = 2 ; foreach ($row in $Sheet2) {$rows2[$row.$key] = ($i ++) } - } - else {Write-Warning -Message "Could not find a column '$key' to use as a key - DIFF rows will not have numbers."} - - #Do the comparison and add file,sheet and row to the result - these are prefixed with "_" to show they are added but the addition still might fail so make sure we have some DIFF - $diff = Compare-Object $Sheet1 $Sheet2 -Property $PropList - $diff = $diff | Select-Object -Property (@( - @{n="_Side"; e={$_.SideIndicator }} - @{n="_File"; e={if ($_.SideIndicator -eq '=>') {$Differencefile} else {$Referencefile } }} , - @{n="_Sheet"; e={if ($_.SideIndicator -eq '=>') {$worksheet2 } else {$worksheet1 } }} , - @{n='_Row'; e={if ($_.$key -and $_.SideIndicator -eq '=>') {$rows2[$_.$key]} elseif ($_.$key) {$rows1[$_.$key]} else { "" } }} - ) + $PropList) | Sort-Object -Property row,file - - #if BackgroundColor was specified, set it on extra or extra or changed rows - but remember we we only have row numbers if we have a key - if (($PropList -contains $Key) -and $BackgroundColor) { - #Differences may only exist in one file. So gather the changes for each file; open the file, update each impacted row, save the file - $updates = $diff | Group-object -Property "_File" + #Add RowNumber, Sheetname and file name to every row + $i = $startRow + 1 ; foreach ($row in $Sheet1) {Add-Member -InputObject $row -MemberType NoteProperty -Name "_Row" -Value ($i ++) + Add-Member -InputObject $row -MemberType NoteProperty -Name "_Sheet" -Value $worksheet1 + Add-Member -InputObject $row -MemberType NoteProperty -Name "_File" -Value $Referencefile} + $i = $startRow + 1 ; foreach ($row in $Sheet2) {Add-Member -InputObject $row -MemberType NoteProperty -Name "_Row" -Value ($i ++) + Add-Member -InputObject $row -MemberType NoteProperty -Name "_Sheet" -Value $worksheet2 + Add-Member -InputObject $row -MemberType NoteProperty -Name "_File" -Value $Differencefile} + + if ($ExcludeDifferent -and -not $IncludeEqual) {$IncludeEqual = $true} + #Do the comparison and add file,sheet and row to the result - these are prefixed with "_" to show they are added the addition will fail if the sheet has these properties so split the operations + $diff = Compare-Object -ReferenceObject $Sheet1 -DifferenceObject $Sheet2 -Property $propList -PassThru -IncludeEqual:$IncludeEqual -ExcludeDifferent:$ExcludeDifferent | + Sort-Object -Property "_Row","File" + + #if BackgroundColor was specified, set it on extra or extra or changed rows + if ($BackgroundColor ) { + #Differences may only exist in one file. So gather the changes for each file; open the file, update each impacted row in the shee, save the file + $updates = $diff.where({$_.SideIndicator -ne "=="}) | Group-object -Property "_File" foreach ($file in $updates) { try {$xl = Open-ExcelPackage -Path $file.name } catch {Write-warning -Message "Can't open $($file.Name) for writing." ; return} + if ($AllDataBackgroundColor) { + $file.Group._sheet | Sort-Object -Unique | ForEach-Object { + $ws = $xl.Workbook.Worksheets[$_] + if ($headerName) {$range = "A" + $startrow + ":" + $ws.dimension.end.address} + else {$range = "A" + ($startrow + 1) + ":" + $ws.dimension.end.address} + Set-Format -WorkSheet $ws -BackgroundColor $AllDataBackgroundColor -Range $Range + } + } foreach ($row in $file.group) { - $ws = $xl.Workbook.Worksheets[$row._Sheet] + $ws = $xl.Workbook.Worksheets[$row._Sheet] $range = $ws.Dimension -replace "\d+",$row._row Set-Format -WorkSheet $ws -Range $range -BackgroundColor $BackgroundColor } @@ -145,16 +159,15 @@ $xl.save() ; $xl.Stream.Close() ; $xl.Dispose() } } - #if font colour was specified, set it on changed properties where the same key appears in both sheets. - if (($PropList -contains $Key) -and $FontColor) { - $updates = $diff | Group-object -Property $Key | where {$_.count -eq 2} + if ($FontColor -and ($propList -contains $Key) ) { + $updates = $diff.where({$_.SideIndicator -ne "=="}) | Group-object -Property $Key | where {$_.count -eq 2} if ($updates) { $XL1 = Open-ExcelPackage -path $Referencefile if ($oneFile ) {$xl2 = $xl1} else {$xl2 = Open-ExcelPackage -path $Differencefile } foreach ($u in $updates) { - foreach ($p in $proplist) { + foreach ($p in $propList) { 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 @@ -165,20 +178,32 @@ if (-not $oneFile) {$xl2.Save() ; $xl2.Stream.Close() ; $xl2.Dispose()} } } + elseif ($FontColor) {Write-Warning -Message "To match rows to set changed cells, you must specify -Key and it must match one of the included properties" } - if ($show) { - Start-Process -FilePath $Referencefile - if (-not $oneFile) { Start-Process -FilePath $Differencefile } + if ($show) { + Start-Process -FilePath $Referencefile + if (-not $oneFile) { Start-Process -FilePath $Differencefile } + } + elseif ($GridView) { + $Sheet2 | ForEach-Object -Begin {$Rowhash = @{} } -Process {$Rowhash[$_.$key] = $_._row } + if ($StartRow) { $rowCount1 = $StartRow} else { $rowCount1 = 1} + $rowCount2 = $null + $diff | Group-Object -Property $key | Sort-Object -Property @{e={($_.group | Measure-Object -Property _row -Maximum).maximum} } | ForEach-Object { + $hash = [ordered]@{"") {$rowCount1 = $hash["Row"] = $rowCount2 + $Hash[$key] = $keyVal + foreach ($p in $propList.Where({$_ -ne $key})) { + if ($row.SideIndicator -eq "==") {$hash[("=>$P")] = $hash[("<=$P")] =$row.$P} + else {$hash[($row.SideIndicator+$P)] =$row.$P} + } + } + [Pscustomobject]$hash } | Sort-Object -Property ")" } - elseif ($GridView) { - if ($StartRow) {$lastrow = $StartRow} else {$lastRow = 1} - $diff | Group-Object -Property $key | foreach { - $hash = [ordered]@{row = $lastRow; $key = $_.Name; } ; - foreach ($row IN $_.Group) { - if ($row._Side -eq "=>") {$lastRow = $hash.row = $row._Row } - foreach ($p in $proplist.Where({$_ -ne $key})) {$hash[($row._Side+$P)] =$row.$P} - } - [Pscustomobject]$hash } | Sort-Object -Property row| Update-FirstObjectProperties | Out-GridView -Title "Comparing $Referencefile::$worksheet1 (=>) with $Differencefile::$WorkSheet2 (<=)" - } - else {return $diff} + elseif (-not $PassThru) {return ($diff | Select-Object -Property (@(@{n="_Side";e={$_.SideIndicator}},"_File" ,"_Sheet","_Row") + $propList))} + if ( $PassThru) {return $diff } } \ No newline at end of file