From bfbba90c4442f988a8c0544d55e97864a461fbf8 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Tue, 10 Jul 2018 13:43:15 +0100 Subject: [PATCH] Updates to Help text - mostly layout, inc making it VSCode freindly --- AddConditionalFormatting.ps1 | 6 +- ConvertExcelToImageFile.ps1 | 98 ++++++++++++++++---------------- ConvertFromExcelData.ps1 | 32 +++++------ ConvertFromExcelToSQLInsert.ps1 | 46 +++++---------- Export-charts.ps1 | 4 +- Get-ExcelSheetInfo.ps1 | 16 ++---- Get-ExcelWorkbookInfo.ps1 | 15 ++--- Set-Column.ps1 | 24 ++++---- Set-Row.ps1 | 39 +++++++------ Update-FirstObjectProperties.ps1 | 3 +- formatting.ps1 | 50 ++++++++-------- 11 files changed, 155 insertions(+), 178 deletions(-) diff --git a/AddConditionalFormatting.ps1 b/AddConditionalFormatting.ps1 index 6d35dbd..db18fc6 100644 --- a/AddConditionalFormatting.ps1 +++ b/AddConditionalFormatting.ps1 @@ -32,14 +32,14 @@ [Parameter(Mandatory = $true, ParameterSetName = "FourIconSet")] [Parameter(Mandatory = $true, ParameterSetName = "FiveIconSet")] [OfficeOpenXml.ExcelAddress]$Range , - #One or more row(s), Column(s) and/or block(s) of cells to format + #One or more row(s), column(s) and/or block(s) of cells to format [Parameter(Mandatory = $true, ParameterSetName = "NamedRuleAddress")] [Parameter(Mandatory = $true, ParameterSetName = "DataBarAddress")] [Parameter(Mandatory = $true, ParameterSetName = "ThreeIconSetAddress")] [Parameter(Mandatory = $true, ParameterSetName = "FourIconSetAddress")] [Parameter(Mandatory = $true, ParameterSetName = "FiveIconSetAddress")] $Address , - #One of the standard named rules - Top / Bottom / Less than / Greater than / Contains etc + #One of the standard named rules - Top / Bottom / Less than / Greater than / Contains etc. [Parameter(Mandatory = $true, ParameterSetName = "NamedRule", Position = 3)] [Parameter(Mandatory = $true, ParameterSetName = "NamedRuleAddress", Position = 3)] [OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType]$RuleType , @@ -65,7 +65,7 @@ [OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting5IconsSetType]$FiveIconsSet, #A value for the condition (e.g. "2000" if the test is 'lessthan 2000') [string]$ConditionValue, - #A second value for the conditions like between x and Y + #A second value for the conditions like "between x and Y" [string]$ConditionValue2, #Background colour for matching items [System.Drawing.Color]$BackgroundColor, diff --git a/ConvertExcelToImageFile.ps1 b/ConvertExcelToImageFile.ps1 index 6381759..c89f069 100644 --- a/ConvertExcelToImageFile.ps1 +++ b/ConvertExcelToImageFile.ps1 @@ -1,53 +1,53 @@ Function Convert-XlRangeToImage { -<# - .Synopsis - Gets the specified part of an Excel file and exports it as an image - .Description - Excel allows charts to be exported directly to a file, but can't do this with the rest of a sheet. To work round this this function - * Opens a copy of Excel and loads a file - * Selects a worksheet and then a range of cells in that worksheet - * Copies the select to the clipboard - * Saves the clipboard contents as an image file (it will save as .JPG unless the file name ends .BMP or .PNG) - * Copies a single cell to the clipboard (to prevent the "you have put a lot in the clipboard" message appearing) - * Closes Excel -#> -Param ( - #Path to the Excel file - [parameter(Mandatory=$true)] - $Path, - #Worksheet name - if none is specified "Sheet1" will be assumed - $workSheetname = "Sheet1" , - #Range of cells within the sheet, e.g "A1:Z99" - [parameter(Mandatory=$true)] - $range, - #A bmp, png or jpg file where the result will be saved - $destination = "$pwd\temp.png", - #If specified opens the image in the default viewer. - [switch]$show -) - $extension = $destination -replace '^.*\.(\w+)$' ,'$1' - if ($extension -in @('JPEG','BMP','PNG')) { - $Format = [system.Drawing.Imaging.ImageFormat]$extension - } #if we don't recognise the extension OR if it is JPG with an E, use JPEG format - else { $Format = [system.Drawing.Imaging.ImageFormat]::Jpeg} - Write-Progress -Activity "Exporting $range of $workSheetname in $Path" -Status "Starting Excel" - $xlApp = New-Object -ComObject "Excel.Application" - Write-Progress -Activity "Exporting $range of $workSheetname in $Path" -Status "Opening Workbook and copying data" - $xlWbk = $xlApp.Workbooks.Open($Path) - $xlWbk.Worksheets($workSheetname).Select() - $xlWbk.ActiveSheet.Range($range).Select() | Out-Null - $xlApp.Selection.Copy() | Out-Null - Write-Progress -Activity "Exporting $range of $workSheetname in $Path" -Status "Saving copied data" - # Get-Clipboard came in with PS5. Older versions can use [System.Windows.Clipboard] but it is ugly. - $image = Get-Clipboard -Format Image - $image.Save($destination, $Format) - Write-Progress -Activity "Exporting $range of $workSheetname in $Path" -Status "Closing Excel" - $xlWbk.ActiveSheet.Range("a1").Select() | Out-Null - $xlApp.Selection.Copy() | Out-Null - $xlApp.Quit() - Write-Progress -Activity "Exporting $range of $workSheetname in $Path" -Completed - if ($show) {Start-Process -FilePath $destination} - else {Get-Item -Path $destination} + <# + .Synopsis + Gets the specified part of an Excel file and exports it as an image + .Description + Excel allows charts to be exported directly to a file, but can't do this with the rest of a sheet. To work round this this function + * Opens a copy of Excel and loads a file + * Selects a worksheet and then a range of cells in that worksheet + * Copies the select to the clipboard + * Saves the clipboard contents as an image file (it will save as .JPG unless the file name ends .BMP or .PNG) + * Copies a single cell to the clipboard (to prevent the "you have put a lot in the clipboard" message appearing) + * Closes Excel + #> + Param ( + #Path to the Excel file + [parameter(Mandatory=$true)] + $Path, + #Worksheet name - if none is specified "Sheet1" will be assumed + $workSheetname = "Sheet1" , + #Range of cells within the sheet, e.g "A1:Z99" + [parameter(Mandatory=$true)] + $range, + #A bmp, png or jpg file where the result will be saved + $destination = "$pwd\temp.png", + #If specified opens the image in the default viewer. + [switch]$show + ) + $extension = $destination -replace '^.*\.(\w+)$' ,'$1' + if ($extension -in @('JPEG','BMP','PNG')) { + $Format = [system.Drawing.Imaging.ImageFormat]$extension + } #if we don't recognise the extension OR if it is JPG with an E, use JPEG format + else { $Format = [system.Drawing.Imaging.ImageFormat]::Jpeg} + Write-Progress -Activity "Exporting $range of $workSheetname in $Path" -Status "Starting Excel" + $xlApp = New-Object -ComObject "Excel.Application" + Write-Progress -Activity "Exporting $range of $workSheetname in $Path" -Status "Opening Workbook and copying data" + $xlWbk = $xlApp.Workbooks.Open($Path) + $xlWbk.Worksheets($workSheetname).Select() + $xlWbk.ActiveSheet.Range($range).Select() | Out-Null + $xlApp.Selection.Copy() | Out-Null + Write-Progress -Activity "Exporting $range of $workSheetname in $Path" -Status "Saving copied data" + # Get-Clipboard came in with PS5. Older versions can use [System.Windows.Clipboard] but it is ugly. + $image = Get-Clipboard -Format Image + $image.Save($destination, $Format) + Write-Progress -Activity "Exporting $range of $workSheetname in $Path" -Status "Closing Excel" + $xlWbk.ActiveSheet.Range("a1").Select() | Out-Null + $xlApp.Selection.Copy() | Out-Null + $xlApp.Quit() + Write-Progress -Activity "Exporting $range of $workSheetname in $Path" -Completed + if ($show) {Start-Process -FilePath $destination} + else {Get-Item -Path $destination} } <# del demo*.xlsx diff --git a/ConvertFromExcelData.ps1 b/ConvertFromExcelData.ps1 index bd75895..3421bc1 100644 --- a/ConvertFromExcelData.ps1 +++ b/ConvertFromExcelData.ps1 @@ -1,25 +1,25 @@ function ConvertFrom-ExcelData { <# - .SYNOPSIS - Reads data from a sheet, and for each row, calls a custom scriptblock with a list of property names and the row of data. + .SYNOPSIS + Reads data from a sheet, and for each row, calls a custom scriptblock with a list of property names and the row of data. - - .EXAMPLE - ConvertFrom-ExcelData .\testSQLGen.xlsx { - param($propertyNames, $record) + + .EXAMPLE + ConvertFrom-ExcelData .\testSQLGen.xlsx { + param($propertyNames, $record) - $reportRecord = @() - foreach ($pn in $propertyNames) { - $reportRecord += "{0}: {1}" -f $pn, $record.$pn + $reportRecord = @() + foreach ($pn in $propertyNames) { + $reportRecord += "{0}: {1}" -f $pn, $record.$pn + } + $reportRecord +="" + $reportRecord -join "`r`n" } - $reportRecord +="" - $reportRecord -join "`r`n" -} -First: John -Last: Doe -The Zip: 12345 -.... + First: John + Last: Doe + The Zip: 12345 + .... #> param( [Alias("FullName")] diff --git a/ConvertFromExcelToSQLInsert.ps1 b/ConvertFromExcelToSQLInsert.ps1 index 0f4708a..7a3f00c 100644 --- a/ConvertFromExcelToSQLInsert.ps1 +++ b/ConvertFromExcelToSQLInsert.ps1 @@ -1,51 +1,34 @@ function ConvertFrom-ExcelToSQLInsert { <# - .SYNOPSIS + .SYNOPSIS Generate SQL insert statements from Excel spreadsheet. - - .DESCRIPTION + .DESCRIPTION Generate SQL insert statements from Excel spreadsheet. - - .PARAMETER TableName + .PARAMETER TableName Name of the target database table. - - .PARAMETER Path + .PARAMETER Path Path to an existing .XLSX file - This parameter is passed to Import-Excel as is. - - .PARAMETER WorkSheetname + .PARAMETER WorkSheetname Specifies the name of the worksheet in the Excel workbook to import. By default, if no name is provided, the first worksheet will be imported. - This parameter is passed to Import-Excel as is. - - .PARAMETER StartRow + .PARAMETER StartRow The row from where we start to import data, all rows above the StartRow are disregarded. By default this is the first row. - When the parameters ‘-NoHeader’ and ‘-HeaderName’ are not provided, this row will contain the column headers that will be used as property names. When one of both parameters are provided, the property names are automatically created and this row will be treated as a regular row containing data. - - .PARAMETER Header + .PARAMETER Header Specifies custom property names to use, instead of the values defined in the column headers of the TopRow. - - In case you provide less header names than there is data in the worksheet, then only the data with a corresponding header name will be imported and the data without header name will be disregarded. - - In case you provide more header names than there is data in the worksheet, then all data will be imported and all objects will have all the property names you defined in the header names. As such, the last properties will be blanc as there is no data for them. - - .PARAMETER NoHeader + If you provide fewr header names than there is data in the worksheet, then only the data with a corresponding header name will be imported and the data without header name will be disregarded. + If you provide more header names than there is data in the worksheet, then all data will be imported and all objects will have all the property names you defined in the header names. As such, the last properties will be blank as there is no data for them. + .PARAMETER NoHeader Automatically generate property names (P1, P2, P3, ..) instead of the ones defined in the column headers of the TopRow. - This switch is best used when you want to import the complete worksheet ‘as is’ and are not concerned with the property names. - - .PARAMETER DataOnly + .PARAMETER DataOnly Import only rows and columns that contain data, empty rows and empty columns are not imported. - - - .PARAMETER ConvertEmptyStringsToNull + .PARAMETER ConvertEmptyStringsToNull If specified, cells without any data are replaced with NULL, instead of an empty string. - This is to address behviors in certain DBMS where an empty string is insert as 0 for INT column, instead of a NULL value. - .EXAMPLE + .EXAMPLE Generate SQL insert statements from Movies.xlsx file, leaving blank cells as empty strings: ---------------------------------------------------------- @@ -65,7 +48,7 @@ function ConvertFrom-ExcelToSQLInsert { INSERT INTO Movies ('Movie Name', 'Year', 'Rating') Values('Skyfall', '2012', '9'); INSERT INTO Movies ('Movie Name', 'Year', 'Rating') Values('The Avengers', '2012', ''); - .EXAMPLE + .EXAMPLE Generate SQL insert statements from Movies.xlsx file, specify NULL instead of an empty string. ---------------------------------------------------------- @@ -85,7 +68,6 @@ function ConvertFrom-ExcelToSQLInsert { INSERT INTO Movies ('Movie Name', 'Year', 'Rating') Values('Skyfall', '2012', '9'); INSERT INTO Movies ('Movie Name', 'Year', 'Rating') Values('The Avengers', '2012', NULL); - .NOTES #> [CmdletBinding()] param( diff --git a/Export-charts.ps1 b/Export-charts.ps1 index 27ec7ab..e51d894 100644 --- a/Export-charts.ps1 +++ b/Export-charts.ps1 @@ -2,7 +2,7 @@ .Synopsis Exports the charts in an Excel spreadSheet .Example - Export-Charts .\test,xlsx + Export-Charts .\test.xlsx Exports the charts in test.xlsx to JPEG files in the current directory. .Example @@ -13,7 +13,7 @@ Param ( #Path to the Excel file whose chars we will export. $Path = "C:\Users\public\Documents\stats.xlsx", - #If specified, output file objects representing the image files. + #If specified, output file objects representing the image files [switch]$Passthru, #Format to write - JPG by default [ValidateSet("JPG","PNG","GIF")] diff --git a/Get-ExcelSheetInfo.ps1 b/Get-ExcelSheetInfo.ps1 index 6ab93f5..bb00ee3 100644 --- a/Get-ExcelSheetInfo.ps1 +++ b/Get-ExcelSheetInfo.ps1 @@ -1,24 +1,20 @@ Function Get-ExcelSheetInfo { <# - .SYNOPSIS + .SYNOPSIS Get worksheet names and their indices of an Excel workbook. - - .DESCRIPTION + .DESCRIPTION The Get-ExcelSheetInfo cmdlet gets worksheet names and their indices of an Excel workbook. - - .PARAMETER Path + .PARAMETER Path Specifies the path to the Excel file. This parameter is required. - - .EXAMPLE + .EXAMPLE Get-ExcelSheetInfo .\Test.xlsx - .NOTES + .NOTES CHANGELOG 2016/01/07 Added Created by Johan Akerstrom (https://github.com/CosmosKey) - .LINK + .LINK https://github.com/dfinke/ImportExcel - #> [CmdletBinding()] diff --git a/Get-ExcelWorkbookInfo.ps1 b/Get-ExcelWorkbookInfo.ps1 index acd2e92..564e139 100644 --- a/Get-ExcelWorkbookInfo.ps1 +++ b/Get-ExcelWorkbookInfo.ps1 @@ -1,15 +1,12 @@ Function Get-ExcelWorkbookInfo { <# - .SYNOPSIS + .SYNOPSIS Retrieve information of an Excel workbook. - - .DESCRIPTION + .DESCRIPTION The Get-ExcelWorkbookInfo cmdlet retrieves information (LastModifiedBy, LastPrinted, Created, Modified, ...) fron an Excel workbook. These are the same details that are visible in Windows Explorer when right clicking the Excel file, selecting Properties and check the Details tabpage. - - .PARAMETER Path + .PARAMETER Path Specifies the path to the Excel file. This parameter is required. - - .EXAMPLE + .EXAMPLE Get-ExcelWorkbookInfo .\Test.xlsx CorePropertiesXml : #document @@ -32,11 +29,11 @@ Modified : 10/02/2017 12:45:37 CustomPropertiesXml : #document - .NOTES + .NOTES CHANGELOG 2016/01/07 Added Created by Johan Akerstrom (https://github.com/CosmosKey) - .LINK + .LINK https://github.com/dfinke/ImportExcel #> diff --git a/Set-Column.ps1 b/Set-Column.ps1 index 5246a6a..02e3081 100644 --- a/Set-Column.ps1 +++ b/Set-Column.ps1 @@ -1,31 +1,32 @@ Function Set-Column { -<# - .SYNOPSIS + <# + .SYNOPSIS Adds a column to the existing data area in an Excel sheet, fills values and sets formatting - .DESCRIPTION + .DESCRIPTION Set-Column takes a value which is either string containing a value or formula or a scriptblock which evaluates to a string, and optionally a column number and fills that value down the column. A column name can be specified and the new column can be made a named range. The column can be formatted. - .Example + .Example C:> Set-Column -Worksheet $ws -Heading "WinsToFastLaps" -Value {"=E$row/C$row"} -Column 7 -AutoSize -AutoNameRange Here $WS already contains a worksheet which contains counts of races won and fastest laps recorded by racing drivers (in columns C and E) Set-Column specifies that Column 7 should have a heading of "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3 - the data celss should become a named range, which will also be "WinsToFastLaps" the column width will be set automatically - -#> -[cmdletbinding()] + the data cells should become a named range, which will also be "WinsToFastLaps" the column width will be set automatically + #> + [cmdletbinding()] Param ( [Parameter(ParameterSetName="Package",Mandatory=$true)] [OfficeOpenXml.ExcelPackage]$ExcelPackage, - #Sheet to update + #The sheet to update can be a given as a name or an Excel Worksheet object - this sets it by name [Parameter(ParameterSetName="Package")] + #The sheet to update can be a given as a name or an Excel Worksheet object - $workSheet contains the object $Worksheetname = "Sheet1", [Parameter(ParameterSetName="sheet",Mandatory=$true)] [OfficeOpenXml.ExcelWorksheet] $Worksheet, #Column to fill down - first column is 1. 0 will be interpreted as first unused column $Column = 0 , + #First row to fill data in [Int]$StartRow , #value, formula or script block for to fill in. Script block can use $row, $column [number], $ColumnName [letter(s)], $startRow, $startColumn, $endRow, $endColumn [parameter(Mandatory=$true)] @@ -76,8 +77,9 @@ [Switch]$AutoSize, #Set cells to a fixed width, ignored if Autosize is specified [float]$Width, - #Set the inserted data to be a named range (ignored if header is not specified) d + #Set the inserted data to be a named range (ignored if header is not specified) [Switch]$AutoNameRange, + #If Specified, return an ExcelPackage object to allow further work to be done on the file. [switch]$PassThru ) #if we were passed a package object and a worksheet name , get the worksheet. @@ -125,7 +127,7 @@ if ($HorizontalAlignment) { $Worksheet.Column( $Column).Style.HorizontalAlignment = $HorizontalAlignment} if ($VerticalAlignment) { $Worksheet.Column( $Column).Style.VerticalAlignment = $VerticalAlignment } if ($FontColor) { $Worksheet.Column( $Column).Style.Font.Color.SetColor( $FontColor ) } - if ($BorderAround) { $Worksheet.Column( $Column).Style.Border.BorderAround( $BorderAround ) } + if ($BorderAround) { $Worksheet.Column( $Column).Style.Border.BorderAround( $BorderAround ) } if ($BackgroundColor) { $Worksheet.Column( $Column).Style.Fill.PatternType = $BackgroundPattern $Worksheet.Column( $Column).Style.Fill.BackgroundColor.SetColor($BackgroundColor ) diff --git a/Set-Row.ps1 b/Set-Row.ps1 index 9250c1a..d378763 100644 --- a/Set-Row.ps1 +++ b/Set-Row.ps1 @@ -1,24 +1,22 @@ Function Set-Row { -<# -.Synopsis - Fills values into a row in a Excel spreadsheet -.Description - Set-Row accepts either a Worksheet object or an Excel package object returned by Export-Excel and the name of a sheet, - and inserts the chosen contents into a row of the sheet. - The contents can be a constant "42" , a formula or a script block which is converted into a constant or formula. - The first cell of the row can optional be given a heading. -.Example - Set-row -Worksheet $ws -Heading Total -Value {"=sum($columnName`2:$columnName$endrow)" } + <# + .Synopsis + Fills values into a row in a Excel spreadsheet + .Description + Set-Row accepts either a Worksheet object or an Excel package object returned by Export-Excel and the name of a sheet, + and inserts the chosen contents into a row of the sheet. + The contents can be a constant "42" , a formula or a script block which is converted into a constant or formula. + The first cell of the row can optional be given a heading. + .Example + Set-row -Worksheet $ws -Heading Total -Value {"=sum($columnName`2:$columnName$endrow)" } - $Ws contains a worksheet object, and no Row number is specified so Set-Row will select the next row after the end of the data in the sheet - The first cell will contain "Total", and each other cell will contain - =Sum(xx2:xx99) - where xx is the column name, and 99 is the last row of data. - Note the use of `2 to Prevent 2 becoming part of the variable "ColumnName" - The script block can use $row, $column, $ColumnName, $startRow/Column $endRow/Column - - -#> -[cmdletbinding()] + $Ws contains a worksheet object, and no Row number is specified so Set-Row will select the next row after the end of the data in the sheet + The first cell will contain "Total", and each other cell will contain + =Sum(xx2:xx99) - where xx is the column name, and 99 is the last row of data. + Note the use of `2 to Prevent 2 becoming part of the variable "ColumnName" + The script block can use $row, $column, $ColumnName, $startRow/Column $endRow/Column + #> + [cmdletbinding()] Param ( #An Excel package object - e.g. from Export-Excel -passthru - requires a sheet name [Parameter(ParameterSetName="Package",Mandatory=$true)] @@ -75,11 +73,12 @@ [OfficeOpenXml.Style.ExcelHorizontalAlignment]$HorizontalAlignment, #Position cell contents to top bottom or centre [OfficeOpenXml.Style.ExcelVerticalAlignment]$VerticalAlignment, - #Degrees to rotate text. Up to +90 for anti-clockwise ("upwards"), or to -90 for clockwise. + #Degrees to rotate text. Up to +90 for anti-clockwise ("upwards"), or to -90 for clockwise [ValidateRange(-90, 90)] [int]$TextRotation , #Set cells to a fixed hieght [float]$Height, + #If Specified, return an ExcelPackage object to allow further work to be done on the file [switch]$PassThru ) diff --git a/Update-FirstObjectProperties.ps1 b/Update-FirstObjectProperties.ps1 index 6716a59..f19935b 100644 --- a/Update-FirstObjectProperties.ps1 +++ b/Update-FirstObjectProperties.ps1 @@ -72,7 +72,8 @@ Function Update-FirstObjectProperties { .NOTES CHANGELOG - 2017/06/08 Function born #> + 2017/06/08 Function born + #> Try { $Union = @() diff --git a/formatting.ps1 b/formatting.ps1 index 5d6d26a..f54c27c 100644 --- a/formatting.ps1 +++ b/formatting.ps1 @@ -1,22 +1,22 @@ Function Add-ConditionalFormatting { -<# -.Synopsis - Adds contitional formatting to worksheet -.Example - $excel = $avdata | Export-Excel -Path (Join-path $FilePath "\Machines.XLSX" ) -WorksheetName "Server Anti-Virus" -AutoSize -FreezeTopRow -AutoFilter -PassThru + <# + .Synopsis + Adds contitional formatting to worksheet + .Example + $excel = $avdata | Export-Excel -Path (Join-path $FilePath "\Machines.XLSX" ) -WorksheetName "Server Anti-Virus" -AutoSize -FreezeTopRow -AutoFilter -PassThru - Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "b":b1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "2003" - Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "i2:i1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "Disabled" - $excel.Workbook.Worksheets[1].Cells["D1:G1048576"].Style.Numberformat.Format = [cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern - $excel.Workbook.Worksheets[1].Row(1).style.font.bold = $true - $excel.Save() ; $excel.Dispose() + Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "b":b1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "2003" + Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "i2:i1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "Disabled" + $excel.Workbook.Worksheets[1].Cells["D1:G1048576"].Style.Numberformat.Format = [cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern + $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 is stored in $Excel - The desired worksheet is selected and the then columns B and i are conditially formatted (excluding the top row) to show - Fixed formats are then applied to dates in columns D..G and the top row is formatted - Finally the workbook is saved and the Excel closed. + Here Export-Excel is called with the -passThru parameter so the Excel Package object is stored in $Excel + The desired worksheet is selected and the then columns B and i are conditially formatted (excluding the top row) to show + Fixed formats are then applied to dates in columns D..G and the top row is formatted + Finally the workbook is saved and the Excel closed. -#> + #> Param ( #The worksheet where the format is to be applied [OfficeOpenXml.ExcelWorksheet]$WorkSheet , @@ -89,17 +89,17 @@ } Function Set-Format { -<# -.SYNOPSIS - Applies Number, font, alignment and colour formatting to a range of Excel Cells -.EXAMPLE - $sheet.Column(3) | Set-Format -HorizontalAlignment Right -NumberFormat "#,###" - Selects column 3 from a sheet object (within a workbook object, which is a child of the ExcelPackage object) and passes it to Set-Format which formats as an integer with comma seperated groups -.EXAMPLE - Set-Format -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NumberFormat "#,###" - Instead of piping the address in this version specifies a block of cells and applies similar formatting + <# + .SYNOPSIS + Applies Number, font, alignment and colour formatting to a range of Excel Cells + .EXAMPLE + $sheet.Column(3) | Set-Format -HorizontalAlignment Right -NumberFormat "#,###" + Selects column 3 from a sheet object (within a workbook object, which is a child of the ExcelPackage object) and passes it to Set-Format which formats as an integer with comma seperated groups + .EXAMPLE + Set-Format -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NumberFormat "#,###" + Instead of piping the address in this version specifies a block of cells and applies similar formatting -#> + #> Param ( #One or more row(s), Column(s) and/or block(s) of cells to format [Parameter(ValueFromPipeline=$true)]