mirror of
https://github.com/dfinke/ImportExcel.git
synced 2026-01-28 15:23:54 +00:00
Compare commits
13 Commits
v7.8.0
...
add-header
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6166f0e87c | ||
|
|
bb8297c528 | ||
|
|
b61aef888f | ||
|
|
4c002358fe | ||
|
|
be5d270f44 | ||
|
|
f9fb49ad04 | ||
|
|
4727bb3b2b | ||
|
|
ac435fc1e1 | ||
|
|
340ffc560b | ||
|
|
069c227391 | ||
|
|
fa4f3a23cd | ||
|
|
f54db0e2d9 | ||
|
|
8c1388a799 |
29
Examples/CopyRange/CopyFromOneSheetInSameWorkbook.ps1
Normal file
29
Examples/CopyRange/CopyFromOneSheetInSameWorkbook.ps1
Normal file
@@ -0,0 +1,29 @@
|
||||
<#
|
||||
Copy a range from WorksheetA to WorksheetB
|
||||
#>
|
||||
|
||||
$data = ConvertFrom-Csv @"
|
||||
Region,State,Units,Price
|
||||
West,Texas,927,923.71
|
||||
North,Tennessee,466,770.67
|
||||
East,Florida,520,458.68
|
||||
East,Maine,828,661.24
|
||||
West,Virginia,465,053.58
|
||||
North,Missouri,436,235.67
|
||||
South,Kansas,214,992.47
|
||||
North,North Dakota,789,640.72
|
||||
South,Delaware,712,508.55
|
||||
"@
|
||||
|
||||
$xlfile = "./test.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
$data | Export-Excel $xlfile -WorksheetName WorksheetA
|
||||
$data | Export-Excel $xlfile -WorksheetName WorksheetB
|
||||
|
||||
$excel = Open-ExcelPackage $xlfile
|
||||
|
||||
# Copy a range from WorksheetA to WorksheetB
|
||||
$excel.WorksheetA.Cells["A3:B5"].Copy($excel.WorksheetB.Cells["G3"])
|
||||
|
||||
Close-ExcelPackage $excel -Show
|
||||
10
Examples/FormatResults/GetAsMarkdownTable.ps1
Normal file
10
Examples/FormatResults/GetAsMarkdownTable.ps1
Normal file
@@ -0,0 +1,10 @@
|
||||
param(
|
||||
[Alias('FullName')]
|
||||
[String[]]$Path
|
||||
)
|
||||
|
||||
if ($PSVersionTable.PSVersion.Major -gt 5 -and -not (Get-Command Format-Markdown -ErrorAction SilentlyContinue)) {
|
||||
throw "This requires EZOut. Install-Module EZOut -AllowClobber -Scope CurrentUser"
|
||||
}
|
||||
|
||||
Import-Excel $Path | Format-Markdown
|
||||
10
Examples/FormatResults/GetAsYaml.ps1
Normal file
10
Examples/FormatResults/GetAsYaml.ps1
Normal file
@@ -0,0 +1,10 @@
|
||||
param(
|
||||
[Alias('FullName')]
|
||||
[String[]]$Path
|
||||
)
|
||||
|
||||
if ($PSVersionTable.PSVersion.Major -gt 5 -and -not (Get-Command Format-YAML -ErrorAction SilentlyContinue)) {
|
||||
throw "This requires EZOut. Install-Module EZOut -AllowClobber -Scope CurrentUser"
|
||||
}
|
||||
|
||||
Import-Excel $Path | Format-YAML
|
||||
11
Examples/FormatResults/Sample.csv
Normal file
11
Examples/FormatResults/Sample.csv
Normal file
@@ -0,0 +1,11 @@
|
||||
"OrderId","Category","Sales","Quantity","Discount"
|
||||
"1","Cosmetics","744.01","7","0.7"
|
||||
"2","Grocery","349.13","25","0.3"
|
||||
"3","Apparels","535.11","88","0.2"
|
||||
"4","Electronics","524.69","60","0.1"
|
||||
"5","Electronics","439.1","41","0"
|
||||
"6","Apparels","56.84","54","0.8"
|
||||
"7","Electronics","326.66","97","0.7"
|
||||
"8","Cosmetics","17.25","74","0.6"
|
||||
"9","Grocery","199.96","39","0.4"
|
||||
"10","Grocery","731.77","20","0.3"
|
||||
|
BIN
Examples/FormatResults/Sample.xlsx
Normal file
BIN
Examples/FormatResults/Sample.xlsx
Normal file
Binary file not shown.
24
Examples/HeaderName/ConvertDictionaryOfArraysToExcel.ps1
Normal file
24
Examples/HeaderName/ConvertDictionaryOfArraysToExcel.ps1
Normal file
@@ -0,0 +1,24 @@
|
||||
function ConvertTo-Excel {
|
||||
param(
|
||||
$Path,
|
||||
[System.Collections.IDictionary]$targetData
|
||||
)
|
||||
|
||||
$column = 1
|
||||
foreach ($key in $targetData.Keys) {
|
||||
$cityData[$key] | Export-Excel $xlfile -StartColumn ($column++) -HeaderName $key -AutoSize
|
||||
}
|
||||
}
|
||||
|
||||
$cityData = [Ordered]@{}
|
||||
|
||||
$cityData.City = "New York City", "Paris", "Barcelona", "Rome"
|
||||
$cityData.Country = "United States", "France", "Spain", "Italy"
|
||||
$cityData.Population = 8600000, 2141000, 5515000, 2873000
|
||||
|
||||
$xlfile = "$PSScriptRoot/test.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
ConvertTo-Excel $xlfile $cityData
|
||||
|
||||
. $xlfile
|
||||
24
Examples/HeaderName/HeaderName.ps1
Normal file
24
Examples/HeaderName/HeaderName.ps1
Normal file
@@ -0,0 +1,24 @@
|
||||
try { Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 } catch { throw ; return }
|
||||
|
||||
## This exports only the numbers
|
||||
# 1..10 | Export-excel $PSScriptRoot\test.xlsx -Show
|
||||
|
||||
## This exports the numbers and in A1 the text "MyNum"
|
||||
# 1..10 | Export-excel $PSScriptRoot\test.xlsx -HeaderName MyNum -Show
|
||||
|
||||
$xlfile = "$PSScriptRoot/testMultipleColumns.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
$Regions = 'West', 'North', 'East ', 'East ', 'West ', 'North', 'South', 'North', 'South'
|
||||
$States = 'Texas', 'Tennessee', 'Florida', 'Maine', 'Virginia', 'Missouri', 'Kansas', 'North Dakota', 'Delaware'
|
||||
$Units = 927, 466, 520, 828, 465, 436, 214, 789, 712
|
||||
$Prices = 923.71, 770.67, 458.68, 661.24, 53.58, 235.67, 992.47, 640.72, 508.55
|
||||
|
||||
# Export each list (array) as a separate column to the same worksheet and workbook
|
||||
$Regions | Export-Excel -Path $xlfile -HeaderName Region -StartColumn 1 -AutoSize
|
||||
$States | Export-Excel -Path $xlfile -HeaderName State -StartColumn 2 -AutoSize
|
||||
$Units | Export-Excel -Path $xlfile -HeaderName Units -StartColumn 3 -AutoSize
|
||||
$Prices | Export-Excel -Path $xlfile -HeaderName Prices -StartColumn 4 -AutoSize
|
||||
|
||||
# Show the results in Excel
|
||||
. $xlfile
|
||||
@@ -6,7 +6,7 @@
|
||||
RootModule = 'ImportExcel.psm1'
|
||||
|
||||
# Version number of this module.
|
||||
ModuleVersion = '7.8.0'
|
||||
ModuleVersion = '7.8.2'
|
||||
|
||||
# ID used to uniquely identify this module
|
||||
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
|
||||
|
||||
@@ -118,7 +118,7 @@
|
||||
Write-Warning -Message "The condition will look for the quotes at the start and end."
|
||||
}
|
||||
if ($PSBoundParameters.ContainsKey("ConditionValue" ) -and
|
||||
$RuleType -match "Top|Botom" ) {$rule.Rank = $ConditionValue }
|
||||
$RuleType -match "Top|Bottom" ) {$rule.Rank = $ConditionValue }
|
||||
if ($PSBoundParameters.ContainsKey("ConditionValue" ) -and
|
||||
$RuleType -match "StdDev" ) {$rule.StdDev = $ConditionValue }
|
||||
if ($PSBoundParameters.ContainsKey("ConditionValue" ) -and
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
[Switch]$Calculate,
|
||||
[Switch]$Show,
|
||||
[String]$WorksheetName = 'Sheet1',
|
||||
[String]$HeaderName,
|
||||
[Alias("PW")]
|
||||
[String]$Password,
|
||||
[switch]$ClearSheet,
|
||||
@@ -258,6 +259,11 @@
|
||||
try {
|
||||
if ($null -eq $InputObject) { $row += 1 }
|
||||
foreach ($TargetData in $InputObject) {
|
||||
|
||||
if ($HeaderName -and $TargetData.psobject.TypeNames[0] -match 'System.String|System.Int32|System.Double|System.Char') {
|
||||
$TargetData = [PSCustomObject]@{ $HeaderName = $TargetData }
|
||||
}
|
||||
|
||||
if ($firstTimeThru) {
|
||||
$firstTimeThru = $false
|
||||
$isDataTypeValueType = ($null -eq $TargetData) -or ($TargetData.GetType().name -match 'string|timespan|datetime|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort|URI|ExcelHyperLink')
|
||||
|
||||
@@ -140,4 +140,72 @@ Describe "Creating small named ranges with hyperlinks" {
|
||||
$pt.RowFields[1].Grouping.Interval | Should -Be 3
|
||||
}
|
||||
}
|
||||
Context "Adding group date column" -Tag GroupColumnTests {
|
||||
it "Tests adding a group date column" {
|
||||
$xlFile = "TestDrive:\Results.xlsx"
|
||||
Remove-Item $xlFile -ErrorAction Ignore
|
||||
|
||||
$PivotTableDefinition = New-PivotTableDefinition -Activate -PivotTableName Points `
|
||||
-PivotRows Driver -PivotColumns Date -PivotData @{Points = "SUM" } -GroupDateColumn Date -GroupDatePart Years, Months
|
||||
|
||||
$excel = Import-Csv "$PSScriptRoot\First10Races.csv" |
|
||||
Select-Object Race, @{n = "Date"; e = { [datetime]::ParseExact($_.date, "dd/MM/yyyy", (Get-Culture)) } }, FinishPosition, Driver, GridPosition, Team, Points |
|
||||
Export-Excel $xlFile -AutoSize -PivotTableDefinition $PivotTableDefinition -PassThru
|
||||
|
||||
$excel.Workbook.Worksheets.Count | Should -Be 2
|
||||
$excel.Workbook.Worksheets[1].Name | Should -BeExactly 'Sheet1'
|
||||
$excel.Workbook.Worksheets[2].Name | Should -BeExactly 'Points'
|
||||
$excel.Points.PivotTables.Count | Should -Be 1
|
||||
$pt = $excel.Points.PivotTables[0]
|
||||
$pt.RowFields.Count | Should -Be 1
|
||||
$pt.RowFields[0].name | Should -Be "Driver"
|
||||
|
||||
$pt.ColumnFields.Count | Should -Be 2
|
||||
|
||||
$pt.ColumnFields[0].name | Should -Be "Years"
|
||||
$pt.ColumnFields[0].Grouping | Should -Not -BeNullOrEmpty
|
||||
$pt.ColumnFields[0].Grouping.GroupBy | Should -Be "Years"
|
||||
|
||||
$pt.ColumnFields[1].name | Should -Be "Date"
|
||||
$pt.ColumnFields[1].Grouping | Should -Not -BeNullOrEmpty
|
||||
$pt.ColumnFields[1].Grouping.GroupBy | Should -Be "Months"
|
||||
|
||||
Close-ExcelPackage $excel
|
||||
|
||||
Remove-Item $xlFile -ErrorAction Ignore
|
||||
}
|
||||
}
|
||||
Context "Adding group numeric column" -Tag GroupColumnTests {
|
||||
it "Tests adding numeric group column" {
|
||||
$xlFile = "TestDrive:\Results.xlsx"
|
||||
Remove-Item $xlFile -ErrorAction Ignore
|
||||
|
||||
$PivotTableDefinition = New-PivotTableDefinition -Activate -PivotTableName Places `
|
||||
-PivotRows Driver -PivotColumns FinishPosition -PivotData @{Date = "Count" } -GroupNumericColumn FinishPosition -GroupNumericMin 1 -GroupNumericMax 25 -GroupNumericInterval 3
|
||||
|
||||
$excel = Import-Csv "$PSScriptRoot\First10Races.csv" |
|
||||
Select-Object Race, @{n = "Date"; e = { [datetime]::ParseExact($_.date, "dd/MM/yyyy", (Get-Culture)) } }, FinishPosition, Driver, GridPosition, Team, Points |
|
||||
Export-Excel $xlFile -AutoSize -PivotTableDefinition $PivotTableDefinition -PassThru
|
||||
|
||||
$excel.Workbook.Worksheets.Count | Should -Be 2
|
||||
$excel.Workbook.Worksheets[1].Name | Should -BeExactly 'Sheet1'
|
||||
$excel.Workbook.Worksheets[2].Name | Should -BeExactly 'Places'
|
||||
$excel.Places.PivotTables.Count | Should -Be 1
|
||||
$pt = $excel.Places.PivotTables[0]
|
||||
$pt.RowFields.Count | Should -Be 1
|
||||
$pt.RowFields[0].name | Should -Be "Driver"
|
||||
|
||||
$pt.ColumnFields.Count | Should -Be 1
|
||||
|
||||
$pt.ColumnFields[0].name | Should -Be "FinishPosition"
|
||||
$pt.ColumnFields[0].Grouping | Should -Not -BeNullOrEmpty
|
||||
$pt.ColumnFields[0].Grouping.Start | Should -Be 1
|
||||
$pt.ColumnFields[0].Grouping.End | Should -Be 25
|
||||
$pt.ColumnFields[0].Grouping.Interval | Should -Be 3
|
||||
|
||||
Close-ExcelPackage $excel
|
||||
|
||||
Remove-Item $xlFile -ErrorAction Ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
88
__tests__/HeaderName.tests.ps1
Normal file
88
__tests__/HeaderName.tests.ps1
Normal file
@@ -0,0 +1,88 @@
|
||||
if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||
}
|
||||
|
||||
Describe "Test HeaderName parameter" -Tag HeaderName {
|
||||
It "Should add data as usual" {
|
||||
$xlfile = "TestDrive:\headername.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
1..10 | Export-Excel -Path $xlfile
|
||||
|
||||
{ Test-Path $xlfile } | Should -BeTrue
|
||||
|
||||
$excel = Open-ExcelPackage $xlfile
|
||||
|
||||
1..10 | ForEach-Object {
|
||||
$excel.Sheet1.Cells[$_, 1].Text | Should -BeExactly $_
|
||||
}
|
||||
|
||||
Close-ExcelPackage $excel
|
||||
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
It "Should add data and the first cell should have the header name" {
|
||||
$xlfile = "TestDrive:\headername.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
1..10 | Export-Excel -Path $xlfile -HeaderName MyNum
|
||||
|
||||
{ Test-Path $xlfile } | Should -BeTrue
|
||||
|
||||
$excel = Open-ExcelPackage $xlfile
|
||||
|
||||
$excel.Sheet1.Cells[1, 1].Text | Should -BeExactly "MyNum"
|
||||
|
||||
1..10 | ForEach-Object {
|
||||
$excel.Sheet1.Cells[($_ + 1), 1].Text | Should -BeExactly $_
|
||||
}
|
||||
|
||||
Close-ExcelPackage $excel
|
||||
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
It "Should ignore the header name" {
|
||||
$data = ConvertFrom-Csv @"
|
||||
Region,State,Units,Price
|
||||
West,Texas,927,923.71
|
||||
North,Tennessee,466,770.67
|
||||
East,Florida,520,458.68
|
||||
East,Maine,828,661.24
|
||||
West,Virginia,465,053.58
|
||||
North,Missouri,436,235.67
|
||||
South,Kansas,214,992.47
|
||||
North,North Dakota,789,640.72
|
||||
South,Delaware,712,508.55
|
||||
"@
|
||||
|
||||
$xlfile = "TestDrive:\headername.xlsx"
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
|
||||
$data | Export-Excel -Path $xlfile -HeaderName MyNum
|
||||
|
||||
{ Test-Path $xlfile } | Should -BeTrue
|
||||
|
||||
$excel = Open-ExcelPackage $xlfile
|
||||
|
||||
$excel.Sheet1.Cells[1, 1].Text | Should -BeExactly "Region"
|
||||
$excel.Sheet1.Cells[1, 2].Text | Should -BeExactly "State"
|
||||
$excel.Sheet1.Cells[1, 3].Text | Should -BeExactly "Units"
|
||||
$excel.Sheet1.Cells[1, 4].Text | Should -BeExactly "Price"
|
||||
|
||||
$excel.Sheet1.Cells[2, 1].Text | Should -BeExactly "West"
|
||||
$excel.Sheet1.Cells[2, 2].Text | Should -BeExactly "Texas"
|
||||
$excel.Sheet1.Cells[2, 3].Text | Should -Be 927
|
||||
$excel.Sheet1.Cells[2, 4].Text | Should -Be 923.71
|
||||
|
||||
$excel.Sheet1.Cells[10, 1].Text | Should -BeExactly "South"
|
||||
$excel.Sheet1.Cells[10, 2].Text | Should -BeExactly "Delaware"
|
||||
$excel.Sheet1.Cells[10, 3].Text | Should -Be 712
|
||||
$excel.Sheet1.Cells[10, 4].Text | Should -Be 508.55
|
||||
|
||||
Close-ExcelPackage $excel
|
||||
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
20
changelog.md
20
changelog.md
@@ -1,3 +1,23 @@
|
||||
# 7.8.2
|
||||
|
||||
- Added `HeaderName` parameter to `Export-Excel`. Allows you to output an object with a property name. Otherwise the data is just the array of values.
|
||||
|
||||
```powershell
|
||||
1..10 | Export-Excel -Path .\test.xlsx -HeaderName MyNum
|
||||
```
|
||||
|
||||
- Added example `CopyFromOneSheetInSameWorkbook`. Shows how to copy a range of data from WorksheetA to WorksheetB
|
||||
- Added example `HeaderName`. Shows how to use the new `-HeaderName` parameter
|
||||
- Added example `HeaderName`. Shows how to use the new `-HeaderName` parameter
|
||||
- Added example `ConvertDictionaryOfArraysToExcel` example. Takes a dictionary of arrays and converts it Rows and Columns of data in Excel.
|
||||
|
||||
# 7.8.1
|
||||
|
||||
- Fixed conditional formatting so it recognizes 'Top and Bottom' as a rule type. Thanks [g-pearl](https://github.com/g-pearl)
|
||||
* Update open-excelpackage.md. Thanks [stahler](https://github.com/stahler)
|
||||
- Added Group Column tests
|
||||
|
||||
|
||||
# 7.8.0
|
||||
Thanks [James O'Neill](https://github.com/jhoneill)
|
||||
|
||||
|
||||
@@ -39,8 +39,8 @@ This will create a new file in the temp folder if it doesn't already exist. It t
|
||||
### EXAMPLE 2
|
||||
|
||||
```text
|
||||
PS\> $excela= Open-ExcelPackage -path "$xlPath" -Password $password
|
||||
PS\> $sheet1 = $excel.Workbook.Worksheetsa"sheet1" ]
|
||||
PS\> $excel= Open-ExcelPackage -path "$xlPath" -Password $password
|
||||
PS\> $sheet1 = $excel.Workbook.Worksheets["sheet1"]
|
||||
PS\> Set-ExcelRange -Range $sheet1.Cells ["E1:S1048576" ], $sheet1.Cells ["V1:V1048576" ] -NFormat ( [cultureinfo ]::CurrentCulture.DateTimeFormat.ShortDatePattern)
|
||||
PS\> Close-ExcelPackage $excel -Show
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user