mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Updates to help text (more proof reading still needed)
This commit is contained in:
@@ -39,22 +39,24 @@
|
||||
.PARAMETER IncludePivotTable
|
||||
Adds a Pivot table using the data in the worksheet.
|
||||
.PARAMETER PivotTableName
|
||||
If a Pivot table is created from command line parameters, specificies the name of the new sheet holding the pivot. If Omitted this will be "WorksheetName-PivotTable"
|
||||
If a Pivot table is created from command line parameters, specifies the name of the new sheet holding the pivot. If Omitted this will be "WorksheetName-PivotTable"
|
||||
.PARAMETER PivotRows
|
||||
Name(s) columns from the spreadhseet which will provide the Row name(s) in a pivot table created from command line parameters.
|
||||
Name(s) columns from the spreadsheet which will provide the Row name(s) in a pivot table created from command line parameters.
|
||||
.PARAMETER PivotColumns
|
||||
Name(s) columns from the spreadhseet which will provide the Column name(s) in a pivot table created from command line parameters.
|
||||
Name(s) columns from the spreadsheet which will provide the Column name(s) in a pivot table created from command line parameters.
|
||||
.PARAMETER PivotFilter
|
||||
Name(s) columns from the spreadhseet which will provide the Filter name(s) in a pivot table created from command line parameters.
|
||||
Name(s) columns from the spreadsheet which will provide the Filter name(s) in a pivot table created from command line parameters.
|
||||
.PARAMETER PivotData
|
||||
In a pivot table created from command line parameters, the fields to use in the table body are given as a Hash table in the form ColumnName = Average|Count|CountNums|Max|Min|Product|None|StdDev|StdDevP|Sum|Var|VarP .
|
||||
.PARAMETER PivotDataToColumn
|
||||
If there are multiple datasets in a PivotTable, by default they are shown seperatate rows under the given row heading; this switch makes them seperate columns.
|
||||
If there are multiple datasets in a PivotTable, by default they are shown as separate rows under the given row heading; this switch makes them separate columns.
|
||||
.PARAMETER NoTotalsInPivot
|
||||
In a pivot table created from command line parameters, prevents the addition of totals to rows and columns.
|
||||
.PARAMETER PivotTotals
|
||||
By default, Pivot tables have totals for each Row (on the right) and for each column at the bottom. This allows just one or neither to be selected.
|
||||
.PARAMETER PivotTableDefinition
|
||||
Instead of describing a single pivot table with mutliple commandline paramters; you can use a HashTable in the form PivotTableName = Definition;
|
||||
Definition is itself a hashtable with Sheet PivotTows, PivotColumns, PivotData, IncludePivotChart and ChartType values.
|
||||
Instead of describing a single pivot table with multiple commandline parameters; you can use a HashTable in the form PivotTableName = Definition;
|
||||
Definition is itself a hashtable with Sheet, PivotRows, PivotColumns, PivotData, IncludePivotChart and ChartType values.
|
||||
.PARAMETER IncludePivotChart
|
||||
Include a chart with the Pivot table - implies -IncludePivotTable.
|
||||
.PARAMETER ChartType
|
||||
@@ -92,20 +94,20 @@
|
||||
.PARAMETER ExcelChartDefinition
|
||||
A hash table containing ChartType, Title, NoLegend, ShowCategory, ShowPercent, Yrange, Xrange and SeriesHeader for one or more [non-pivot] charts.
|
||||
.PARAMETER HideSheet
|
||||
Name(s) of Sheet(s) to hide in the workbook, supports wildcards. If all sheets would be hidden, the sheet being worked on will be revealed .
|
||||
Name(s) of Sheet(s) to hide in the workbook, supports wildcards. If all sheets would be hidden, the sheet being worked on will be revealed.
|
||||
.PARAMETER UnHideSheet
|
||||
Name(s) of Sheet(s) to Reveal in the workbook, supports wildcards.
|
||||
.PARAMETER MoveToStart
|
||||
If specified, the worksheet will be moved to the start of the workbook.
|
||||
MoveToStart takes precedence over MoveToEnd, Movebefore and MoveAfter if more than one is specified.
|
||||
-MoveToStart takes precedence over -MoveToEnd, -Movebefore and -MoveAfter if more than one is specified.
|
||||
.PARAMETER MoveToEnd
|
||||
If specified, the worksheet will be moved to the end of the workbook.
|
||||
(This is the default position for newly created sheets, but this can be used to move existing sheets.)
|
||||
.PARAMETER MoveBefore
|
||||
If specified, the worksheet will be moved before the nominated one (which can be a postion starting from 1, or a name).
|
||||
MoveBefore takes precedence over MoveAfter if both are specified.
|
||||
If specified, the worksheet will be moved before the nominated one (which can be a position starting from 1, or a name).
|
||||
-MoveBefore takes precedence over -MoveAfter if both are specified.
|
||||
.PARAMETER MoveAfter
|
||||
If specified, the worksheet will be moved after the nominated one (which can be a postion starting from 1, or a name or *).
|
||||
If specified, the worksheet will be moved after the nominated one (which can be a position starting from 1, or a name or *).
|
||||
If * is used, the worksheet names will be examined starting with the first one, and the sheet placed after the last sheet which comes before it alphabetically.
|
||||
.PARAMETER KillExcel
|
||||
Closes Excel - prevents errors writing to the file because Excel has it open.
|
||||
@@ -124,7 +126,7 @@
|
||||
.PARAMETER FreezePane
|
||||
Freezes panes at specified coordinates (in the form RowNumber , ColumnNumber).
|
||||
.PARAMETER AutoFilter
|
||||
Enables the 'Filter' in Excel on the complete header row. So users can easily sort, filter and/or search the data in the selected column from within Excel.
|
||||
Enables the 'Filter' in Excel on the complete header row, so users can easily sort, filter and/or search the data in the selected column from within Excel.
|
||||
.PARAMETER AutoSize
|
||||
Sizes the width of the Excel column to the maximum width needed to display all the containing data in that cell.
|
||||
.PARAMETER Activate
|
||||
@@ -147,10 +149,10 @@
|
||||
# number with 2 decimal places.
|
||||
'0.00'
|
||||
|
||||
# number with 2 decimal places and thousand separator.
|
||||
# number with 2 decimal places and thousand-separator.
|
||||
'#,##0.00'
|
||||
|
||||
# number with 2 decimal places and thousand separator and money symbol.
|
||||
# number with 2 decimal places and thousand-separator and money-symbol.
|
||||
'€#,##0.00'
|
||||
|
||||
# percentage (1 = 100%, 0.01 = 1%)
|
||||
@@ -255,7 +257,9 @@
|
||||
New-ConditionalText -ConditionalType GreaterThan 525 -ConditionalTextColor DarkRed -BackgroundColor LightPink
|
||||
)
|
||||
|
||||
Exports data that will have a 'Conditional formatting rule' in Excel on these cells that will show the background fill color in 'LightPink' and the text color in 'DarkRed' when the value is greater then '525'. In case this condition is not met the color will be the default, black text on a white background.
|
||||
Exports data that will have a 'Conditional formatting rule' in Excel on these cells that will show the background fill color in
|
||||
'LightPink' and the text color in 'DarkRed' when the value is greater than '525'. In case this condition is not met the color will
|
||||
be the default, black text on a white background.
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
@@ -352,13 +356,12 @@
|
||||
$excel.Dispose()
|
||||
Start-Process .\test.xlsx
|
||||
|
||||
This example uses -passthrough - put service information into sheet1 of the work book and saves the excelPackageObject in $Excel.
|
||||
This example uses -passthrough - put service information into sheet1 of the workbook and saves the ExcelPackageObject in $Excel.
|
||||
It then uses the package object to apply formatting, and then saves the workbook and disposes of the object before loading the document in Excel.
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
PS> Remove-Item -Path .\test.xlsx -ErrorAction Ignore
|
||||
|
||||
$excel = Get-Process | Select-Object -Property Name,Company,Handles,CPU,PM,NPM,WS | Export-Excel -Path .\test.xlsx -ClearSheet -WorksheetName "Processes" -PassThru
|
||||
$sheet = $excel.Workbook.Worksheets["Processes"]
|
||||
$sheet.Column(1) | Set-ExcelRange -Bold -AutoFit
|
||||
@@ -1090,10 +1093,10 @@ function Add-WorkSheet {
|
||||
#If specified, the worksheet will be moved to the end of the workbook.
|
||||
#(This is the default position for newly created sheets, but this can be used to move existing sheets.)
|
||||
[Switch]$MoveToEnd,
|
||||
#If specified, the worksheet will be moved before the nominated one (which can be a postion starting from 1, or a name).
|
||||
#If specified, the worksheet will be moved before the nominated one (which can be a position starting from 1, or a name).
|
||||
#MoveBefore takes precedence over MoveAfter if both are specified.
|
||||
$MoveBefore ,
|
||||
# If specified, the worksheet will be moved after the nominated one (which can be a postion starting from 1, or a name or *).
|
||||
# If specified, the worksheet will be moved after the nominated one (which can be a position starting from 1, or a name or *).
|
||||
# If * is used, the worksheet names will be examined starting with the first one, and the sheet placed after the last sheet which comes before it alphabetically.
|
||||
$MoveAfter ,
|
||||
#If there is already content in the workbook the new sheet will not be active UNLESS Activate is specified
|
||||
|
||||
@@ -8,32 +8,30 @@
|
||||
In the former case the header row is copied from the first sheet and, by default, each row of data is labelled with the name of the sheet it came from.
|
||||
In the latter case -NoHeader is specified, and each copied block can have the sheet it came from placed above it as a title.
|
||||
.EXAMPLE
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
PS> foreach ($computerName in @('Server1', 'Server2', 'Server3', 'Server4')) {
|
||||
Get-Service -ComputerName $computerName | Select-Object -Property Status, Name, DisplayName, StartType |
|
||||
Export-Excel -Path .\test.xlsx -WorkSheetname $computerName -AutoSize
|
||||
}
|
||||
$ptDef =New-PivotTableDefinition -PivotTableName "Pivot1" -SourceWorkSheet "Combined" -PivotRows "Status" -PivotFilter "MachineName" -PivotData @{Status='Count'} -IncludePivotChart -ChartType BarClustered3D
|
||||
Join-Worksheet -Path .\test.xlsx -WorkSheetName combined -FromLabel "MachineName" -HideSource -AutoSize -FreezeTopRow -BoldTopRow -PivotTableDefinition $pt -Show
|
||||
PS> $ptDef =New-PivotTableDefinition -PivotTableName "Pivot1" -SourceWorkSheet "Combined" -PivotRows "Status" -PivotFilter "MachineName" -PivotData @{Status='Count'} -IncludePivotChart -ChartType BarClustered3D
|
||||
PS> Join-Worksheet -Path .\test.xlsx -WorkSheetName combined -FromLabel "MachineName" -HideSource -AutoSize -FreezeTopRow -BoldTopRow -PivotTableDefinition $pt -Show
|
||||
|
||||
The foreach command gets the services running on four servers and exports each to its own page in Test.xlsx.
|
||||
$PtDef= creates a defintion for a single Pivot table.
|
||||
$PtDef= creates a defintion for a PivotTable.
|
||||
The Join-Worksheet command uses the same file and merges the results onto a sheet named "Combined". It sets a column header of "Machinename",
|
||||
this column will contain the name of the sheet the data was copied from; after copying the data to the sheet "combined", the other sheets will be hidden.
|
||||
Join-Worksheet finishes by calling export-Excel to AutoSize cells, freeze the top row and make it bold and add the Pivot table.
|
||||
Join-Worksheet finishes by calling Export-Excel to AutoSize cells, freeze the top row and make it bold and add the Pivot table.
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
PS> Get-WmiObject -Class win32_logicaldisk | select -Property DeviceId,VolumeName, Size,Freespace |
|
||||
Export-Excel -Path "$env:computerName.xlsx" -WorkSheetname Volumes -NumberFormat "0,000"
|
||||
Get-NetAdapter | Select-Object Name,InterfaceDescription,MacAddress,LinkSpeed |
|
||||
PS> Get-NetAdapter | Select-Object Name,InterfaceDescription,MacAddress,LinkSpeed |
|
||||
Export-Excel -Path "$env:COMPUTERNAME.xlsx" -WorkSheetname NetAdapter
|
||||
Join-Worksheet -Path "$env:COMPUTERNAME.xlsx" -WorkSheetName Summary -Title "Summary" -TitleBold -TitleSize 22 -NoHeader -LabelBlocks -AutoSize -HideSource -show
|
||||
PS> Join-Worksheet -Path "$env:COMPUTERNAME.xlsx" -WorkSheetName Summary -Title "Summary" -TitleBold -TitleSize 22 -NoHeader -LabelBlocks -AutoSize -HideSource -show
|
||||
|
||||
The first two commands get logical disk and network card information; each type is exported to its own sheet in a workbook.
|
||||
The Join-worksheet command copies both onto a page named "Summary". Because the data is disimilar -NoHeader is specified, ensuring the whole of each page is copied.
|
||||
The Join-Worksheet command copies both onto a page named "Summary". Because the data is disimilar -NoHeader is specified, ensuring the whole of each page is copied.
|
||||
Specifying -LabelBlocks causes each sheet's name to become a title on the summary page above the copied data.
|
||||
The source data is hidden, a title is added in 22 point boldface and the columns are sized to fit the data.
|
||||
#>
|
||||
|
||||
@@ -3,21 +3,26 @@
|
||||
.SYNOPSIS
|
||||
Inserts a DataTable - returned by SQL query into an ExcelSheet, more efficiently than sending it via Export-Excel
|
||||
.DESCRIPTION
|
||||
This command can accept a data table object or take a SQL command and run it against a database connection.
|
||||
If running the SQL command, it accepts an object representing a session with a SQL server or ODBC database, or a connection String to make a session.
|
||||
It the DataTable is inserted into the Excel sheet
|
||||
It takes most of the parameters of Export-Excel, but it is more efficient than getting dataRows and piping them into Export-Excel,
|
||||
data-rows have additional properties which need to be stripped off.
|
||||
This command can accept a data table object or take a SQL statement and run it against a database connection.
|
||||
If running a SQL statement, the accepts either
|
||||
* an object representing a session with a SQL server or ODBC database, or
|
||||
* a connection String to make a session.
|
||||
The command takes most of the parameters of Export-Excel, and after inserting the table into the worksheet it
|
||||
calls Export-Excel to carry out other tasks on the sheet. It is more efficient to do this than to get data-rows
|
||||
and pipe them into Export-Excel, stripped off the database 'housekeeping' properties.
|
||||
.PARAMETER DataTable
|
||||
A System.Data.DataTable object containing the data to be inserted into the spreadsheet without running a query.
|
||||
.PARAMETER Session
|
||||
An active ODBC Connection or SQL connection object representing a session with a database which will be queried to get the data .
|
||||
.PARAMETER Connection
|
||||
Database connection string; either DSN=ODBC_Data_Source_Name, a full odbc or SQL Connection string, or the name of a SQL server. This is used to create a database session.
|
||||
A database connection string to be used to create a database session; either
|
||||
* A Data source name written in the form DSN=ODBC_Data_Source_Name, or
|
||||
* A full odbc or SQL Connection string, or
|
||||
* The name of a SQL server.
|
||||
.PARAMETER MSSQLServer
|
||||
Specifies the connection string is for SQL server, not ODBC .
|
||||
Specifies the connection string is for SQL server, not ODBC.
|
||||
.PARAMETER SQL
|
||||
The SQL query to run against the session which was passed in -Session or set up from $Connection.
|
||||
The SQL query to run against the session which was passed in -Session or set up from -Connection.
|
||||
.PARAMETER Database
|
||||
Switches to a specific database on a SQL server.
|
||||
.PARAMETER QueryTimeout
|
||||
@@ -25,9 +30,9 @@
|
||||
.PARAMETER Path
|
||||
Path to a new or existing .XLSX file.
|
||||
.PARAMETER WorkSheetName
|
||||
The name of a sheet within the workbook - "Sheet1" by default .
|
||||
The name of a sheet within the workbook - "Sheet1" by default.
|
||||
.PARAMETER KillExcel
|
||||
Closes Excel - prevents errors writing to the file because Excel has it open
|
||||
Closes Excel - prevents errors writing to the file because Excel has it open.
|
||||
.PARAMETER Title
|
||||
Text of a title to be placed in the top left cell.
|
||||
.PARAMETER TitleBold
|
||||
@@ -123,7 +128,8 @@
|
||||
|
||||
.EXAMPLE
|
||||
C:\> Send-SQLDataToExcel -MsSQLserver -Connection localhost -SQL "select name,type,type_desc from [master].[sys].[all_objects]" -Path .\temp.xlsx -WorkSheetname master -AutoSize -FreezeTopRow -AutoFilter -BoldTopRow
|
||||
Connects to the local SQL server and selects 3 columns from [Sys].[all_objects] and exports then to a sheet named master with some basic header manager
|
||||
|
||||
Connects to the local SQL server and selects 3 columns from [Sys].[all_objects] and exports then to a sheet named master with some basic header management
|
||||
.EXAMPLE
|
||||
C:\> $SQL="SELECT top 25 DriverName, Count(RaceDate) as Races, Count(Win) as Wins, Count(Pole) as Poles, Count(FastestLap) as Fastlaps FROM Results GROUP BY DriverName ORDER BY (count(win)) DESC"
|
||||
C:\> $Connection = 'Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DriverId=790;ReadOnly=0;Dbq=C:\users\James\Documents\f1Results.xlsx;'
|
||||
@@ -136,7 +142,7 @@
|
||||
C:\> Get-SQL -Session F1 -excel -Connection "C:\Users\mcp\OneDrive\public\f1\f1Results.xlsx" -sql $sql -OutputVariable Table | out-null
|
||||
C:\> Send-SQLDataToExcel -DataTable $Table -Path ".\demo3.xlsx" -WorkSheetname Gpwinners -autosize -TableName winners -TableStyle Light6 -show
|
||||
|
||||
This uses Get-SQL (at least V1.1 download from the gallery with Install-Module -Name GetSQL - note the function is get-SQL the module is GetSQL without the "-" )
|
||||
This uses Get-SQL (at least V1.1 - download from the gallery with Install-Module -Name GetSQL - note the function is Get-SQL the module is GetSQL without the "-" )
|
||||
to simplify making database connections and building /submitting SQL statements.
|
||||
Here it uses the same SQL statement as before; -OutputVariable leaves a System.Data.DataTable object in $table
|
||||
and Send-SQLDataToExcel puts $table into the worksheet and sets it as an Excel table.
|
||||
@@ -145,7 +151,7 @@
|
||||
C:\> $SQL = "SELECT top 25 DriverName, Count(Win) as Wins FROM Results GROUP BY DriverName ORDER BY (count(win)) DESC"
|
||||
C:\> Send-SQLDataToExcel -Session $DbSessions["f1"] -SQL $sql -Path ".\demo3.xlsx" -WorkSheetname Gpwinners -autosize -ColumnChart
|
||||
|
||||
Like the previous example, this uses Get-SQL (download from the gallery with Install-Module -Name GetSQL).It uses the connection which Get-SQL made rather than an ODFBC connection string
|
||||
Like the previous example, this uses Get-SQL (download from the gallery with Install-Module -Name GetSQL). It uses the connection which Get-SQL made rather than an ODFBC connection string
|
||||
Here the data is presented as a quick chart.
|
||||
.EXAMPLE
|
||||
C:\> Send-SQLDataToExcel -path .\demo3.xlsx -WorkSheetname "LR" -Connection "DSN=LR" -sql "SELECT name AS CollectionName FROM AgLibraryCollection Collection ORDER BY CollectionName"
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
Function Set-ExcelColumn {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Adds a column to the existing data area in an Excel sheet, fills values and sets formatting
|
||||
Adds or modifies a column in an Excel sheet, filling values, settings formatting and/or creating named ranges.
|
||||
.DESCRIPTION
|
||||
Set-ExcelColumn takes a value which is either a string containing a value or formula or a scriptblock
|
||||
Set-ExcelColumn can take a value which is either a string containing a value or formula or a scriptblock
|
||||
which evaluates to a string, and optionally a column number and fills that value down the column.
|
||||
A column heading can be specified and the new column can be made a named range.
|
||||
A column heading can be specified and the column can be made a named range.
|
||||
The column can be formatted in the same operation.
|
||||
.EXAMPLE
|
||||
Set-ExcelColumn -Worksheet $ws -Column 5 -NumberFormat 'Currency'
|
||||
@@ -16,15 +16,20 @@
|
||||
.EXAMPLE
|
||||
Set-ExcelColumn -Worksheet $ws -Heading "WinsToFastLaps" -Value {"=E$row/C$row"} -Column 7 -AutoSize -AutoNameRange
|
||||
|
||||
Here $WS already contains a worksheet which contains counts of races won and fastest laps recorded by racing drivers (in columns C and E)
|
||||
Set-ExcelColumn specifies that Column 7 should have a heading of "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3 etc
|
||||
the data cells should become a named range, which will also be "WinsToFastLaps" the column width will be set automatically
|
||||
Here, $WS already contains a worksheet which contains counts of races won and fastest laps recorded by racing drivers (in columns C and E)
|
||||
Set-ExcelColumn specifies that Column 7 should have a heading of "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3 etc
|
||||
the new data cells should become a named range, which will also be named "WinsToFastLaps" the column width will be set automatically
|
||||
.EXAMPLE
|
||||
Set-ExcelColumn -Worksheet $ws -Heading "Link" -Value {"https://en.wikipedia.org" + $worksheet.cells["B$Row"].value } -AutoSize
|
||||
|
||||
In this example, the worksheet in $ws has partial links to wikipedia pages in column B.
|
||||
The Value parameter is is a script block and it outputs a string which begins https... and ends with the value of cell at column B in the current row.
|
||||
When given a valid URI, Set-ExcelColumn makes it a hyperlink. The column will be autosized to fit the links.
|
||||
The -Value parameter is is a script block and it outputs a string which begins https... and ends with the value of cell at column
|
||||
B in the current row. When given a valid URI, Set-ExcelColumn makes it a hyperlink. The column will be autosized to fit the links.
|
||||
.EXAMPLE
|
||||
4..6 | Set-ExcelColumn -Worksheet $ws -AutoNameRange
|
||||
|
||||
Again $ws contains a worksheet. Here columns 4 to 6 are made into named ranges, row 1 is used for the range name
|
||||
and the rest of the column becomes the range.
|
||||
#>
|
||||
[cmdletbinding()]
|
||||
[Alias("Set-Column")]
|
||||
|
||||
10
Set-Row.ps1
10
Set-Row.ps1
@@ -3,18 +3,18 @@
|
||||
.Synopsis
|
||||
Fills values into a [new] row in an Excel spreadsheet. And sets row formmats.
|
||||
.Description
|
||||
Set-ExcelRow accepts either a Worksheet object or an Excel package object returned by Export-Excel and the name of a sheet,
|
||||
Set-ExcelRow accepts either a Worksheet object or an Excel package object returned by Export-Excel and the name of a sheet,
|
||||
and inserts the chosen contents into a row of the sheet.
|
||||
The contents can be a constant "42" , a formula or a script block which is converted into a constant or formula.
|
||||
The contents can be a constant e.g. "42" , a formula or a script block which is converted into a constant or formula.
|
||||
The first cell of the row can optionally be given a heading.
|
||||
.Example
|
||||
Set-ExcelRow -Worksheet $ws -Heading Total -Value {"=sum($columnName`2:$columnName$endrow)" }
|
||||
|
||||
$Ws contains a worksheet object, and no Row number is specified so Set-ExcelRow will select the next row after the end of the data in the sheet
|
||||
The first cell will contain "Total", and each other cell will contain
|
||||
$Ws contains a worksheet object, and no Row number is specified so Set-ExcelRow will select the next row after the end
|
||||
of the data in the sheet. The first cell in the row will contain "Total", and each other cell will contain
|
||||
=Sum(xx2:xx99) - where xx is the column name, and 99 is the last row of data.
|
||||
Note the use of `2 to Prevent 2 becoming part of the variable "ColumnName"
|
||||
The script block can use $row, $column, $ColumnName, $startRow/Column $endRow/Column
|
||||
The script block can use $Worksheet, $Row, $Column (number), $ColumnName (letter), $StartRow/Column and $EndRow/Column
|
||||
.Example
|
||||
Set-ExcelRow -Worksheet $ws -Heading Total -HeadingBold -Value {"=sum($columnName`2:$columnName$endrow)" } -NumberFormat 'Currency' -StartColumn 2 -Bold -BorderTop Double -BorderBottom Thin
|
||||
|
||||
|
||||
@@ -1,91 +1,99 @@
|
||||
Function Compare-WorkSheet {
|
||||
<#
|
||||
.Synopsis
|
||||
Compares two worksheets with the same name in different files.
|
||||
Compares two worksheets and shows the differences.
|
||||
.Description
|
||||
This command takes two file names, a worksheet name and a name for a key column.
|
||||
This command takes two file names, one or two worksheet name and a name for a key column.
|
||||
It reads the worksheet from each file and decides the column names.
|
||||
It builds as hashtable of the key column values and the rows they appear in
|
||||
It then uses PowerShell's compare object command to compare the sheets (explicity checking all column names which have not been excluded)
|
||||
For the difference rows it adds the row number for the key of that row - we have to add the key after doing the comparison,
|
||||
otherwise rows will be considered as different simply because they have different row numbers
|
||||
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.
|
||||
It builds a hashtable of the key-column values and the rows in which they appear.
|
||||
It then uses PowerShell's compare object command to compare the sheets (explicity checking
|
||||
all the column names which have not been excluded). For the difference rows it adds the
|
||||
row number for the key of that row - we have to add the key after doing the comparison,
|
||||
otherwise identical rows at diffeent positions in the file will not will be considered to match.
|
||||
We also add the name of the file and sheet in which the difference occurs.
|
||||
If -BackgroundColor is specified the difference rows will be changed to that background in the orginal file.
|
||||
.Example
|
||||
Compare-WorkSheet -Referencefile 'Server56.xlsx' -Differencefile 'Server57.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 command compares the "products" pages in the two workbooks, but we don't want to register a differnce if if the software was installed on a
|
||||
different date or from a different place, so Excluding Install* removes InstallDate and InstallSource.
|
||||
This data doesn't have a "name" column" so we specify the "IdentifyingNumber" column as the key.
|
||||
Compare-WorkSheet -Referencefile 'Server56.xlsx' -Differencefile 'Server57.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 command compares the "Products" pages in the two workbooks, but we don't want to register a difference if the software was installed on a
|
||||
different date or from a different place, and excluding Install* removes InstallDate and InstallSource.
|
||||
This data doesn't have a "Name" column" so we specify the "IdentifyingNumber" column as the key.
|
||||
The results will be presented as a table.
|
||||
.Example
|
||||
compare-WorkSheet "Server54.xlsx" "Server55.xlsx" -WorkSheetName services -GridView
|
||||
This time two workbooks contain the result of redirecting Get-WmiObject -Class win32_service to Export-Excel
|
||||
Here the -Differencefile and -Referencefile parameter switches are assumed , and the default setting for -key ("Name") works for services
|
||||
This will display the differences between the "services" sheets using a grid view
|
||||
compare-WorkSheet "Server54.xlsx" "Server55.xlsx" -WorkSheetName Services -GridView
|
||||
|
||||
This time two workbooks contain the result of redirecting Get-WmiObject -Class win32_service to Export-Excel.
|
||||
Here the -Differencefile and -Referencefile parameter switches are assumed , and the default setting for -key ("Name") works for services
|
||||
This will display the differences between the "Services" sheets using a grid view
|
||||
.Example
|
||||
Compare-WorkSheet 'Server54.xlsx' 'Server55.xlsx' -WorkSheetName Services -BackgroundColor lightGreen
|
||||
This version of the command outputs the differences between the "services" pages and also highlights any different rows in the spreadsheet files.
|
||||
|
||||
This version of the command outputs the differences between the "services" pages and highlights any different rows in the spreadsheet files.
|
||||
.Example
|
||||
Compare-WorkSheet 'Server54.xlsx' 'Server55.xlsx' -WorkSheetName Services -BackgroundColor lightGreen -FontColor Red -Show
|
||||
This builds on the previous example: this time Where two changed rows have the value in the "name" column (the default value for -key),
|
||||
|
||||
This example builds on the previous one: this time where two changed rows have the value in the "Name" column (the default value for -Key),
|
||||
this version adds highlighting of the changed cells in red; and then opens the Excel file.
|
||||
.Example
|
||||
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 the command specifies a limited set of columns should be used.
|
||||
machine name and time the test was run the command specifies that a limited set of columns should be used.
|
||||
.Example
|
||||
Compare-WorkSheet 'Server54.xlsx' 'Server55.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,
|
||||
|
||||
The "General" page in the two workbooks has a title and two unlabelled columns with a row each for CPU, Memory, Domain, Disk and so on;
|
||||
so the command is instructed to start at row 2 in order to skip the title and given names for 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
|
||||
|
||||
This version of the previous command highlights 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
|
||||
#First file to compare.
|
||||
[parameter(Mandatory=$true,Position=0)]
|
||||
$Referencefile ,
|
||||
#Second file to compare
|
||||
#Second file to compare.
|
||||
[parameter(Mandatory=$true,Position=1)]
|
||||
$Differencefile ,
|
||||
#Name(s) of worksheets to compare.
|
||||
$WorkSheetName = "Sheet1",
|
||||
#Properties to include in the DIFF - supports wildcards, default is "*"
|
||||
#Properties to include in the DIFF - supports wildcards, default is "*".
|
||||
$Property = "*" ,
|
||||
#Properties to exclude from the the search - supports wildcards
|
||||
#Properties to exclude from the search - supports wildcards.
|
||||
$ExcludeProperty ,
|
||||
#Specifies custom property names to use, instead of the values defined in the column headers of the TopRow.
|
||||
#Specifies custom property names to use, instead of the values defined in the starting row of the sheet.
|
||||
[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
|
||||
#Automatically generate property names (P1, P2, P3 ...) instead of the using the values the starting 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.
|
||||
#The row from where we start to import data: all rows above the start row are disregarded. By default, this is the first row.
|
||||
[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
|
||||
#If specified, highlights the DIFF rows.
|
||||
[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).
|
||||
[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"
|
||||
#Name of a column which is unique and will be used to add a row to the DIFF object, defaults to "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 (unless -passthru is also specified)
|
||||
#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
|
||||
#If specified, the command tries to the show the DIFF in a Grid-View 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
|
||||
#If specified -Passthrough a 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
|
||||
#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
|
||||
#If Specified the result includes only the rows where both are equal.
|
||||
[Switch]$ExcludeDifferent
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user