mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Added paramters to Compare worksheet
Now has -Gridview, and supports startrow, headernames and NoHeader (as per import Excel) and ensures the headers don't clash.
This commit is contained in:
@@ -12,23 +12,28 @@
|
|||||||
We also add the name of the file in which the difference occurs.
|
We also add the name of the file in which the difference occurs.
|
||||||
If -BackgroundColor is specified the difference rows will be changed to that background.
|
If -BackgroundColor is specified the difference rows will be changed to that background.
|
||||||
.Example
|
.Example
|
||||||
compare-WorkSheet -Referencefile 'Server1.xlsx' -Differencefile 'Server2.xlsx' -WorkSheetName Products -key IdentifyingNumber -ExcludeProperty Install* | format-table
|
Compare-WorkSheet -Referencefile 'Server1.xlsx' -Differencefile 'Server2.xlsx' -WorkSheetName Products -key IdentifyingNumber -ExcludeProperty Install* | format-table
|
||||||
The two workbooks in this example contain the result of redirecting a subset of properties from Get-WmiObject -Class win32_product to Export-Excel
|
The two workbooks in this example contain the result of redirecting a subset of properties from Get-WmiObject -Class win32_product to Export-Excel
|
||||||
The command compares the "products" pages in the two workbooks, but we don't want a match if the software was installed on a
|
The command compares the "products" pages in the two workbooks, but we don't want a match if the software was installed on a
|
||||||
different date or from a different place, so Excluding Install* removes InstallDate and InstallSource. The results will be presented as a table.
|
different date or from a different place, so Excluding Install* removes InstallDate and InstallSource. The results will be presented as a table.
|
||||||
.Example
|
.Example
|
||||||
compare-WorkSheet 'Server1.xlsx' 'Server2.xlsx' -WorkSheetName Services -key Name -BackgroundColor lightGreen
|
Compare-WorkSheet 'Server1.xlsx' 'Server2.xlsx' -WorkSheetName Services -key Name -BackgroundColor lightGreen
|
||||||
This time two workbooks contain the result of redirecting Get-WmiObject -Class win32_service to Export-Excel
|
This time two workbooks contain the result of redirecting Get-WmiObject -Class win32_service to Export-Excel
|
||||||
This command compares the "services" pages and highlights the rows in the spreadsheet files.
|
This command compares the "services" pages and highlights the rows in the spreadsheet files.
|
||||||
Here the -Differencefile and -Referencefile parameter switches are assumed
|
Here the -Differencefile and -Referencefile parameter switches are assumed
|
||||||
.Example
|
.Example
|
||||||
compare-WorkSheet 'Server1.xlsx' 'Server2.xlsx' -WorkSheetName Services -BackgroundColor lightGreen -fontColor Red -Show
|
Compare-WorkSheet 'Server1.xlsx' 'Server2.xlsx' -WorkSheetName Services -BackgroundColor lightGreen -fontColor Red -Show
|
||||||
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 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
|
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
|
.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" -FontColor Red -TabColor Yellow -Show
|
||||||
This time the reference file and the difference file are the same file and two different sheets are used. Because the tests include the
|
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.
|
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.
|
||||||
#>
|
#>
|
||||||
[cmdletbinding()]
|
[cmdletbinding()]
|
||||||
Param(
|
Param(
|
||||||
@@ -46,6 +51,14 @@
|
|||||||
$Property = "*" ,
|
$Property = "*" ,
|
||||||
#Properties to exclude from the the search - supports wildcards
|
#Properties to exclude from the the search - supports wildcards
|
||||||
$ExcludeProperty ,
|
$ExcludeProperty ,
|
||||||
|
#Specifies custom property names to use, instead of the values defined in the column headers of the TopRow.
|
||||||
|
[Parameter(ParameterSetName='B', Mandatory)]
|
||||||
|
[String[]]$Headername,
|
||||||
|
#Automatically generate property names (P1, P2, P3, ..) instead of the using the values the top row of the sheet
|
||||||
|
[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,
|
||||||
#If specified, highlights the DIFF rows
|
#If specified, highlights the DIFF rows
|
||||||
[System.Drawing.Color]$BackgroundColor,
|
[System.Drawing.Color]$BackgroundColor,
|
||||||
#If specified identifies the tabs which contain DIFF rows (ignored if -backgroundColor is omitted)
|
#If specified identifies the tabs which contain DIFF rows (ignored if -backgroundColor is omitted)
|
||||||
@@ -53,11 +66,15 @@
|
|||||||
#If specified, highlights the DIFF columns in rows which have the same key.
|
#If specified, highlights the DIFF columns in rows which have the same key.
|
||||||
[System.Drawing.Color]$FontColor,
|
[System.Drawing.Color]$FontColor,
|
||||||
#If specified opens the Excel workbooks instead of outputting the diff to the console
|
#If specified opens the Excel workbooks instead of outputting the diff to the console
|
||||||
[Switch]$Show
|
[Switch]$Show,
|
||||||
|
#If specified, tries to the show the DIFF in a gridview. (Works best with few columns)
|
||||||
|
[switch]$GridView
|
||||||
)
|
)
|
||||||
|
|
||||||
$oneFile = ((Resolve-Path -Path $Referencefile).path -eq (Resolve-Path -Path $Differencefile).path)
|
$oneFile = ((Resolve-Path -Path $Referencefile).path -eq (Resolve-Path -Path $Differencefile).path)
|
||||||
|
|
||||||
|
if ($Key -eq "Name" -and $NoHeader) {$key = "p1"}
|
||||||
|
|
||||||
#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 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] ) ) {
|
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"
|
Write-Warning -Message "If both the Reference and difference file are the same then worksheet name must provide 2 different names"
|
||||||
@@ -68,9 +85,11 @@
|
|||||||
else {Write-Warning -Message "You must provide either a single worksheet name or two names." ; return }
|
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
|
#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 -ErrorAction stop
|
$Sheet1 = Import-Excel -Path $Referencefile -WorksheetName $WorkSheet1 @params
|
||||||
$Sheet2 = Import-Excel -Path $Differencefile -WorksheetName $WorkSheet2 -ErrorAction stop
|
$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 }
|
||||||
|
|
||||||
@@ -104,7 +123,7 @@
|
|||||||
@{n="_File"; e={if ($_.SideIndicator -eq '=>') {$Differencefile} else {$Referencefile } }} ,
|
@{n="_File"; e={if ($_.SideIndicator -eq '=>') {$Differencefile} else {$Referencefile } }} ,
|
||||||
@{n="_Sheet"; e={if ($_.SideIndicator -eq '=>') {$worksheet2 } else {$worksheet1 } }} ,
|
@{n="_Sheet"; e={if ($_.SideIndicator -eq '=>') {$worksheet2 } else {$worksheet1 } }} ,
|
||||||
@{n='_Row'; e={if ($_.$key -and $_.SideIndicator -eq '=>') {$rows2[$_.$key]} elseif ($_.$key) {$rows1[$_.$key]} else { "" } }}
|
@{n='_Row'; e={if ($_.$key -and $_.SideIndicator -eq '=>') {$rows2[$_.$key]} elseif ($_.$key) {$rows1[$_.$key]} else { "" } }}
|
||||||
) + $PropList) #| Sort-Object -Property row,file
|
) + $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 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) {
|
if (($PropList -contains $Key) -and $BackgroundColor) {
|
||||||
@@ -147,9 +166,19 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($show) {
|
if ($show) {
|
||||||
Start-Process -FilePath $Referencefile
|
Start-Process -FilePath $Referencefile
|
||||||
if (-not $oneFile) { Start-Process -FilePath $Differencefile }
|
if (-not $oneFile) { Start-Process -FilePath $Differencefile }
|
||||||
}
|
}
|
||||||
else {return $diff}
|
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}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user