mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-27 13:43:36 +00:00
Compare commits
4 Commits
copilot/fi
...
add-header
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6166f0e87c | ||
|
|
bb8297c528 | ||
|
|
b61aef888f | ||
|
|
4c002358fe |
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
|
||||||
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'
|
RootModule = 'ImportExcel.psm1'
|
||||||
|
|
||||||
# Version number of this module.
|
# Version number of this module.
|
||||||
ModuleVersion = '7.8.1'
|
ModuleVersion = '7.8.2'
|
||||||
|
|
||||||
# ID used to uniquely identify this module
|
# ID used to uniquely identify this module
|
||||||
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
|
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
[Switch]$Calculate,
|
[Switch]$Calculate,
|
||||||
[Switch]$Show,
|
[Switch]$Show,
|
||||||
[String]$WorksheetName = 'Sheet1',
|
[String]$WorksheetName = 'Sheet1',
|
||||||
|
[String]$HeaderName,
|
||||||
[Alias("PW")]
|
[Alias("PW")]
|
||||||
[String]$Password,
|
[String]$Password,
|
||||||
[switch]$ClearSheet,
|
[switch]$ClearSheet,
|
||||||
@@ -258,6 +259,11 @@
|
|||||||
try {
|
try {
|
||||||
if ($null -eq $InputObject) { $row += 1 }
|
if ($null -eq $InputObject) { $row += 1 }
|
||||||
foreach ($TargetData in $InputObject) {
|
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) {
|
if ($firstTimeThru) {
|
||||||
$firstTimeThru = $false
|
$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')
|
$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')
|
||||||
|
|||||||
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
|
||||||
|
}
|
||||||
|
}
|
||||||
13
changelog.md
13
changelog.md
@@ -1,3 +1,16 @@
|
|||||||
|
# 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
|
# 7.8.1
|
||||||
|
|
||||||
- Fixed conditional formatting so it recognizes 'Top and Bottom' as a rule type. Thanks [g-pearl](https://github.com/g-pearl)
|
- Fixed conditional formatting so it recognizes 'Top and Bottom' as a rule type. Thanks [g-pearl](https://github.com/g-pearl)
|
||||||
|
|||||||
Reference in New Issue
Block a user