Append, and formatting

Changed Export-Excel.ps1

#1 @ Line 197 Made new parameter sets . Default, and table already
existed and use path. Added DefaultPackage and TablePackage
A New parameter  "Package" allows an ExcelPackage object returned by
-passThru to be passed in
~Line 400 code to use package or path depending on path passed.

(also added Open-ExcelPackage  to get the object without exporting and
Close-ExcelPackage to close it nicely - these are in their own file)

#2.  @ Line 256 added new parameter excludeProperty to remove unwanted
properties without needing to go through select-object
~Line 459 added logic to exclude the properties specified in the new
parameter

#3 .  @ Line 262 Added new parameter Append
~Line 420 code to read the existing headers and move the insertion point
below the current data
(normal behaviour is to check if headers exist when adding data in the
process block, which makes this change wonderfully easy)
~Line 510 changed basis for identifying named ranges and changed scope
for rangeName so it can be used on other sheets

#4.  ~Line 550. Remove any existing Pivot table before trying to
[re]create it.

Added formatting.ps1 which applies conditional and normal formats -
requires an ExcelPackage to be open.

Added Open-ExcelPackage.ps1 (which contains a close function as well to
get the the object and save it )  open allows the sheet to be loaded
into a package object without needing to export .

Updated .psm1 to add the formating and open/close ps1 files.
This commit is contained in:
jhoneill
2017-10-21 18:14:02 +01:00
parent 954ed8d736
commit bd40cfe829
4 changed files with 328 additions and 35 deletions

View File

@@ -1,4 +1,4 @@
Add-Type -Path "$($PSScriptRoot)\EPPlus.dll"
Add-Type -Path "$($PSScriptRoot)\EPPlus.dll"
. $PSScriptRoot\Charting.ps1
. $PSScriptRoot\ConvertFromExcelData.ps1
@@ -7,6 +7,7 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll"
. $PSScriptRoot\Copy-ExcelWorkSheet.ps1
. $PSScriptRoot\Export-Excel.ps1
. $PSScriptRoot\Export-ExcelSheet.ps1
. $PSScriptRoot\Formatting.ps1
. $PSScriptRoot\Get-ExcelColumnName.ps1
. $PSScriptRoot\Get-ExcelSheetInfo.ps1
. $PSScriptRoot\Get-ExcelWorkbookInfo.ps1
@@ -20,16 +21,20 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll"
. $PSScriptRoot\New-ConditionalText.ps1
. $PSScriptRoot\New-ExcelChart.ps1
. $PSScriptRoot\New-PSItem.ps1
. $PSScriptRoot\Open-ExcelPackage.ps1
. $PSScriptRoot\Pivot.ps1
. $PSScriptRoot\Set-CellStyle.ps1
. $PSScriptRoot\TrackingUtils.ps1
. $PSScriptRoot\Update-FirstObjectProperties.ps1
if ($PSVersionTable.PSVersion.Major -ge 5) {
. $PSScriptRoot\Plot.ps1
Function New-Plot {
[OutputType([PSPlot])]
Param()
[PSPlot]::new()
}
@@ -46,11 +51,11 @@ Function Import-Excel {
Create custom objects from the rows in an Excel worksheet.
.DESCRIPTION
The Import-Excel cmdlet creates custom objects from the rows in an Excel worksheet. Each row represents one object. All of this is possible without installing Microsoft Excel and by using the .NET library <EFBFBD>EPPLus.dll<EFBFBD>.
The Import-Excel cmdlet creates custom objects from the rows in an Excel worksheet. Each row represents one object. All of this is possible without installing Microsoft Excel and by using the .NET library EPPLus.dll.
By default, the property names of the objects are retrieved from the column headers. Because an object cannot have a blanc property name, only columns with column headers will be imported.
If the default behavior is not desired and you want to import the complete worksheet <EFBFBD>as is<EFBFBD>, the parameter <EFBFBD>-NoHeader<EFBFBD> can be used. In case you want to provide your own property names, you can use the parameter <EFBFBD>-HeaderName<EFBFBD>.
If the default behavior is not desired and you want to import the complete worksheet as is, the parameter -NoHeader can be used. In case you want to provide your own property names, you can use the parameter -HeaderName.
.PARAMETER Path
Specifies the path to the Excel file.
@@ -71,18 +76,18 @@ Function Import-Excel {
.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 <EFBFBD>as is<EFBFBD> and are not concerned with the property names.
This switch is best used when you want to import the complete worksheet as is and are not concerned with the property names.
.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 <EFBFBD>-NoHeader<EFBFBD> and <EFBFBD>-HeaderName<EFBFBD> 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.
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 Password
Accepts a string that will be used to open a password protected Excel file.
.EXAMPLE
Import data from an Excel worksheet. One object is created for each row. The property names of the objects consist of the column names defined in the first row. In case a column doesn<EFBFBD>t have a column header (usually in row 1 when <EFBFBD>-StartRow<EFBFBD> is not used), then the unnamed columns will be skipped and the data in those columns will not be imported.
Import data from an Excel worksheet. One object is created for each row. The property names of the objects consist of the column names defined in the first row. In case a column doesnt have a column header (usually in row 1 when -StartRow is not used), then the unnamed columns will be skipped and the data in those columns will not be imported.
----------------------------------------------
| File: Movies.xlsx - Sheet: Actors |
@@ -104,7 +109,7 @@ Function Import-Excel {
Notice that column 'B' is not imported because there's no value in cell 'B1' that can be used as property name for the objects.
.EXAMPLE
Import the complete Excel worksheet <EFBFBD>as is<EFBFBD> by using the <EFBFBD>-NoHeader<EFBFBD> switch. One object is created for each row. The property names of the objects will be automatically generated (P1, P2, P3, ..).
Import the complete Excel worksheet as is by using the -NoHeader switch. One object is created for each row. The property names of the objects will be automatically generated (P1, P2, P3, ..).
----------------------------------------------
| File: Movies.xlsx - Sheet: Actors |
@@ -132,7 +137,7 @@ Function Import-Excel {
Notice that the column header (row 1) is imported as an object too.
.EXAMPLE
Import data from an Excel worksheet. One object is created for each row. The property names of the objects consist of the names defined in the parameter <EFBFBD>-HeaderName<EFBFBD>. The properties are named starting from the most left column (A) to the right. In case no value is present in one of the columns, that property will have an empty value.
Import data from an Excel worksheet. One object is created for each row. The property names of the objects consist of the names defined in the parameter -HeaderName. The properties are named starting from the most left column (A) to the right. In case no value is present in one of the columns, that property will have an empty value.
----------------------------------------------------------
| File: Movies.xlsx - Sheet: Movies |
@@ -169,7 +174,7 @@ Function Import-Excel {
Notice that empty rows are imported and that data for the property 'Genre' is not present in the worksheet. As such, the 'Genre' property will be blanc for all objects.
.EXAMPLE
Import data from an Excel worksheet. One object is created for each row. The property names of the objects are automatically generated by using the switch <EFBFBD>-NoHeader<EFBFBD> (P1, P@, P#, ..). The switch <EFBFBD>-DataOnly<EFBFBD> will speed up the import because empty rows and empty columns are not imported.
Import data from an Excel worksheet. One object is created for each row. The property names of the objects are automatically generated by using the switch -NoHeader (P1, P@, P#, ..). The switch -DataOnly will speed up the import because empty rows and empty columns are not imported.
----------------------------------------------------------
| File: Movies.xlsx - Sheet: Movies |
@@ -181,7 +186,7 @@ Function Import-Excel {
|4 Skyfall 2012 9 |
----------------------------------------------------------
PS C:\> Import-Excel -Path 'C:\Movies.xlsx' -WorkSheetname Movies <EFBFBD>NoHeader -DataOnly
PS C:\> Import-Excel -Path 'C:\Movies.xlsx' -WorkSheetname Movies NoHeader -DataOnly
P1: The Bodyguard
P2: 1992
@@ -198,7 +203,7 @@ Function Import-Excel {
Notice that empty rows and empty columns are not imported.
.EXAMPLE
Import data from an Excel worksheet. One object is created for each row. The property names are provided with the <EFBFBD>-HeaderName<EFBFBD> parameter. The import will start from row 2 and empty columns and rows are not imported.
Import data from an Excel worksheet. One object is created for each row. The property names are provided with the -HeaderName parameter. The import will start from row 2 and empty columns and rows are not imported.
----------------------------------------------------------
| File: Movies.xlsx - Sheet: Actors |
@@ -209,13 +214,13 @@ Function Import-Excel {
|3 Jean-Claude Vandamme Brussels |
----------------------------------------------------------
PS C:\> Import-Excel -Path 'C:\Movies.xlsx' -WorkSheetname Actors -DataOnly -HeaderName 'FirstName', 'SecondName', 'City' <EFBFBD>StartRow 2
PS C:\> Import-Excel -Path 'C:\Movies.xlsx' -WorkSheetname Actors -DataOnly -HeaderName 'FirstName', 'SecondName', 'City' StartRow 2
FirstName : Jean-Claude
SecondName: Vandamme
City : Brussels
Notice that only 1 object is imported with only 3 properties. Column B and row 2 are empty and have been disregarded by using the switch '-DataOnly'. The property names have been named with the values provided with the parameter '-HeaderName'. Row number 1 with <EFBFBD>Chuck Norris<EFBFBD> has not been imported, because we started the import from row 2 with the parameter <EFBFBD>-StartRow 2<EFBFBD>.
Notice that only 1 object is imported with only 3 properties. Column B and row 2 are empty and have been disregarded by using the switch '-DataOnly'. The property names have been named with the values provided with the parameter '-HeaderName'. Row number 1 with Chuck Norris has not been imported, because we started the import from row 2 with the parameter -StartRow 2.
.LINK
https://github.com/dfinke/ImportExcel