mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Updates after proof reading help
This commit is contained in:
@@ -5,11 +5,12 @@
|
||||
.Description
|
||||
Conditional formatting allows Excel to:
|
||||
* Mark cells with icons depending on their value
|
||||
* Show a databar whose length indicates the value or a 2 or 3 color scale where the color indicates the relative value
|
||||
* Show a databar whose length indicates the value or a two or three color scale where the color indicates the relative value
|
||||
* Change the color, font, or number format of cells which meet given criteria
|
||||
Add-ConditionalFormatting allows these parameters to be set; for fine tuning of the rules the -PassThru switch,
|
||||
will return the rule so that you can modify things which are specific to that type of rule,
|
||||
for example the values which correspond to each icon in an Icon-Set.
|
||||
Add-ConditionalFormatting allows these parameters to be set; for fine tuning of
|
||||
the rules, the -PassThru switch will return the rule so that you can modify
|
||||
things which are specific to that type of rule, example, the values which
|
||||
correspond to each icon in an Icon-Set.
|
||||
.Example
|
||||
>
|
||||
$excel = $avdata | Export-Excel -Path (Join-path $FilePath "\Machines.XLSX" ) -WorksheetName "Server Anti-Virus" -AutoSize -FreezeTopRow -AutoFilter -PassThru
|
||||
@@ -19,31 +20,37 @@
|
||||
$excel.Workbook.Worksheets[1].Row(1).style.font.bold = $true
|
||||
$excel.Save() ; $excel.Dispose()
|
||||
|
||||
Here Export-Excel is called with the -PassThru parameter so the Excel Package object representing Machines.XLSX is stored in $Excel.
|
||||
The desired worksheet is selected and then columns" B" and "I" are conditionally formatted (excluding the top row) to show red text if
|
||||
they contain "2003" or "Disabled" respectively. A fixed date format is then applied to columns D..G, and the top row is formatted.
|
||||
Here Export-Excel is called with the -PassThru parameter so the ExcelPackage object
|
||||
representing Machines.XLSX is stored in $Excel.The desired worksheet is selected
|
||||
and then columns" B" and "I" are conditionally formatted (excluding the top row)
|
||||
to show red text if they contain "2003" or "Disabled" respectively.
|
||||
A fixed date format is then applied to columns D to G, and the top row is formatted.
|
||||
Finally the workbook is saved and the Excel package object is closed.
|
||||
.Example
|
||||
>
|
||||
$r = Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Range "B1:B100" -ThreeIconsSet Flags -Passthru
|
||||
$r.Reverse = $true ; $r.Icon1.Type = "Num"; $r.Icon2.Type = "Num" ; $r.Icon2.value = 100 ; $r.Icon3.type = "Num" ;$r.Icon3.value = 1000
|
||||
|
||||
Again Export-Excel has been called with -Passthru leaving a package object in $Excel
|
||||
This time B1:B100 has been conditionally formatted with 3 icons, using the "Flags" Icon-Set.
|
||||
Add-ConditionalFormatting does not provide access to every option in the formatting rule, so passthru has been used and the
|
||||
rule is modified to apply the flags in reverse order, and transitions between flags are set to 100 and 1000
|
||||
Again Export-Excel has been called with -PassThru leaving a package object
|
||||
in $Excel. This time B1:B100 has been conditionally formatted with 3 icons,
|
||||
using the "Flags" Icon-Set. Add-ConditionalFormatting does not provide access
|
||||
to every option in the formatting rule, so -PassThru has been used and the
|
||||
rule is modified to apply the flags in reverse order, and transitions
|
||||
between flags are set to 100 and 1000.
|
||||
.Example
|
||||
Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red
|
||||
|
||||
This time $sheet holds an ExcelWorkseet object and databars are added to column D excluding the top row.
|
||||
This time $sheet holds an ExcelWorkshseet object and databars are added to
|
||||
column D, excluding the top row.
|
||||
.Example
|
||||
Add-ConditionalFormatting -Address $worksheet.cells["FinishPosition"] -RuleType Equal -ConditionValue 1 -ForeGroundColor Purple -Bold -Priority 1 -StopIfTrue
|
||||
Add-ConditionalFormatting -Address $worksheet.cells["FinishPosition"] -RuleType Equal -ConditionValue 1 -ForeGroundColor Purple -Bold -Priority 1 -StopIfTrue
|
||||
|
||||
In this example a named range is used to select the cells where the condition should apply,
|
||||
and instead of specifying a sheet and range within the sheet as separate paramters,
|
||||
the cells where the format should apply are specified directly.
|
||||
If a cell in the "FinishPosition" range is 1, then the text is turned to Bold & Purple.
|
||||
This rule is moved to first in the priority list, and where cells have a value of 1, no other rules will be processed.
|
||||
In this example a named range is used to select the cells where the condition
|
||||
should apply, and instead of specifying a sheet and range within the sheet as
|
||||
separate parameters, the cells where the format should apply are specified
|
||||
directly. If a cell in the "FinishPosition" range is 1, then the text is
|
||||
turned to Bold & Purple. This rule is moved to first in the priority list,
|
||||
and where cells have a value of 1, no other rules will be processed.
|
||||
.Example
|
||||
>
|
||||
$excel = Get-ChildItem | Select-Object -Property Name,Length,LastWriteTime,CreationTime | Export-Excel "$env:temp\test43.xlsx" -PassThru -AutoSize
|
||||
@@ -60,22 +67,30 @@
|
||||
|
||||
Close-ExcelPackage -Show $excel
|
||||
|
||||
The first few lines of code export a list of file and directory names, sizes and dates to a spreadsheet. It puts the date of the export in Cell F1.
|
||||
The first Conditional format changes the color of files and folders that begin with a ".", "_" or anything else which sorts before "A"
|
||||
The second Conditional format changes the Number format of numbers bigger than 1 million for example 1,234,567,890 will dispay as "1,234.57M"
|
||||
The third looks highlights datestamps of files less than a week old when the export was run.
|
||||
The = is necessary in the condition value to otherwise the rule will look for the the text INT($F$1-7).
|
||||
The cell address for the date is fixed using the standard Excel $ notation.
|
||||
The final Conditional format looks for files which have not changed since they were created. Here the condition value is "=C2". The = Sign means C2 is
|
||||
treated as a formula, not literal text. Unlike the file age, we want the cell used to change for each cell where the conditional format applies.
|
||||
The first cell in the conditional format range is D2, which is compared against C2, then D3 is compared against C3 and so on.
|
||||
A common mistake is to include the title row in the range and accidentally apply conditional formatting to it,
|
||||
or to begin the range at row 2 but use row 1 as the starting point for comparisons.
|
||||
The first few lines of code export a list of file and directory names, sizes
|
||||
and dates to a spreadsheet. It puts the date of the export in cell F1.
|
||||
The first Conditional format changes the color of files and folders that begin
|
||||
with a ".", "_" or anything else which sorts before "A".
|
||||
The second Conditional format changes the Number format of numbers bigger than
|
||||
1 million, for example 1,234,567,890 will dispay as "1,234.57M"
|
||||
The third highlights datestamps of files less than a week old when the export
|
||||
was run; the = is necessary in the condition value otherwise the rule will
|
||||
look for the the text INT($F$1-7), and the cell address for the date is fixed
|
||||
using the standard Excel $ notation.
|
||||
The final Conditional format looks for files which have not changed since they
|
||||
were created. Here the condition value is "=C2". The = sign means C2 is treated
|
||||
as a formula, not literal text. Unlike the file age, we want the cell used to
|
||||
change for each cell where the conditional format applies. The first cell in
|
||||
the conditional format range is D2, which is compared against C2, then D3 is
|
||||
compared against C3 and so on. A common mistake is to include the title row in
|
||||
the range and accidentally apply conditional formatting to it, or to begin the
|
||||
range at row 2 but use row 1 as the starting point for comparisons.
|
||||
.Example
|
||||
Add-ConditionalFormatting $ws.Cells["B:B"] GreaterThan 10000000 -Fore Red -Stop -Pri 1
|
||||
|
||||
This version shows the shortest syntax - the Address, Ruletype, and Conditionvalue can be identified from their position,
|
||||
and ForegroundColor, StopIfTrue and Priority can all be shortend.
|
||||
This version shows the shortest syntax - the Address, Ruletype, and
|
||||
Conditionvalue can be identified from their position, and ForegroundColor,
|
||||
StopIfTrue and Priority can all be shortend.
|
||||
|
||||
#>
|
||||
Param (
|
||||
@@ -84,15 +99,15 @@
|
||||
[Alias("Range")]
|
||||
$Address ,
|
||||
#The worksheet where the format is to be applied
|
||||
[OfficeOpenXml.ExcelWorksheet]$WorkSheet ,
|
||||
[OfficeOpenX1ml.ExcelWorksheet]$WorkSheet ,
|
||||
#A standard named-rule - Top / Bottom / Less than / Greater than / Contains etc.
|
||||
[Parameter(Mandatory = $true, ParameterSetName = "NamedRule", Position = 1)]
|
||||
[OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType]$RuleType ,
|
||||
#Text colour for matching objects
|
||||
#Text color for matching objects
|
||||
[Parameter(ParameterSetName = "NamedRule")]
|
||||
[Alias("ForegroundColour")]
|
||||
[System.Drawing.Color]$ForegroundColor,
|
||||
#Colour for databar type charts
|
||||
#Color for databar type charts
|
||||
[Parameter(Mandatory = $true, ParameterSetName = "DataBar")]
|
||||
[Alias("DataBarColour")]
|
||||
[System.Drawing.Color]$DataBarColor,
|
||||
@@ -114,16 +129,16 @@
|
||||
#A value for the condition (for example 2000 if the test is 'lessthan 2000'; Formulas should begin with "=" )
|
||||
[Parameter(ParameterSetName = "NamedRule",Position = 2)]
|
||||
$ConditionValue,
|
||||
#A second value for the conditions like "between x and Y"
|
||||
#A second value for the conditions like "Between X and Y"
|
||||
[Parameter(ParameterSetName = "NamedRule",Position = 3)]
|
||||
$ConditionValue2,
|
||||
#Background colour for matching items
|
||||
#Background color for matching items
|
||||
[Parameter(ParameterSetName = "NamedRule")]
|
||||
[System.Drawing.Color]$BackgroundColor,
|
||||
#Background pattern for matching items
|
||||
[Parameter(ParameterSetName = "NamedRule")]
|
||||
[OfficeOpenXml.Style.ExcelFillStyle]$BackgroundPattern = [OfficeOpenXml.Style.ExcelFillStyle]::None ,
|
||||
#Secondary colour when a background pattern requires it
|
||||
#Secondary color when a background pattern requires it
|
||||
[Parameter(ParameterSetName = "NamedRule")]
|
||||
[System.Drawing.Color]$PatternColor,
|
||||
#Sets the numeric format for matching items
|
||||
|
||||
133
Export-Excel.ps1
133
Export-Excel.ps1
@@ -1,21 +1,25 @@
|
||||
function Export-Excel {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Export data to an Excel worksheet.
|
||||
Exports data to an Excel worksheet.
|
||||
.DESCRIPTION
|
||||
Export data to an Excel file and where possible try to convert numbers so Excel recognizes them as numbers instead of text. After all. Excel is a spreadsheet program used for number manipulation and calculations. In case the number conversion is not desired, use the parameter '-NoNumberConversion *'.
|
||||
Exports data to an Excel file and where possible tries to convert numbers
|
||||
in text fields so Excel recognizes them as numbers instead of text.
|
||||
After all: Excel is a spreadsheet program used for number manipulation
|
||||
and calculations. If number conversion is not desired, use the
|
||||
parameter -NoNumberConversion *.
|
||||
.PARAMETER Path
|
||||
Path to a new or existing .XLSX file.
|
||||
.PARAMETER ExcelPackage
|
||||
An object representing an Excel Package - usually this is returned by specifying -Passthru allowing multiple commands to work on the same Workbook without saving and reloading each time.
|
||||
An object representing an Excel Package - usually this is returned by specifying -PassThru allowing multiple commands to work on the same workbook without saving and reloading each time.
|
||||
.PARAMETER WorksheetName
|
||||
The name of a sheet within the workbook - "Sheet1" by default.
|
||||
.PARAMETER ClearSheet
|
||||
If specified Export-Excel will remove any existing worksheet with the selected name. The Default behaviour is to overwrite cells in this sheet as needed (but leaving non-overwritten ones in place).
|
||||
.PARAMETER Append
|
||||
If specified data will be added to the end of an existing sheet, using the same column headings.
|
||||
If specified dat,a will be added to the end of an existing sheet, using the same column headings.
|
||||
.PARAMETER TargetData
|
||||
Data to insert onto the worksheet - this is often provided from the pipeline.
|
||||
Data to insert onto the worksheet - this is usually provided from the pipeline.
|
||||
.PARAMETER DisplayPropertySet
|
||||
Many (but not all) objects have a hidden property named psStandardmembers with a child property DefaultDisplayPropertySet ; this parameter reduces the properties exported to those in this set.
|
||||
.PARAMETER NoAliasOrScriptPropeties
|
||||
@@ -37,36 +41,36 @@
|
||||
.PARAMETER Password
|
||||
Sets password protection on the workbook.
|
||||
.PARAMETER IncludePivotTable
|
||||
Adds a Pivot table using the data in the worksheet.
|
||||
Adds a PivotTable using the data in the worksheet.
|
||||
.PARAMETER PivotTableName
|
||||
If a Pivot table is created from command line parameters, specifies the name of the new sheet holding the pivot. Defaults to "WorksheetName-PivotTable"
|
||||
If a PivotTable is created from command line parameters, specifies the name of the new sheet holding the pivot. Defaults to "WorksheetName-PivotTable".
|
||||
.PARAMETER PivotRows
|
||||
Name(s) columns from the spreadsheet which will provide the Row name(s) in a pivot table created from command line parameters.
|
||||
Name(s) of column(s) from the spreadsheet which will provide the Row name(s) in a PivotTable created from command line parameters.
|
||||
.PARAMETER PivotColumns
|
||||
Name(s) columns from the spreadsheet which will provide the Column name(s) in a pivot table created from command line parameters.
|
||||
Name(s) of columns from the spreadsheet which will provide the Column name(s) in a PivotTable created from command line parameters.
|
||||
.PARAMETER PivotFilter
|
||||
Name(s) columns from the spreadsheet 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 PivotTable 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.
|
||||
In a PivotTable 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 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.
|
||||
In a PivotTable 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.
|
||||
By default, PivotTables 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 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.
|
||||
Instead of describing a single PivotTable with multiple command-line 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.
|
||||
Include a chart with the PivotTable - implies -IncludePivotTable.
|
||||
.PARAMETER ChartType
|
||||
The type for Pivot chart (one of Excel's defined chart types)
|
||||
The type for PivotChart (one of Excel's defined chart types).
|
||||
.PARAMETER NoLegend
|
||||
Exclude the legend from the pivot chart.
|
||||
Exclude the legend from the PivotChart.
|
||||
.PARAMETER ShowCategory
|
||||
Add category labels to the pivot chart.
|
||||
Add category labels to the PivotChart.
|
||||
.PARAMETER ShowPercent
|
||||
Add Percentage labels to the pivot chart.
|
||||
Add percentage labels to the PivotChart.
|
||||
.PARAMETER ConditionalFormat
|
||||
One or more conditional formatting rules defined with New-ConditionalFormattingIconSet.
|
||||
.PARAMETER ConditionalText
|
||||
@@ -74,13 +78,13 @@
|
||||
.PARAMETER NoNumberConversion
|
||||
By default we convert all values to numbers if possible, but this isn't always desirable. NoNumberConversion allows you to add exceptions for the conversion. Wildcards (like '*') are allowed.
|
||||
.PARAMETER BoldTopRow
|
||||
Makes the top Row boldface.
|
||||
Makes the top row boldface.
|
||||
.PARAMETER NoHeader
|
||||
Does not put field names at the top of columns.
|
||||
.PARAMETER RangeName
|
||||
Makes the data in the worksheet a named range.
|
||||
.PARAMETER TableName
|
||||
Makes the data in the worksheet a table with a name applies a style to it. Name must not contain spaces.
|
||||
Makes the data in the worksheet a table with a name, and applies a style to it. Name must not contain spaces.
|
||||
.PARAMETER TableStyle
|
||||
Selects the style for the named table - defaults to 'Medium6'.
|
||||
.PARAMETER BarChart
|
||||
@@ -92,11 +96,11 @@
|
||||
.PARAMETER PieChart
|
||||
Creates a "quick" pie chart using the first text column as labels and the first numeric column as values
|
||||
.PARAMETER ExcelChartDefinition
|
||||
A hash table containing ChartType, Title, NoLegend, ShowCategory, ShowPercent, Yrange, Xrange and SeriesHeader for one or more [non-pivot] charts.
|
||||
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 the selection would cause all sheets to be hidden, the sheet being worked on will be revealed.
|
||||
.PARAMETER UnHideSheet
|
||||
Name(s) of Sheet(s) to Reveal in the workbook, supports wildcards.
|
||||
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.
|
||||
@@ -126,13 +130,13 @@
|
||||
.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 Excel filter on the complete header row, so users can easily sort, filter and/or search the data in the selected column.
|
||||
.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
|
||||
If there is already content in the workbook, a new sheet will not be active UNLESS Activate is specified; if a Pivot table is included it will be the active sheet
|
||||
If there is already content in the workbook, a new sheet will not be active UNLESS Activate is specified; if a PivotTable is included it will be the active sheet
|
||||
.PARAMETER Now
|
||||
The 'Now' switch is a shortcut that creates automatically a temporary file, enables 'AutoSize', 'AutoFiler' and 'Show', and opens the file immediately.
|
||||
The -Now switch is a shortcut that automatically creates a temporary file, enables "AutoSize", "AutoFiler" and "Show", and opens the file immediately.
|
||||
.PARAMETER NumberFormat
|
||||
Formats all values that can be converted to a number to the format specified.
|
||||
|
||||
@@ -162,7 +166,7 @@
|
||||
'[Blue]$#,##0.00;[Red]-$#,##0.00'
|
||||
|
||||
.PARAMETER ReZip
|
||||
If specified, Export-Excel will expand the contents of the .XLSX file (which is multiple files in a zip archive) and rebuilt it.
|
||||
If specified, Export-Excel will expand the contents of the .XLSX file (which is multiple files in a zip archive) and rebuild it.
|
||||
.PARAMETER NoClobber
|
||||
Not used. Left in to avoid problems with older scripts, it may be removed in future versions.
|
||||
.PARAMETER CellStyleSB
|
||||
@@ -171,7 +175,7 @@
|
||||
.PARAMETER Show
|
||||
Opens the Excel file immediately after creation. Convenient for viewing the results instantly without having to search for the file first.
|
||||
.PARAMETER ReturnRange
|
||||
If specified, Export-Excel returns the range of added cells in the format "A1:Z100"
|
||||
If specified, Export-Excel returns the range of added cells in the format "A1:Z100".
|
||||
.PARAMETER PassThru
|
||||
If specified, Export-Excel returns an object representing the Excel package without saving the package first.
|
||||
To save, you need to call Close-ExcelPackage or send the object back to Export-Excel, or use its .Save() or SaveAs() method.
|
||||
@@ -190,7 +194,9 @@
|
||||
Write-Output -1 668 34 777 860 -0.5 119 -0.1 234 788 |
|
||||
Export-Excel @ExcelParams -NumberFormat '[Blue]$#,##0.00;[Red]-$#,##0.00'
|
||||
|
||||
Exports all data to the Excel file 'Excel.xslx' and colors the negative values in 'Red' and the positive values in 'Blue'. It will also add a dollar sign '$' in front of the rounded numbers to two decimal characters behind the comma.
|
||||
Exports all data to the Excel file 'Excel.xslx' and colors the negative values
|
||||
in Red and the positive values in Blue. It will also add a dollar sign in front
|
||||
of the numbers which use a thousand seperator and display to two decimal places.
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
@@ -216,7 +222,9 @@
|
||||
PhoneNr3 = '+3244444444'
|
||||
} | Export-Excel @ExcelParams -NoNumberConversion IPAddress, Number1
|
||||
|
||||
Exports all data to the Excel file 'Excel.xslx' and tries to convert all values to numbers where possible except for 'IPAddress' and 'Number1'. These are stored in the sheet 'as is', without being converted to a number.
|
||||
Exports all data to the Excel file "Excel.xlsx" and tries to convert all values
|
||||
to numbers where possible except for "IPAddress" and "Number1", which are
|
||||
stored in the sheet 'as is', without being converted to a number.
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
@@ -242,7 +250,9 @@
|
||||
PhoneNr3 = '+3244444444'
|
||||
} | Export-Excel @ExcelParams -NoNumberConversion *
|
||||
|
||||
Exports all data to the Excel file 'Excel.xslx' as is, no number conversion will take place. This means that Excel will show the exact same data that you handed over to the 'Export-Excel' function.
|
||||
Exports all data to the Excel file 'Excel.xslx' as is, no number conversion
|
||||
will take place. This means that Excel will show the exact same data that
|
||||
you handed over to the 'Export-Excel' function.
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
@@ -257,9 +267,11 @@
|
||||
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 than '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
|
||||
that will show cells with a value is greater than 525, whith a
|
||||
background fill color of "LightPink" and the text in "DarkRed".
|
||||
Where condition is not met the color willbe the default, black
|
||||
text on a white background.
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
@@ -275,7 +287,12 @@
|
||||
New-ConditionalText Running Blue Cyan
|
||||
)
|
||||
|
||||
Export all services to an Excel sheet where all cells have a 'Conditional formatting rule' in Excel that will show the background fill color in 'LightPink' and the text color in 'DarkRed' when the value contains the word 'Stop'. If the value contains the word 'Running' it will have a background fill color in 'Cyan' and a text color 'Blue'. In case none of these conditions are met the color will be the default, black text on a white background.
|
||||
Exports all services to an Excel sheet, setting a Conditional formatting rule
|
||||
that will set the background fill color to "LightPink" and the text color
|
||||
to "DarkRed" when the value contains the word "Stop".
|
||||
If the value contains the word "Running" it will have a background fill
|
||||
color of "Cyan" and text colored 'Blue'. If neither condition is met, the
|
||||
color will be the default, black text on a white background.
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
@@ -310,7 +327,8 @@
|
||||
$Array | Out-GridView -Title 'Not showing Member3 and Member4'
|
||||
$Array | Update-FirstObjectProperties | Export-Excel @ExcelParams -WorksheetName Numbers
|
||||
|
||||
Updates the first object of the array by adding property 'Member3' and 'Member4'. Afterwards. all objects are exported to an Excel file and all column headers are visible.
|
||||
Updates the first object of the array by adding property 'Member3' and 'Member4'.
|
||||
Afterwards. all objects are exported to an Excel file and all column headers are visible.
|
||||
|
||||
.EXAMPLE
|
||||
Get-Process | Export-Excel .\test.xlsx -WorksheetName Processes -IncludePivotTable -Show -PivotRows Company -PivotData PM
|
||||
@@ -341,10 +359,10 @@
|
||||
Get-Process | Select-Object -Property Name,Company,Handles,CPU,VM | Export-Excel -Path .\test.xlsx -AutoSize -WorksheetName 'sheet2'
|
||||
Export-Excel -Path .\test.xlsx -PivotTableDefinition $pt -Show
|
||||
|
||||
This example defines two pivot tables. Then it puts Service data on Sheet1 with one call to Export-Excel and Process Data on sheet2 with a second call to Export-Excel.
|
||||
The third and final call adds the two pivot tables and opens the spreadsheet in Excel.
|
||||
|
||||
|
||||
This example defines two PivotTables. Then it puts Service data on Sheet1
|
||||
with one call to Export-Excel and Process Data on sheet2 with a second
|
||||
call to Export-Excel. The third and final call adds the two PivotTables
|
||||
and opens the spreadsheet in Excel.
|
||||
.EXAMPLE
|
||||
>
|
||||
PS> Remove-Item -Path .\test.xlsx
|
||||
@@ -356,8 +374,11 @@
|
||||
$excel.Dispose()
|
||||
Start-Process .\test.xlsx
|
||||
|
||||
This example uses -passthrough. It puts 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.
|
||||
This example uses -PassThru. It puts service information into sheet1 of the
|
||||
workbook and saves the ExcelPackage object 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. Other commands in the module remove the need
|
||||
to work directly with the package object in this way.
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
@@ -375,12 +396,13 @@
|
||||
foreach ($c in 5..9) {Set-ExcelRange -Address $sheet.Column($c) -AutoFit }
|
||||
Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -IncludePivotChart -ChartType ColumnClustered -NoLegend -PivotRows company -PivotData @{'Name'='Count'} -Show
|
||||
|
||||
This a more sophisticated version of the previous example showing different ways of using Set-ExcelRange, and also adding conditional formatting.
|
||||
In the final command a Pivot chart is added and the workbook is opened in Excel.
|
||||
This a more sophisticated version of the previous example showing different
|
||||
ways of using Set-ExcelRange, and also adding conditional formatting.
|
||||
In the final command a PivotChart is added and the workbook is opened in Excel.
|
||||
.EXAMPLE
|
||||
0..360 | ForEach-Object {[pscustomobject][ordered]@{X=$_; Sinx="=Sin(Radians(x)) "} } | Export-Excel -now -LineChart -AutoNameRange
|
||||
|
||||
Creates a line chart showing the value of Sine(x) for values of X between 0 and 360 degrees.
|
||||
Creates a line chart showing the value of Sine(x) for values of x between 0 and 360 degrees.
|
||||
.LINK
|
||||
https://github.com/dfinke/ImportExcel
|
||||
#>
|
||||
@@ -472,12 +494,12 @@
|
||||
[Object[]]$ConditionalFormat,
|
||||
[Object[]]$ConditionalText,
|
||||
[ScriptBlock]$CellStyleSB,
|
||||
#If there is already content in the workbook the sheet with the Pivot table will not be active UNLESS Activate is specified
|
||||
#If there is already content in the workbook the sheet with the PivotTable will not be active UNLESS Activate is specified
|
||||
[switch]$Activate,
|
||||
[Parameter(ParameterSetName = 'Now')]
|
||||
[Switch]$Now,
|
||||
[Switch]$ReturnRange,
|
||||
#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.
|
||||
#By default PivotTables have Totals for each Row (on the right) and for each column at the bottom. This allows just one or neither to be selected.
|
||||
[ValidateSet("Both","Columns","Rows","None")]
|
||||
[String]$PivotTotals = "Both",
|
||||
#Included for compatibility - equivalent to -PivotTotals "None"
|
||||
@@ -823,7 +845,7 @@
|
||||
'SourceRange' = $dataRange
|
||||
}
|
||||
if ($PivotTableName -and ($pkg.workbook.worksheets.tables.name -contains $PivotTableName)) {
|
||||
Write-Warning -Message "The selected Pivot table name '$PivotTableName' is already used as a table name. Adding a suffix of 'Pivot'."
|
||||
Write-Warning -Message "The selected PivotTable name '$PivotTableName' is already used as a table name. Adding a suffix of 'Pivot'."
|
||||
$PivotTableName += 'Pivot'
|
||||
}
|
||||
|
||||
@@ -991,7 +1013,7 @@
|
||||
Write-Verbose -Message "Added conditional formatting to range $($c.range)"
|
||||
}
|
||||
elseif ($c -is [hashtable] -or $c -is[System.Collections.Specialized.OrderedDictionary]) {
|
||||
if (-not $c.Range) {$c.Range = $ws.Dimension.Address }
|
||||
if (-not $c.Range -or $c.Address) {$c.Address = $ws.Dimension.Address }
|
||||
Add-ConditionalFormatting -WorkSheet $ws @c
|
||||
}
|
||||
}
|
||||
@@ -1173,7 +1195,7 @@ function Select-Worksheet {
|
||||
Sets the selected tab in an Excel workbook to be the chosen sheet and unselects all the others.
|
||||
.DESCRIPTION
|
||||
Sometimes when a sheet is added we want it to be the active sheet, sometimes we want the active sheet to be left as it was.
|
||||
Select-Worksheet exists to change the which sheet is the selected tab when Excel opens the file.
|
||||
Select-Worksheet exists to change which sheet is the selected tab when Excel opens the file.
|
||||
.EXAMPLE
|
||||
Select-Worksheet -ExcelWorkbook $ExcelWorkbook -WorksheetName "NewSheet"
|
||||
$ExcelWorkbook holds a workbook object containing a sheet named "NewSheet";
|
||||
@@ -1184,10 +1206,11 @@ function Select-Worksheet {
|
||||
This sheet will become the [only] active sheet in the workbook.
|
||||
.EXAMPLE
|
||||
Select-Worksheet -ExcelWorksheet $ws
|
||||
$ws holds an Excel worksheet which will become the [only] active sheet in the workbook.
|
||||
$ws holds an Excel worksheet which will become the [only] active sheet
|
||||
in its workbook.
|
||||
#>
|
||||
param (
|
||||
#An object representing an Excel Package.
|
||||
#An object representing an ExcelPackage.
|
||||
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Package', Position = 0)]
|
||||
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
||||
#An Excel workbook to which the Worksheet will be added - a package contains one Workbook so you can use workbook or package as it suits.
|
||||
@@ -1195,7 +1218,7 @@ function Select-Worksheet {
|
||||
[OfficeOpenXml.ExcelWorkbook]$ExcelWorkbook,
|
||||
[Parameter(ParameterSetName='Package')]
|
||||
[Parameter(ParameterSetName='Workbook')]
|
||||
#The name of the worksheet 'Sheet1' by default.
|
||||
#The name of the worksheet "Sheet1" by default.
|
||||
[string]$WorksheetName,
|
||||
#An object representing an Excel worksheet.
|
||||
[Parameter(ParameterSetName='Sheet',Mandatory=$true)]
|
||||
|
||||
@@ -3,10 +3,13 @@
|
||||
.SYNOPSIS
|
||||
Combines data on all the sheets in an Excel worksheet onto a single sheet.
|
||||
.DESCRIPTION
|
||||
Join worksheet can work in two main ways:
|
||||
Either Combining data which has the same layout from many pages into one, or combining pages which have nothing in common.
|
||||
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.
|
||||
Join-Worksheet can work in two main ways, either
|
||||
Combining data which has the same layout from many pages into one, or
|
||||
combining pages which have nothing in common.
|
||||
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
|
||||
>
|
||||
PS> foreach ($computerName in @('Server1', 'Server2', 'Server3', 'Server4')) {
|
||||
@@ -16,11 +19,15 @@
|
||||
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 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.
|
||||
The foreach command gets the services running on four servers and exports each
|
||||
to its own page in Test.xlsx.
|
||||
$PtDef= creates a definition for a PivotTable.
|
||||
The Join-Worksheet command uses the same file and merges the results into 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 thePivotTable.
|
||||
|
||||
.EXAMPLE
|
||||
>
|
||||
@@ -30,10 +37,14 @@
|
||||
Export-Excel -Path "$env:COMPUTERNAME.xlsx" -WorkSheetname NetAdapter
|
||||
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.
|
||||
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.
|
||||
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 dissimilar, -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.
|
||||
#>
|
||||
[CmdletBinding(DefaultParameterSetName = 'Default')]
|
||||
param (
|
||||
@@ -41,21 +52,21 @@
|
||||
[Parameter(ParameterSetName = "Default", Position = 0)]
|
||||
[Parameter(ParameterSetName = "Table" , Position = 0)]
|
||||
[String]$Path ,
|
||||
# An object representing an Excel Package - usually this is returned by specifying -Passthru allowing multiple commands to work on the same Workbook without saving and reloading each time.
|
||||
# An object representing an Excel Package - either from Open-Excel Package or specifying -PassThru to Export-Excel.
|
||||
[Parameter(Mandatory = $true, ParameterSetName = "PackageDefault")]
|
||||
[Parameter(Mandatory = $true, ParameterSetName = "PackageTable")]
|
||||
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
||||
# The name of a sheet within the workbook where the other sheets will be joined together - "Combined" by default.
|
||||
$WorkSheetName = 'Combined',
|
||||
# If specified any pre-existing target for the joined data will be deleted and re-created; otherwise data will be appended on this sheet.
|
||||
# If specified ,any pre-existing target for the joined data will be deleted and re-created; otherwise data will be appended on this sheet.
|
||||
[switch]$Clearsheet,
|
||||
#Join-Worksheet assumes each sheet has identical headers and the headers should be copied to the target sheet, unless -NoHeader is specified.
|
||||
[switch]$NoHeader,
|
||||
#If -NoHeader is NOT specified, then rows of data will be labeled with the name of the sheet they came, FromLabel is the header for this column. If it is null or empty, the labels will be omitted.
|
||||
#If -NoHeader is NOT specified, then rows of data will be labeled with the name of the sheet they came from. FromLabel is the header for this column. If it is null or empty, the labels will be omitted.
|
||||
$FromLabel = "From" ,
|
||||
#If specified, the copied blocks of data will have the name of the sheet they were copied from inserted above them as a title.
|
||||
[switch]$LabelBlocks,
|
||||
#Sizes the width of the Excel column to the maximum width needed to display all the containing data in that cell.
|
||||
#Sets the width of the Excel columns to display all the data in their cells.
|
||||
[Switch]$AutoSize,
|
||||
#Freezes headers etc. in the top row.
|
||||
[Switch]$FreezeTopRow,
|
||||
@@ -65,13 +76,13 @@
|
||||
[Switch]$FreezeTopRowFirstColumn,
|
||||
# Freezes panes at specified coordinates (in the form RowNumber , ColumnNumber).
|
||||
[Int[]]$FreezePane,
|
||||
#Enables the 'Filter' in Excel on the complete header row. So users can easily sort, filter and/or search the data in the select column from within Excel.
|
||||
#Enables the Excel filter on the headers of the combined sheet.
|
||||
[Parameter(ParameterSetName = 'Default')]
|
||||
[Parameter(ParameterSetName = 'PackageDefault')]
|
||||
[Switch]$AutoFilter,
|
||||
#Makes the top Row boldface.
|
||||
#Makes the top row boldface.
|
||||
[Switch]$BoldTopRow,
|
||||
#If Specified hides the sheets that the data is copied from.
|
||||
#If specified, hides the sheets that the data is copied from.
|
||||
[switch]$HideSource,
|
||||
#Text of a title to be placed in Cell A1.
|
||||
[String]$Title,
|
||||
@@ -83,7 +94,7 @@
|
||||
[Switch]$TitleBold,
|
||||
#Sets the point size for the title.
|
||||
[Int]$TitleSize = 22,
|
||||
#Hashtable(s) with Sheet PivotRows, PivotColumns, PivotData, IncludePivotChart and ChartType values to specify a definition for one or more pivot table(s).
|
||||
#Hashtable(s) with Sheet PivotRows, PivotColumns, PivotData, IncludePivotChart and ChartType values to specify a definition for one or morePivotTable(s).
|
||||
[Hashtable]$PivotTableDefinition,
|
||||
#A hashtable containing ChartType, Title, NoLegend, ShowCategory, ShowPercent, Yrange, Xrange and SeriesHeader for one or more [non-pivot] charts.
|
||||
[Object[]]$ExcelChartDefinition,
|
||||
@@ -107,13 +118,13 @@
|
||||
})]
|
||||
[Parameter(ParameterSetName = 'Table' , Mandatory = $true)]
|
||||
[Parameter(ParameterSetName = 'PackageTable' , Mandatory = $true)]
|
||||
# Makes the data in the worksheet a table with a name applies a style to it. Name must not contain spaces.
|
||||
# Makes the data in the worksheet a table with a name and applies a style to it. Name must not contain spaces.
|
||||
[String]$TableName,
|
||||
[Parameter(ParameterSetName = 'Table')]
|
||||
[Parameter(ParameterSetName = 'PackageTable')]
|
||||
#Selects the style for the named table - defaults to 'Medium6'.
|
||||
#Selects the style for the named table - defaults to "Medium6".
|
||||
[OfficeOpenXml.Table.TableStyles]$TableStyle = 'Medium6',
|
||||
#Selects the style for the named table - defaults to 'Medium6'.
|
||||
#If specified, returns the range of cells in the combined sheet, in the format "A1:Z100".
|
||||
[switch]$ReturnRange,
|
||||
#Opens the Excel file immediately after creation. Convenient for viewing the results instantly without having to search for the file first.
|
||||
[switch]$Show,
|
||||
|
||||
@@ -4,29 +4,36 @@
|
||||
Merges two Worksheets (or other objects) into a single Worksheet with differences marked up.
|
||||
.Description
|
||||
The Compare-Worksheet command takes two Worksheets and marks differences in the source document, and optionally outputs a grid showing the changes.
|
||||
By contrast the Merge-Worksheet command takes the Worksheets and combines them into a single sheet showing the old and new data side by side .
|
||||
Although it is designed to work with Excel data it can work with arrays of any kind of object; so it can be a merge *of* Worksheets, or a merge *to* Worksheet.
|
||||
By contrast the Merge-Worksheet command takes the Worksheets and combines them into a single sheet showing the old and new data side by side.
|
||||
Although it is designed to work with Excel data it can work with arrays of any kind of object; so it can be a merge *of* Worksheets, or a merge *to* a Worksheet.
|
||||
.Example
|
||||
merge-Worksheet "Server54.xlsx" "Server55.xlsx" -WorksheetName services -OutputFile Services.xlsx -OutputSheetName 54-55 -show
|
||||
The workbooks contain audit information for two servers, one page contains a list of services. This command creates a Worksheet named 54-55
|
||||
in a workbook named services which shows all the services and their differences, and opens it in Excel.
|
||||
Merge-Worksheet "Server54.xlsx" "Server55.xlsx" -WorksheetName services -OutputFile Services.xlsx -OutputSheetName 54-55 -show
|
||||
The workbooks contain audit information for two servers, one sheet contains
|
||||
a list of services. This command creates a worksheet named "54-55" in a
|
||||
workbook named "services.xlsx" which shows all the services and their
|
||||
differences, and opens the new workbook in Excel.
|
||||
.Example
|
||||
merge-Worksheet "Server54.xlsx" "Server55.xlsx" -WorksheetName services -OutputFile Services.xlsx -OutputSheetName 54-55 -HideEqual -AddBackgroundColor LightBlue -show
|
||||
This modifies the previous command to hide the equal rows in the output sheet and changes the color used to mark rows added to the second file.
|
||||
Merge-Worksheet "Server54.xlsx" "Server55.xlsx" -WorksheetName services -OutputFile Services.xlsx -OutputSheetName 54-55 -HideEqual -AddBackgroundColor LightBlue -show
|
||||
This modifies the previous command to hide the equal rows in the output
|
||||
sheet and changes the color used to mark rows added to the second file.
|
||||
.Example
|
||||
merge-Worksheet -OutputFile .\j1.xlsx -OutputSheetName test11 -ReferenceObject (dir .\ImportExcel\4.0.7) -DifferenceObject (dir .\ImportExcel\4.0.8) -Property Length -Show
|
||||
Merge-Worksheet -OutputFile .\j1.xlsx -OutputSheetName test11 -ReferenceObject (dir .\ImportExcel\4.0.7) -DifferenceObject (dir .\ImportExcel\4.0.8) -Property Length -Show
|
||||
This version compares two directories, and marks what has changed.
|
||||
Because no "Key" property is given, "Name" is assumed to be the key and the only other property examined is length.
|
||||
Files which are added or deleted or have changed size will be highlighed in the output sheet. Changes to dates or other attributes will be ignored.
|
||||
Because no "Key" property is given, "Name" is assumed to be the key
|
||||
and the only other property examined is length. Files which are added
|
||||
or deleted or have changed size will be highlighed in the output sheet.
|
||||
Changes to dates or other attributes will be ignored.
|
||||
.Example
|
||||
merge-Worksheet -RefO (dir .\ImportExcel\4.0.7) -DiffO (dir .\ImportExcel\4.0.8) -Pr Length | Out-GridView
|
||||
This time no file is written and the results -which include all properties, not just length, are output and sent to Out-Gridview.
|
||||
This version uses aliases to shorten the parameters,
|
||||
(OutputFileName can be "outFile" and the sheet "OutSheet" : DifferenceObject & ReferenceObject can be DiffObject & RefObject).
|
||||
Merge-Worksheet -RefO (dir .\ImportExcel\4.0.7) -DiffO (dir .\ImportExcel\4.0.8) -Pr Length | Out-GridView
|
||||
This time no file is written and the results - which include all properties,
|
||||
not just length, are output and sent to Out-Gridview. This version uses
|
||||
aliases to shorten the parameters, (OutputFileName can be "outFile" and
|
||||
the Sheet can be"OutSheet"; DifferenceObject & ReferenceObject can be
|
||||
DiffObject & RefObject respectively).
|
||||
#>
|
||||
[cmdletbinding(SupportsShouldProcess=$true)]
|
||||
Param(
|
||||
#First Excel file to compare. You can compare two Excel files or two other objects but not one of each.
|
||||
#First Excel file to compare. You can compare two Excel files or two other objects or a reference obhct against a difference file, but not a reference file against an object.
|
||||
[parameter(ParameterSetName='A',Mandatory=$true,Position=0)] #A = Compare two files default headers
|
||||
[parameter(ParameterSetName='B',Mandatory=$true,Position=0)] #B = Compare two files user supplied headers
|
||||
[parameter(ParameterSetName='C',Mandatory=$true,Position=0)] #C = Compare two files headers P1, P2, P3 etc
|
||||
@@ -36,12 +43,12 @@
|
||||
[parameter(ParameterSetName='A',Mandatory=$true,Position=1)]
|
||||
[parameter(ParameterSetName='B',Mandatory=$true,Position=1)]
|
||||
[parameter(ParameterSetName='C',Mandatory=$true,Position=1)]
|
||||
[parameter(ParameterSetName='E',Mandatory=$true,Position=1)] #D Compat two objects; E = Compare one object one file that uses default headers
|
||||
[parameter(ParameterSetName='E',Mandatory=$true,Position=1)] #D Compare two objects; E = Compare one object one file that uses default headers
|
||||
[parameter(ParameterSetName='F',Mandatory=$true,Position=1)] #F = Compare one object one file that uses user supplied headers
|
||||
[parameter(ParameterSetName='G',Mandatory=$true,Position=1)] #G Compare one object one file that uses headers P1, P2, P3 etc
|
||||
$Differencefile ,
|
||||
|
||||
#Name(s) of Worksheets to compare,
|
||||
#Name(s) of Worksheets to compare.
|
||||
[parameter(ParameterSetName='A',Position=2)] #Applies to all sets EXCEPT D which is two objects (no sheets)
|
||||
[parameter(ParameterSetName='B',Position=2)]
|
||||
[parameter(ParameterSetName='C',Position=2)]
|
||||
@@ -59,12 +66,12 @@
|
||||
[parameter(ParameterSetName='G')]
|
||||
[int]$Startrow = 1,
|
||||
|
||||
#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 column headers of the Start ROw.
|
||||
[Parameter(ParameterSetName='B',Mandatory=$true)] #Compare object + sheet or 2 sheets with user supplied headers
|
||||
[Parameter(ParameterSetName='F',Mandatory=$true)]
|
||||
[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 using the values the top row of the sheet.
|
||||
[Parameter(ParameterSetName='C',Mandatory=$true)] #Compare object + sheet or 2 sheets with headers of P1, P2, P3 ...
|
||||
[Parameter(ParameterSetName='G',Mandatory=$true)]
|
||||
[switch]$NoHeader,
|
||||
@@ -108,9 +115,9 @@
|
||||
[System.Drawing.Color]$DeleteBackgroundColor = [System.Drawing.Color]::LightPink,
|
||||
#Sets the background color for rows not in the reference but added to the difference sheet.
|
||||
[System.Drawing.Color]$AddBackgroundColor = [System.Drawing.Color]::PaleGreen,
|
||||
#if Specified hides the rows in the spreadsheet that are equal and only shows changes, added or deleted rows.
|
||||
#if specified, hides the rows in the spreadsheet that are equal and only shows changes, added or deleted rows.
|
||||
[switch]$HideEqual ,
|
||||
#If specified outputs the data to the pipeline (you can add -whatif so it the command only outputs to the pipeline).
|
||||
#If specified, outputs the data to the pipeline (you can add -WhatIf so the command only outputs to the pipeline).
|
||||
[switch]$Passthru ,
|
||||
#If specified, opens the output workbook.
|
||||
[Switch]$Show
|
||||
@@ -311,39 +318,59 @@ Function Merge-MultipleSheets {
|
||||
.Synopsis
|
||||
Merges Worksheets into a single Worksheet with differences marked up.
|
||||
.Description
|
||||
The Merge Worksheet command combines 2 sheets. Merge-MultipleSheets is designed to merge more than 2.
|
||||
So if asked to merge sheets A,B,C which contain Services, with a Name, Displayname and Start mode, where "name" is treated as the key
|
||||
Merge-MultipleSheets calls Merge-Worksheet to merge Name, Displayname and Start mode, from sheets A and C
|
||||
the result has column headings -Row, Name, DisplayName, Startmode, C-DisplayName, C-StartMode C-Is, C-Row
|
||||
Merge-MultipleSheets then calls Merge-Worsheet with this result and sheet B, comparing 'Name', 'Displayname' and 'Start mode' columns on each side
|
||||
which outputs _Row, Name, DisplayName, Startmode, B-DisplayName, B-StartMode B-Is, B-Row, C-DisplayName, C-StartMode C-Is, C-Row
|
||||
Any columns in the "reference" side which are not used in the comparison are appended on the right, which is we compare the sheets in reverse order.
|
||||
The Merge Worksheet command combines two sheets. Merge-MultipleSheets is
|
||||
designed to merge more than two. So if asked to merge sheets A,B,C which
|
||||
contain Services, with a Name, Displayname and Start mode, where "Name" is
|
||||
treated as the key, Merge-MultipleSheets calls Merge-Worksheet to merge
|
||||
"Name", "Displayname" and "Startmode" from sheets A and C; the result has
|
||||
column headings "_Row", "Name", "DisplayName", "Startmode", "C-DisplayName",
|
||||
"C-StartMode", "C-Is" and "C-Row".
|
||||
Merge-MultipleSheets then calls Merge-Worksheet again passing it the
|
||||
intermediate result and sheet B, comparing "Name", "Displayname" and
|
||||
"Start mode" columns on each side, and gets a result with columns "_Row",
|
||||
"Name", "DisplayName", "Startmode", "B-DisplayName", "B-StartMode", "B-Is",
|
||||
"B-Row", "C-DisplayName", "C-StartMode", "C-Is" and "C-Row". Any columns on
|
||||
the "reference" side which are not used in the comparison are added on the
|
||||
right, which is why we compare the sheets in reverse order.
|
||||
|
||||
The "Is" column holds "Same", "Added", "Removed" or "Changed" and is used for conditional formatting in the output sheet (this is hidden by default),
|
||||
and when the data is written to Excel the "reference" columns, in this case "DisplayName" and "Start" are renamed to reflect their source,
|
||||
so become "A-DisplayName" and "A-Start".
|
||||
The "Is" columns hold "Same", "Added", "Removed" or "Changed" and is used for
|
||||
conditional formatting in the output sheet (these columns are hidden by default),
|
||||
and when the data is written to Excel the "reference" columns, in this case
|
||||
"DisplayName" and "Start" are renamed to reflect their source, so become
|
||||
"A-DisplayName" and "A-Start".
|
||||
|
||||
Conditional formatting is also applied to the "key" column (name in this case) so the view can be filtered to rows with changes by filtering this column on color.
|
||||
Conditional formatting is also applied to the Key column ("Name" in this
|
||||
case) so the view can be filtered to rows with changes by filtering this
|
||||
column on color.
|
||||
|
||||
Note: the processing order can affect what is seen as a change. For example if there is an extra item in sheet B in the example above,
|
||||
Sheet C will be processed and that row and will not be seen to be missing. When sheet B is processed it is marked as an addition, and the conditional formatting marks
|
||||
the entries from sheet A to show that a values were added in at least one sheet.
|
||||
However if Sheet B is the reference sheet, A and C will be seen to have an item removed;
|
||||
and if B is processed before C, the extra item is known when C is processed and so C is considered to be missing that item.
|
||||
Note: the processing order can affect what is seen as a change. For example
|
||||
if there is an extra item in sheet B in the example above, Sheet C will be
|
||||
processed and that row and will not be seen to be missing. When sheet B is
|
||||
processed it is marked as an addition, and the conditional formatting marks
|
||||
the entries from sheet A to show that a values were added in at least one
|
||||
sheet. However if Sheet B is the reference sheet, A and C will be seen to
|
||||
have an item removed; and if B is processed before C, the extra item is
|
||||
known when C is processed and so C is considered to be missing that item.
|
||||
.Example
|
||||
dir Server*.xlsx | Merge-MulipleSheets -WorksheetName Services -OutputFile Test2.xlsx -OutputSheetName Services -Show
|
||||
We are auditing servers and each one has a workbook in the current directory which contains a "Services" Worksheet (the result of
|
||||
Get-WmiObject -Class win32_service | Select-Object -Property Name, Displayname, Startmode
|
||||
No key is specified so the key is assumed to be the "Name" column. The files are merged and the result is opened on completion.
|
||||
Here we are auditing servers and each one has a workbook in the current
|
||||
directory which contains a "Services" Worksheet (the result of
|
||||
Get-WmiObject -Class win32_service | Select-Object -Property Name, Displayname, Startmode)
|
||||
No key is specified so the key is assumed to be the "Name" column.
|
||||
The files are merged and the result is opened on completion.
|
||||
.Example
|
||||
dir Serv*.xlsx | Merge-MulipleSheets -WorksheetName Software -Key "*" -ExcludeProperty Install* -OutputFile Test2.xlsx -OutputSheetName Software -Show
|
||||
The server audit files in the previous example also have "Software" Worksheet, but no single field on that sheet works as a key.
|
||||
Specifying "*" for the key produces a compound key using all non-excluded fields (and the installation date and file location are excluded).
|
||||
The server audit files in the previous example also have "Software" worksheet,
|
||||
but no single field on that sheet works as a key. Specifying "*" for the key
|
||||
produces a compound key using all non-excluded fields (and the installation
|
||||
date and file location are excluded).
|
||||
.Example
|
||||
Merge-MulipleSheets -Path hotfixes.xlsx -WorksheetName Serv* -Key hotfixid -OutputFile test2.xlsx -OutputSheetName hotfixes -HideRowNumbers -Show
|
||||
This time all the servers have written their hofix information to their own Worksheets in a shared Excel workbook named "Hotfixes"
|
||||
(the information was obtained by running Get-Hotfix | Sort-Object -Property description,hotfixid | Select-Object -Property Description,HotfixID)
|
||||
This ignores any sheets which are not named "Serv*", and uses the HotfixID as the key ; in this version the row numbers are hidden.
|
||||
This time all the servers have written their hotfix information to their own
|
||||
worksheets in a shared Excel workbook named "Hotfixes.xlsx" (the information was
|
||||
obtained by running Get-Hotfix | Sort-Object -Property description,hotfixid | Select-Object -Property Description,HotfixID)
|
||||
This ignores any sheets which are not named "Serv*", and uses the HotfixID as
|
||||
the key; in this version the row numbers are hidden.
|
||||
#>
|
||||
[cmdletbinding()]
|
||||
#[Alias("Merge-MulipleSheets")] #There was a spelling error in the first release. This was there to ensure things didn't break but intelisense gave the alias first.
|
||||
@@ -351,30 +378,30 @@ Function Merge-MultipleSheets {
|
||||
#Paths to the files to be merged.
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
|
||||
[string[]]$Path ,
|
||||
#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,
|
||||
|
||||
#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 column headers of the Start row.
|
||||
[String[]]$Headername,
|
||||
|
||||
#Automatically generate property names (P1, P2, P3, ..) instead of the using the values the top row of the sheet.
|
||||
#If specified, property names will be automatically generated (P1, P2, P3, ..) instead of using the values from the start row.
|
||||
[switch]$NoHeader,
|
||||
|
||||
#Name(s) of Worksheets to compare,
|
||||
#Name(s) of Worksheets to compare.
|
||||
$WorksheetName = "Sheet1",
|
||||
#File to write output to
|
||||
#File to write output to.
|
||||
[Alias('OutFile')]
|
||||
$OutputFile = ".\temp.xlsx",
|
||||
#Name of Worksheet to output - if none specified will use the reference Worksheet name.
|
||||
[Alias('OutSheet')]
|
||||
$OutputSheetName = "Sheet1",
|
||||
#Properties to include in the DIFF - supports wildcards, default is "*".
|
||||
#Properties to include in the comparison - supports wildcards, default is "*".
|
||||
$Property = "*" ,
|
||||
#Properties to exclude from the the search - supports wildcards.
|
||||
#Properties to exclude from the the comparison - supports wildcards.
|
||||
$ExcludeProperty ,
|
||||
#Name of a column which is unique used to pair up rows from the refence and difference side, default is "Name".
|
||||
#Name of a column which is unique used to pair up rows from the reference and difference sides, default is "Name".
|
||||
$Key = "Name" ,
|
||||
#Sets the font color for the "key" field; this means you can filter by color to get only changed rows.
|
||||
#Sets the font color for the Key field; this means you can filter by color to get only changed rows.
|
||||
[System.Drawing.Color]$KeyFontColor = [System.Drawing.Color]::Red,
|
||||
#Sets the background color for changed rows.
|
||||
[System.Drawing.Color]$ChangeBackgroundColor = [System.Drawing.Color]::Orange,
|
||||
@@ -382,9 +409,9 @@ Function Merge-MultipleSheets {
|
||||
[System.Drawing.Color]$DeleteBackgroundColor = [System.Drawing.Color]::LightPink,
|
||||
#Sets the background color for rows not in the reference but added to the difference sheet.
|
||||
[System.Drawing.Color]$AddBackgroundColor = [System.Drawing.Color]::Orange,
|
||||
#if Specified hides the columns in the spreadsheet that contain the row numbers
|
||||
#If specified, hides the columns in the spreadsheet that contain the row numbers.
|
||||
[switch]$HideRowNumbers ,
|
||||
#If specified outputs the data to the pipeline (you can add -whatif so it the command only outputs to the command)
|
||||
#If specified, outputs the data to the pipeline (you can add -whatif so it the command only outputs to the pipeline).
|
||||
[switch]$Passthru ,
|
||||
#If specified, opens the output workbook.
|
||||
[Switch]$Show
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
function New-ConditionalFormattingIconSet {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Creates an object which describes a conditional formatting rule a for 3,4 or 5 icon set
|
||||
Creates an object which describes a conditional formatting rule a for 3,4 or 5 icon set.
|
||||
.DESCRIPTION
|
||||
Export-Excel takes a -ConditionalFormat parameter which can hold one or more descriptions for conditional formats;
|
||||
this command builds the
|
||||
this command builds the defintion of a Conditional formatting rule for an icon set.
|
||||
.PARAMETER Range
|
||||
The range of cells that the conditional format applies to
|
||||
The range of cells that the conditional format applies to.
|
||||
.PARAMETER ConditionalFormat
|
||||
The type of rule: one of "ThreeIconSet","FourIconSet" or "FiveIconSet"
|
||||
.PARAMETER IconType
|
||||
The name of an iconSet - different icons are available depending on whether 3,4 or 5 icon set is selected
|
||||
The name of an iconSet - different icons are available depending on whether 3,4 or 5 icon set is selected.
|
||||
.PARAMETER Reverse
|
||||
Use the icons in the reverse order.
|
||||
.Example
|
||||
@@ -18,9 +18,11 @@ function New-ConditionalFormattingIconSet {
|
||||
$cfdef = New-ConditionalFormattingIconSet -Range $cfrange -ConditionalFormat ThreeIconSet -IconType Arrows
|
||||
Export-Excel -ExcelPackage $excel -ConditionalFormat $cfdef -show
|
||||
|
||||
The first line creates a range - one column wide in the column $column, running from $topRow to $lastDataRow.
|
||||
The second creates a definition object using this range
|
||||
and the third uses Export-Excel with an open package to apply the format and save and open the file.
|
||||
The first line creates a range - one column wide in the column $column, running
|
||||
from $topRow to $lastDataRow.
|
||||
The second line creates a definition object using this range
|
||||
and the third uses Export-Excel with an open package to apply the format and
|
||||
save and open the file.
|
||||
.Link
|
||||
Add-Add-ConditionalFormatting
|
||||
New-ConditionalText
|
||||
|
||||
@@ -5,7 +5,7 @@ function New-ConditionalText {
|
||||
.DESCRIPTION
|
||||
Some Conditional formatting rules don't apply styles to a cell (IconSets and Databars).
|
||||
Some take two parameters (Between).
|
||||
Some take none (ThisWeek , containsErrors, AboveAverage etc).
|
||||
Some take none (ThisWeek, ContainsErrors, AboveAverage etc).
|
||||
The others take a single parameter (Top, BottomPercent, GreaterThan, Contains etc).
|
||||
This command creates an object to describe the last two categories, which can then be passed to Export-Excel.
|
||||
.PARAMETER Range
|
||||
@@ -13,26 +13,31 @@ function New-ConditionalText {
|
||||
.PARAMETER ConditionalType
|
||||
One of the supported rules; by default "ContainsText" is selected.
|
||||
.PARAMETER Text
|
||||
The text (or other value) to use in the rule. Not that Equals, GreaterThan/LessThan rules require text to wrapped in double quotes.
|
||||
The text (or other value) to use in the rule. Note that Equals, GreaterThan/LessThan rules require text to wrapped in double quotes.
|
||||
.PARAMETER ConditionalTextColor
|
||||
The font color for the cell - by default: Dark red.
|
||||
The font color for the cell - by default: "DarkRed".
|
||||
.PARAMETER BackgroundColor
|
||||
The fill color for the cell - by default: Light pink.
|
||||
The fill color for the cell - by default: "LightPink".
|
||||
.PARAMETER PatternType
|
||||
The Background pattern for the cell - by default: Solid
|
||||
The background pattern for the cell - by default: "Solid"
|
||||
.EXAMPLE
|
||||
>
|
||||
$ct = New-ConditionalText -Text 'Ferrari'
|
||||
Export-Excel -ExcelPackage $excel -ConditionalTest $ct -show
|
||||
|
||||
The first line creates a definition object which will highlight the word "Ferrari" in any cell.
|
||||
and the second uses Export-Excel with an open package to apply the format and save and open the file.
|
||||
The first line creates a definition object which will highlight the word
|
||||
"Ferrari" in any cell. and the second uses Export-Excel with an open package
|
||||
to apply the format and save and open the file.
|
||||
.EXAMPLE
|
||||
>
|
||||
$ct = New-ConditionalText -Text "Ferrari"
|
||||
$ct2 = New-ConditionalText -Range $worksheet.Names["FinishPosition"].Address -ConditionalType LessThanOrEqual -Text 3 -ConditionalTextColor Red -BackgroundColor White
|
||||
Export-Excel -ExcelPackage $excel -ConditionalText $ct,$ct2 -show
|
||||
|
||||
This builds on the previous example, and specifies a condition of <=3 with a format of Red text on a white background; this applies to a named range "Finish Position"
|
||||
the range could be written "C:C" to specify a named column, or "C2:C102" to specify certain cells in the column.
|
||||
This builds on the previous example, and specifies a condition of <=3 with
|
||||
a format of red text on a white background; this applies to a named range
|
||||
"Finish Position". The range could be written "C:C" to specify a named
|
||||
column, or "C2:C102" to specify certain cells in the column.
|
||||
.Link
|
||||
Add-Add-ConditionalFormatting
|
||||
New-ConditionalFormattingIconSet
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
Creates a Definition of a chart which can be added using Export-Excel, or Add-PivotTable
|
||||
.DESCRIPTION
|
||||
All the parameters which are passed to Add-ExcelChart can be added to an object and
|
||||
passed to Export-Excel with the -ExcelChartDefinition parameter,
|
||||
or to Add-PivotTable with the -PivotChartDefinition parameter.
|
||||
passed to Export-Excel with the -ExcelChartDefinition parameter,
|
||||
or to Add-PivotTable with the -PivotChartDefinition parameter.
|
||||
This command sets up those definitions.
|
||||
.PARAMETER Title
|
||||
The title for the chart.
|
||||
@@ -20,23 +20,23 @@
|
||||
.PARAMETER YRange
|
||||
The range(s) of cells holding values for the Y-Axis - usually "data".
|
||||
.PARAMETER Width
|
||||
Width of the chart in Pixels. Defaults to 500.
|
||||
Width of the chart in pixels. Defaults to 500.
|
||||
.PARAMETER Height
|
||||
Height of the chart in Pixels. Defaults to 350.
|
||||
Height of the chart in pixels. Defaults to 350.
|
||||
.PARAMETER Row
|
||||
Row position of the top left corner of the chart. 0 places at the top of the sheet, 1 below row 1 and so on.
|
||||
.PARAMETER RowOffSetPixels
|
||||
Offset to position the chart by a fraction of of a row.
|
||||
Offset to position the chart by a fraction of a row.
|
||||
.PARAMETER Column
|
||||
Column position of the top left corner of the chart. 0 places at the edge of the sheet 1 to the right of column A and so on.
|
||||
.PARAMETER ColumnOffSetPixels
|
||||
Offset to position the chart by a fraction of of a column.
|
||||
Offset to position the chart by a fraction of a column.
|
||||
.PARAMETER NoLegend
|
||||
If specified, turns of display of the key. If you only have one data series it may be preferable to use the title to say what the chart is.
|
||||
If specified, turns off display of the key. If you only have one data series it may be preferable to use the title to say what the chart is.
|
||||
.PARAMETER SeriesHeader
|
||||
Specify explicit name(s) for the data series, which will appear in the legend/key
|
||||
Specifies explicit name(s) for the data series, which will appear in the legend/key
|
||||
.PARAMETER LegendPosition
|
||||
Location of the key, either left, right, top, bottom or TopRight.
|
||||
Location of the key, either "Left", "Right", "Top", "Bottom" or "TopRight".
|
||||
.PARAMETER LegendSize
|
||||
Font size for the key.
|
||||
.PARAMETER LegendBold
|
||||
@@ -62,7 +62,7 @@
|
||||
.PARAMETER XMinValue
|
||||
Minimum value for the scale along the X-axis.
|
||||
.PARAMETER xAxisPosition
|
||||
Postion for the X-axis (Top or Bottom).
|
||||
Position for the X-axis ("Top" or" Bottom").
|
||||
.PARAMETER YAxisTitleText
|
||||
Specifies a title for the Y-axis.
|
||||
.PARAMETER YAxisTitleBold
|
||||
@@ -80,7 +80,7 @@
|
||||
.PARAMETER YMinValue
|
||||
Minimum value on the Y-axis.
|
||||
.PARAMETER YAxisPosition
|
||||
Postion for the Y-axis (Left or Right).
|
||||
Position for the Y-axis ("Left" or "Right").
|
||||
.PARAMETER Header
|
||||
No longer used. This may be removed in future versions.
|
||||
.Example
|
||||
@@ -92,7 +92,8 @@
|
||||
|
||||
0..360 | ForEach-Object {[pscustomobject][ordered]@{x = $_; Sinx = "=Sin(Radians(x)) "}} | Export-Excel -AutoNameRange -now -WorkSheetname SinX -ExcelChartDefinition $cDef -Show
|
||||
|
||||
This reworks an example from Add-Excel-Chart but here the chart definition is defined and then it is used in a call to Export-Excel.
|
||||
This reworks an example from Add-Excel-Chart but here the chart is defined
|
||||
and the defintion stored in $cDef and then Export-Excel uses $cDef .
|
||||
#>
|
||||
[Alias("New-ExcelChart")] #This was the former name. The new name reflects that we are defining a chart, not making one in the workbook.
|
||||
[cmdletbinding()]
|
||||
@@ -312,15 +313,15 @@ function Add-ExcelChart {
|
||||
-SeriesHeader "Sin(x)" -LegendSize 8 -legendBold -LegendPosition Bottom
|
||||
Close-ExcelPackage $Excel -Show
|
||||
|
||||
The first line puts numbers from 0 to 360 into a sheet, as the first column, and
|
||||
The first line puts numbers from 0 to 360 into a sheet, as the first column, and
|
||||
a formula to calculate the Sine of that number of number of degrees in the second column.
|
||||
It creates named-ranges for the two columns - "X" and "SinX" respectively
|
||||
The Add-ExcelChart command adds a chart to that worksheet, specifying a line chart
|
||||
The Add-ExcelChart command adds a chart to that worksheet, specifying a line chart
|
||||
with the X values coming from named-range "X" and the Y values coming from named-range "SinX".
|
||||
The chart has a title, and is positioned to the right of column 2 and sized 800 pixels wide
|
||||
The X-axis is labelled "Degrees", in bold 12 point type and runs from 0 to 361 with labels every 30,
|
||||
The X-axis is labelled "Degrees", in bold 12 point type and runs from 0 to 361 with labels every 30,
|
||||
and minor tick marks every 10. Degrees are shown padded to 3 digits.
|
||||
The Y-axis is labelled "Sine" and to allow some room above and below its scale runs from -1.25 to 1.25,
|
||||
The Y-axis is labelled "Sine" and to allow some room above and below its scale runs from -1.25 to 1.25,
|
||||
and is marked off in units of 0.25 shown to two decimal places.
|
||||
The key will for the chart will be at the bottom in 8 point bold type and the line will be named "Sin(x)".
|
||||
#>
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
Function Open-ExcelPackage {
|
||||
<#
|
||||
.Synopsis
|
||||
Returns an Excel Package Object for the specified XLSX file
|
||||
Returns an ExcelPackage object for the specified XLSX fil.e
|
||||
.Description
|
||||
Import-Excel and Export-Excel open an Excel file, carry out their tasks and close it again.
|
||||
Sometimes it is necessary to open a file and do other work on it. Open-Excel package allows the file to be opened for these tasks.
|
||||
It takes a KillExcel switch to make sure Excel is not holding the file open; a password parameter for existing protected files,
|
||||
and a create switch to set-up a new file if no file already exists.
|
||||
Sometimes it is necessary to open a file and do other work on it.
|
||||
Open-ExcelPackage allows the file to be opened for these tasks.
|
||||
It takes a -KillExcel switch to make sure Excel is not holding the file open;
|
||||
a -Password parameter for existing protected files,
|
||||
and a -Create switch to set-up a new file if no file already exists.
|
||||
.Example
|
||||
>
|
||||
PS> $excel = Open-ExcelPackage -Path "$env:TEMP\test99.xlsx" -Create
|
||||
$ws = Add-WorkSheet -ExcelPackage $excel
|
||||
|
||||
This will create a new file in the temp folder if it doesn't already exist. It then adds a worksheet -
|
||||
because no name is specified it will use the default name of "Sheet1"
|
||||
This will create a new file in the temp folder if it doesn't already exist.
|
||||
It then adds a worksheet - because no name is specified it will use the
|
||||
default name of "Sheet1"
|
||||
.Example
|
||||
>
|
||||
PS> $excel = Open-ExcelPackage -path "$xlPath" -Password $password
|
||||
@@ -21,20 +24,21 @@
|
||||
Set-ExcelRange -Range $sheet1.Cells["E1:S1048576"], $sheet1.Cells["V1:V1048576"] -NFormat ([cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern)
|
||||
Close-ExcelPackage $excel -Show
|
||||
|
||||
This will open the password protected file at $xlPath using the password stored in $Password.
|
||||
Sheet1 is selected and formatting applied to two blocks of the sheet; then the file is and saved and loaded into Excel.
|
||||
This will open the password protected file at $xlPath using the password stored
|
||||
in $Password. Sheet1 is selected and formatting applied to two blocks of the sheet;
|
||||
then the file is and saved and loaded into Excel.
|
||||
#>
|
||||
[CmdLetBinding()]
|
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword","")]
|
||||
[OutputType([OfficeOpenXml.ExcelPackage])]
|
||||
Param (
|
||||
#The Path to the file to open
|
||||
#The path to the file to open.
|
||||
[Parameter(Mandatory=$true)]$Path,
|
||||
#If specified, any running instances of Excel will be terminated before opening the file.
|
||||
[switch]$KillExcel,
|
||||
#The password for a protected worksheet, as a [normal] string (not a secure string.)
|
||||
#The password for a protected worksheet, as a [normal] string (not a secure string).
|
||||
[String]$Password,
|
||||
#By default open only opens an existing file; -Create instructs it to create a new file if required.
|
||||
#By default Open-ExcelPackage will only opens an existing file; -Create instructs it to create a new file if required.
|
||||
[switch]$Create
|
||||
)
|
||||
|
||||
@@ -75,7 +79,7 @@ Function Close-ExcelPackage {
|
||||
.Description
|
||||
When working with an ExcelPackage object, the Workbook is held in memory and not saved until the .Save() method of the package is called.
|
||||
Close-ExcelPackage saves and disposes of the Package object. It can be called with -NoSave to abandon the file without saving, with a new "SaveAs" filename,
|
||||
and/or with a password to protect the file. And -Show will open the file in Excel;
|
||||
and/or with a password to protect the file. And -Show will open the file in Excel;
|
||||
-Calculate will try to update the workbook, although not everything can be recalculated
|
||||
.Example
|
||||
Close-ExcelPackage -show $excel
|
||||
|
||||
@@ -130,16 +130,27 @@
|
||||
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 management
|
||||
.EXAMPLE
|
||||
C:\> $SQL="SELECT top 25 Name,Length From TestData ORDER BY Length DESC"
|
||||
C:\> $Connection = ' Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\Users\James\Documents\Database1.accdb;'
|
||||
|
||||
C:\> Send-SQLDataToExcel -Connection $connection -SQL $sql -path .\demo1.xlsx -WorkSheetname "Sizes" -AutoSize
|
||||
|
||||
This declares a SQL statement and creates an ODBC connection string to read from an Access file and extracts data from it and sends it to a new worksheet
|
||||
|
||||
.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;'
|
||||
C:\> Send-SQLDataToExcel -Connection $connection -SQL $sql -path .\demo1.xlsx -WorkSheetname "Winners" -AutoSize -AutoNameRange
|
||||
C:\> $Connection = 'Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Dbq=C:\users\James\Documents\f1Results.xlsx;'
|
||||
|
||||
C:\> Send-SQLDataToExcel -Connection $connection -SQL $sql -path .\demo1.xlsx -WorkSheetname "Winners" -AutoSize -AutoNameRange -ConditionalFormat @{DataBarColor="Blue"; Range="Wins"}
|
||||
|
||||
This declares a SQL statement and creates an ODBC connection string to read from an Excel file, it then runs the statement and outputs the resulting data to a new spreadsheet.
|
||||
The spreadsheet is formatted and a data bar added to show make the drivers' wins clearer.
|
||||
(the F1 results database is available from https://1drv.ms/x/s!AhfYu7-CJv4ehNdZWxJE9LMAX_N5sg )
|
||||
.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:\> 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 "-" )
|
||||
|
||||
@@ -291,12 +291,15 @@ if (Get-Command -ErrorAction SilentlyContinue -name Register-ArgumentCompleter)
|
||||
Function Expand-NumberFormat {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Converts short names for Number formats to the formatting strings used in Excel
|
||||
Converts short names for number formats to the formatting strings used in Excel
|
||||
.DESCRIPTION
|
||||
Where you can type a number format you can write, for example 'Short-Date' and the module will translate it into the format string used by excel
|
||||
Some formats, like Short-Date change how they are presented when Excel loads. (So date will use the local ordering of year, month and Day)
|
||||
Other formats change how they appear when loaded with different cultures (depending on the country "," or "." or " " may be the thousand seperator
|
||||
although excel always stores it as ",")
|
||||
Where you can type a number format you can write, for example, 'Short-Date'
|
||||
and the module will translate it into the format string used by Excel.
|
||||
Some formats, like Short-Date change how they are presented when Excel
|
||||
loads (so date will use the local ordering of year, month and Day). Other
|
||||
formats change how they appear when loaded with different cultures
|
||||
(depending on the country "," or "." or " " may be the thousand seperator
|
||||
although Excel always stores it as ",")
|
||||
.EXAMPLE
|
||||
Expand-NumberFormat percentage
|
||||
|
||||
@@ -304,11 +307,15 @@ Function Expand-NumberFormat {
|
||||
.EXAMPLE
|
||||
Expand-NumberFormat Currency
|
||||
|
||||
Returns the currency format specified in the local regional settings. This may not be the same as Excel uses
|
||||
The regional settings set the currency symbol and then whether it is before or after the number and seperated with a space or not;
|
||||
for negative numbers the number by wrapped in parentheses or a - sign might appear before or after the number and symbol.
|
||||
So this returns $#,##0.00;($#,##0.00) for English US, #,##0.00 €;€#,##0.00- for French. (Note some Eurozone countries write €1,23 and others 1,23€ )
|
||||
In French the decimal point will be rendered as a "," and the thousand seperator as a space.
|
||||
Returns the currency format specified in the local regional settings. This
|
||||
may not be the same as Excel uses. The regional settings set the currency
|
||||
symbol and then whether it is before or after the number and separated with
|
||||
a space or not; for negative numbers the number may be wrapped in parentheses
|
||||
or a - sign might appear before or after the number and symbol.
|
||||
So this returns $#,##0.00;($#,##0.00) for English US, #,##0.00 €;€#,##0.00-
|
||||
for French. (Note some Eurozone countries write €1,23 and others 1,23€ )
|
||||
In French the decimal point will be rendered as a "," and the thousand
|
||||
separator as a space.
|
||||
#>
|
||||
[cmdletbinding()]
|
||||
[OutputType([String])]
|
||||
|
||||
@@ -3,29 +3,37 @@
|
||||
.Synopsis
|
||||
Compares two worksheets and shows the differences.
|
||||
.Description
|
||||
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 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.
|
||||
This command takes two file names, one or two worksheet names and a name
|
||||
for a "key" column. It reads the worksheet from each file and decides the
|
||||
column names and 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
|
||||
(explicitly checkingall 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 different positions in the file will not be considered a 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.
|
||||
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 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 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
|
||||
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
|
||||
This time two workbooks contain the result of redirecting the command
|
||||
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
|
||||
|
||||
@@ -33,25 +41,32 @@
|
||||
.Example
|
||||
Compare-WorkSheet 'Server54.xlsx' 'Server55.xlsx' -WorkSheetName Services -BackgroundColor lightGreen -FontColor Red -Show
|
||||
|
||||
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.
|
||||
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 that a limited set of columns should be used.
|
||||
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 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 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.
|
||||
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
|
||||
told 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 are interested in those 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 highlights 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(
|
||||
@@ -63,9 +78,9 @@
|
||||
$Differencefile ,
|
||||
#Name(s) of worksheets to compare.
|
||||
$WorkSheetName = "Sheet1",
|
||||
#Properties to include in the DIFF - supports wildcards, default is "*".
|
||||
#Properties to include in the comparison - supports wildcards, default is "*".
|
||||
$Property = "*" ,
|
||||
#Properties to exclude from the search - supports wildcards.
|
||||
#Properties to exclude from the comparison - supports wildcards.
|
||||
$ExcludeProperty ,
|
||||
#Specifies custom property names to use, instead of the values defined in the starting row of the sheet.
|
||||
[Parameter(ParameterSetName='B', Mandatory)]
|
||||
@@ -75,25 +90,25 @@
|
||||
[switch]$NoHeader,
|
||||
#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.
|
||||
#If specified, highlights all the cells - so you can make Equal cells one color, and Different cells another.
|
||||
[System.Drawing.Color]$AllDataBackgroundColor,
|
||||
#If specified, highlights the DIFF rows.
|
||||
#If specified, highlights the rows with differences.
|
||||
[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 difference 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, 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 Grid-View 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 a full set of diff data is returned without filtering to the specified properties.
|
||||
#If specifieda 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
|
||||
)
|
||||
|
||||
@@ -174,7 +189,7 @@
|
||||
$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 font color was specified, set it on changed properties where the same key appears in both sheets.
|
||||
if ($diff -and $FontColor -and ($propList -contains $Key) ) {
|
||||
$updates = $diff.where({$_.SideIndicator -ne "=="}) | Group-object -Property $Key | Where-Object {$_.count -eq 2}
|
||||
if ($updates) {
|
||||
|
||||
Reference in New Issue
Block a user