mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-16 08:13:54 +00:00
Compare commits
53 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ca870a889 | ||
|
|
a320cfd28c | ||
|
|
aa1b042767 | ||
|
|
7e8416d67c | ||
|
|
229b60b25d | ||
|
|
5700989321 | ||
|
|
56e1704e7e | ||
|
|
8268bbc2e1 | ||
|
|
5657659331 | ||
|
|
0d4a32e266 | ||
|
|
1c8f8d2a3d | ||
|
|
f6a65677df | ||
|
|
86a7865fb2 | ||
|
|
8b3bf4f14f | ||
|
|
4d6193f549 | ||
|
|
6ebac7b6dc | ||
|
|
d71dd36d56 | ||
|
|
3697cdfeee | ||
|
|
1e172cf21f | ||
|
|
6da7553c98 | ||
|
|
5a444c620b | ||
|
|
4a09fc3570 | ||
|
|
d706a10276 | ||
|
|
1fd2f422cd | ||
|
|
283e50547d | ||
|
|
15211a6297 | ||
|
|
8905b8d401 | ||
|
|
e9b437af4e | ||
|
|
330e237727 | ||
|
|
c56b2cd33a | ||
|
|
1aa5c6da45 | ||
|
|
9eb894cf59 | ||
|
|
3a4c2d7bd9 | ||
|
|
42cb5a316a | ||
|
|
536cdaa841 | ||
|
|
6cd9fad7ba | ||
|
|
829d854c3d | ||
|
|
b33a282740 | ||
|
|
73fc96166c | ||
|
|
00f7278115 | ||
|
|
956cf5aa49 | ||
|
|
877310e015 | ||
|
|
08bf877535 | ||
|
|
88e28a1d6c | ||
|
|
eb63fe259a | ||
|
|
6d97efc5c2 | ||
|
|
c567526eac | ||
|
|
ed210cc730 | ||
|
|
72f44ebcb9 | ||
|
|
f1d20ed163 | ||
|
|
02cf6bb3f3 | ||
|
|
8f0fc7397d | ||
|
|
62c8d74a59 |
9
Examples/ImportColumns/ImportColumns.ps1
Normal file
9
Examples/ImportColumns/ImportColumns.ps1
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}
|
||||||
|
|
||||||
|
# Create example file
|
||||||
|
$xlFile = "$PSScriptRoot\ImportColumns.xlsx"
|
||||||
|
Get-Process | Export-Excel -Path $xlFile
|
||||||
|
# -ImportColumns will also arrange columns
|
||||||
|
Import-Excel -Path $xlFile -ImportColumns @(1,3,2) -NoHeader -StartRow 1
|
||||||
|
# Get only pm, npm, cpu, id, processname
|
||||||
|
Import-Excel -Path $xlFile -ImportColumns @(6,7,12,25,46) | Format-Table -AutoSize
|
||||||
6
FAQ/How to Create an Empty Excel File.md
Normal file
6
FAQ/How to Create an Empty Excel File.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# Create an Empty Excel File
|
||||||
|
Use an empty string to export to an excel file.
|
||||||
|
```powershell
|
||||||
|
#Build an Excel file named: "file.xlsx" containing a worksheet: "MyWorksheet"
|
||||||
|
"" | Export-Excel -Path "C:\Test\file.xlsx" -WorksheetName "MyWorksheet"
|
||||||
|
```
|
||||||
41
FAQ/How to Read an Existing Excel File.md
Normal file
41
FAQ/How to Read an Existing Excel File.md
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# How to Read an Existing Excel File
|
||||||
|
## Enumerate the Excel File Contents
|
||||||
|
```powershell
|
||||||
|
#Load the Excel file into a PSCustomObject
|
||||||
|
$ExcelFile = Import-Excel "C:\Test\file.xlsx" -WorksheetName "Sheet1"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Visual of Data Structure
|
||||||
|
The File C:\Test\file.xlsx contains
|
||||||
|

|
||||||
|
|
||||||
|
After loading this data into ```$ExcelFile``` the data is stored like:
|
||||||
|

|
||||||
|
|
||||||
|
## Other Common Operations
|
||||||
|
|
||||||
|
### Load a Column
|
||||||
|
```powershell
|
||||||
|
$SpecificColumn = $ExcelFile."anotherHeader" #loads column with the header "anotherHeader" -- data stored in an array
|
||||||
|
```
|
||||||
|
|
||||||
|
### Load a Row
|
||||||
|
```powershell
|
||||||
|
$SpecificRow = $ExcelFile[1] #Loads row at index 1. Index 1 is the first row instead of 0.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Map Contents to Hashtable to Interpret Data
|
||||||
|
Sometimes mapping to a Hashtable is more convenient to have access to common Hashtable operations. Enumerate a Hashtable with the row's data by:
|
||||||
|
```powershell
|
||||||
|
$HashTable = @{}
|
||||||
|
$SpecificRow= $ExcelFile[2]
|
||||||
|
$SpecificRow.psobject.properties | ForEach-Object {
|
||||||
|
$HashTable[$_.Name] = $_.Value
|
||||||
|
}
|
||||||
|
```
|
||||||
|
To then iterate through the enumerated Hashtable:
|
||||||
|
```powershell
|
||||||
|
ForEach ($Key in ($HashTable.GetEnumerator()) | Where-Object {$_.Value -eq "x"}){ #Only grabs a key where the value is "x"
|
||||||
|
#values accessible with $Key.Name or $Key.Value
|
||||||
|
}
|
||||||
|
```
|
||||||
34
FAQ/How to Write to an Existing Excel File.md
Normal file
34
FAQ/How to Write to an Existing Excel File.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# Write to an Existing Excel File
|
||||||
|
### Enumerate the Excel File
|
||||||
|
The cmdlets ```Open-ExcelPackage``` and ```Close-ExcelPackage``` allow for direct modification to Excel file contents.
|
||||||
|
```powershell
|
||||||
|
$ExcelPkg = Open-ExcelPackage -Path "C:\Test\file.xlsx"
|
||||||
|
```
|
||||||
|
Contents of file.xlsx:
|
||||||
|

|
||||||
|
### Enumerate the Worksheet to View or Modify the Data
|
||||||
|
```powershell
|
||||||
|
$WorkSheet = $ExcelPkg.Workbook.Worksheets["sheet1"].Cells #open excel worksheet cells from worksheet "sheet1"
|
||||||
|
```
|
||||||
|
Visual of data structure:
|
||||||
|

|
||||||
|
A1 contains "someHeader", A2 contains "data1" etc.
|
||||||
|
### Modify a Specific Value in a File
|
||||||
|
Values can be accessed by row, column. Similar to a 2D array.
|
||||||
|
```powershell
|
||||||
|
$WorkSheet[1,4].Value = "New Column Header" #Starts at index 1 not 0
|
||||||
|
```
|
||||||
|
Contents of file.xlsx after modifying:
|
||||||
|

|
||||||
|
### Load Value at Specific Index
|
||||||
|
```powershell
|
||||||
|
$ValueAtIndex = $WorkSheet[2,1].Value #Loads the value at row 2, column A
|
||||||
|
```
|
||||||
|
```$ValueAtIndex``` now contains: 
|
||||||
|
### Save File After Modifying
|
||||||
|
The changes will not display in the Excel file until Close-ExcelPackage is called.
|
||||||
|
```powershell
|
||||||
|
Close-ExcelPackage $ExcelPkg #close and save changes made to the Excel file.
|
||||||
|
```
|
||||||
|
**Note**: If the file is currently in use, Close-ExcelPackage will return an error and will not save the information.
|
||||||
|
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
RootModule = 'ImportExcel.psm1'
|
RootModule = 'ImportExcel.psm1'
|
||||||
|
|
||||||
# Version number of this module.
|
# Version number of this module.
|
||||||
ModuleVersion = '7.3.1'
|
ModuleVersion = '7.4.1'
|
||||||
|
|
||||||
# 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'
|
||||||
|
|||||||
25
Private/Invoke-ExcelReZipFile.ps1
Normal file
25
Private/Invoke-ExcelReZipFile.ps1
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
function Invoke-ExcelReZipFile {
|
||||||
|
<#
|
||||||
|
#>
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory)]
|
||||||
|
[OfficeOpenXml.ExcelPackage]$ExcelPackage
|
||||||
|
)
|
||||||
|
|
||||||
|
Write-Verbose -Message "Re-Zipping $($ExcelPackage.file) using .NET ZIP library"
|
||||||
|
try {
|
||||||
|
Add-Type -AssemblyName 'System.IO.Compression.Filesystem' -ErrorAction stop
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Error "The -ReZip parameter requires .NET Framework 4.5 or later to be installed. Recommend to install Powershell v4+"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$TempZipPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.IO.Path]::GetRandomFileName())
|
||||||
|
$null = [io.compression.zipfile]::ExtractToDirectory($ExcelPackage.File, $TempZipPath)
|
||||||
|
Remove-Item $ExcelPackage.File -Force
|
||||||
|
$null = [io.compression.zipfile]::CreateFromDirectory($TempZipPath, $ExcelPackage.File)
|
||||||
|
Remove-Item $TempZipPath -Recurse -Force
|
||||||
|
}
|
||||||
|
catch { throw "Error resizipping $path : $_" }
|
||||||
|
}
|
||||||
@@ -4,13 +4,15 @@ function Close-ExcelPackage {
|
|||||||
param (
|
param (
|
||||||
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
|
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
|
||||||
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
|
||||||
[switch]$Show,
|
[Switch]$Show,
|
||||||
[Switch]$NoSave,
|
[Switch]$NoSave,
|
||||||
$SaveAs,
|
$SaveAs,
|
||||||
[ValidateNotNullOrEmpty()]
|
[ValidateNotNullOrEmpty()]
|
||||||
[String]$Password,
|
[String]$Password,
|
||||||
[switch]$Calculate
|
[Switch]$Calculate,
|
||||||
|
[Switch]$ReZip
|
||||||
)
|
)
|
||||||
|
|
||||||
if ( $NoSave) { $ExcelPackage.Dispose() }
|
if ( $NoSave) { $ExcelPackage.Dispose() }
|
||||||
else {
|
else {
|
||||||
if ($Calculate) {
|
if ($Calculate) {
|
||||||
@@ -27,6 +29,9 @@ function Close-ExcelPackage {
|
|||||||
else { $ExcelPackage.Save() }
|
else { $ExcelPackage.Save() }
|
||||||
$SaveAs = $ExcelPackage.File.FullName
|
$SaveAs = $ExcelPackage.File.FullName
|
||||||
}
|
}
|
||||||
|
if ($ReZip) {
|
||||||
|
Invoke-ExcelReZipFile -ExcelPackage $ExcelPackage
|
||||||
|
}
|
||||||
$ExcelPackage.Dispose()
|
$ExcelPackage.Dispose()
|
||||||
if ($Show) { Start-Process -FilePath $SaveAs }
|
if ($Show) { Start-Process -FilePath $SaveAs }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -253,7 +253,8 @@
|
|||||||
else { $firstTimeThru = $true }
|
else { $firstTimeThru = $true }
|
||||||
}
|
}
|
||||||
|
|
||||||
process { if ($PSBoundParameters.ContainsKey("InputObject")) {
|
process {
|
||||||
|
if ($PSBoundParameters.ContainsKey("InputObject")) {
|
||||||
try {
|
try {
|
||||||
if ($null -eq $InputObject) { $row += 1 }
|
if ($null -eq $InputObject) { $row += 1 }
|
||||||
foreach ($TargetData in $InputObject) {
|
foreach ($TargetData in $InputObject) {
|
||||||
@@ -321,7 +322,8 @@
|
|||||||
$ws.Cells[$row, $ColumnIndex].Style.Font.Color.SetColor([System.Drawing.Color]::Blue)
|
$ws.Cells[$row, $ColumnIndex].Style.Font.Color.SetColor([System.Drawing.Color]::Blue)
|
||||||
$ws.Cells[$row, $ColumnIndex].Style.Font.UnderLine = $true
|
$ws.Cells[$row, $ColumnIndex].Style.Font.UnderLine = $true
|
||||||
}
|
}
|
||||||
elseif ($v -isnot [String] ) { #Other objects or null.
|
elseif ($v -isnot [String] ) {
|
||||||
|
#Other objects or null.
|
||||||
if ($null -ne $v) { $ws.Cells[$row, $ColumnIndex].Value = $v.toString() }
|
if ($null -ne $v) { $ws.Cells[$row, $ColumnIndex].Value = $v.toString() }
|
||||||
}
|
}
|
||||||
elseif ($v[0] -eq '=') {
|
elseif ($v[0] -eq '=') {
|
||||||
@@ -362,7 +364,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch { throw "Failed exporting data to worksheet '$WorksheetName' to '$Path': $_" }
|
catch { throw "Failed exporting data to worksheet '$WorksheetName' to '$Path': $_" }
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
end {
|
end {
|
||||||
if ($firstTimeThru -and $ws.Dimension) {
|
if ($firstTimeThru -and $ws.Dimension) {
|
||||||
@@ -386,7 +389,8 @@
|
|||||||
$headerRange = $ws.Dimension.Address -replace "\d+$", $StartRow
|
$headerRange = $ws.Dimension.Address -replace "\d+$", $StartRow
|
||||||
#using a slightly odd syntax otherwise header ends up as a 2D array
|
#using a slightly odd syntax otherwise header ends up as a 2D array
|
||||||
$ws.Cells[$headerRange].Value | ForEach-Object -Begin { $Script:header = @() } -Process { $Script:header += $_ }
|
$ws.Cells[$headerRange].Value | ForEach-Object -Begin { $Script:header = @() } -Process { $Script:header += $_ }
|
||||||
if ($PSBoundParameters.ContainsKey($TargetData)) { #if Export was called with data that writes no header start the range at $startRow ($startRow is data)
|
if ($PSBoundParameters.ContainsKey($TargetData)) {
|
||||||
|
#if Export was called with data that writes no header start the range at $startRow ($startRow is data)
|
||||||
$targetRow = $StartRow
|
$targetRow = $StartRow
|
||||||
}
|
}
|
||||||
else { $targetRow = $StartRow + 1 } #if Export was called without data to add names (assume $startRow is a header) or...
|
else { $targetRow = $StartRow + 1 } #if Export was called without data to add names (assume $startRow is a header) or...
|
||||||
@@ -400,7 +404,8 @@
|
|||||||
foreach ($c in 0..($LastCol - $StartColumn)) {
|
foreach ($c in 0..($LastCol - $StartColumn)) {
|
||||||
$targetRangeName = @($script:Header)[$c] #Let Add-ExcelName fix (and warn about) bad names
|
$targetRangeName = @($script:Header)[$c] #Let Add-ExcelName fix (and warn about) bad names
|
||||||
Add-ExcelName -RangeName $targetRangeName -Range $ws.Cells[$targetRow, ($StartColumn + $c ), $LastRow, ($StartColumn + $c )]
|
Add-ExcelName -RangeName $targetRangeName -Range $ws.Cells[$targetRow, ($StartColumn + $c ), $LastRow, ($StartColumn + $c )]
|
||||||
try {#this test can throw with some names, surpress any error
|
try {
|
||||||
|
#this test can throw with some names, surpress any error
|
||||||
if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress(($targetRangeName -replace '\W' , '_' ))) {
|
if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress(($targetRangeName -replace '\W' , '_' ))) {
|
||||||
Write-Warning -Message "AutoNameRange: Property name '$targetRangeName' is also a valid Excel address and may cause issues. Consider renaming the property."
|
Write-Warning -Message "AutoNameRange: Property name '$targetRangeName' is also a valid Excel address and may cause issues. Consider renaming the property."
|
||||||
}
|
}
|
||||||
@@ -515,7 +520,8 @@
|
|||||||
}
|
}
|
||||||
catch { Write-Warning -Message "Failed adding Freezing the panes in worksheet '$WorksheetName': $_" }
|
catch { Write-Warning -Message "Failed adding Freezing the panes in worksheet '$WorksheetName': $_" }
|
||||||
|
|
||||||
if ($PSBoundParameters.ContainsKey("BoldTopRow")) { #it sets bold as far as there are populated cells: for whole row could do $ws.row($x).style.font.bold = $true
|
if ($PSBoundParameters.ContainsKey("BoldTopRow")) {
|
||||||
|
#it sets bold as far as there are populated cells: for whole row could do $ws.row($x).style.font.bold = $true
|
||||||
try {
|
try {
|
||||||
if ($Title) {
|
if ($Title) {
|
||||||
$range = $ws.Dimension.Address -replace '\d+', ($StartRow + 1)
|
$range = $ws.Dimension.Address -replace '\d+', ($StartRow + 1)
|
||||||
@@ -621,7 +627,8 @@
|
|||||||
if ($c.ConditionalType) {
|
if ($c.ConditionalType) {
|
||||||
$cfParams = @{RuleType = $c.ConditionalType; ConditionValue = $c.Text ;
|
$cfParams = @{RuleType = $c.ConditionalType; ConditionValue = $c.Text ;
|
||||||
BackgroundColor = $c.BackgroundColor; BackgroundPattern = $c.PatternType ;
|
BackgroundColor = $c.BackgroundColor; BackgroundPattern = $c.PatternType ;
|
||||||
ForeGroundColor = $c.ConditionalTextColor}
|
ForeGroundColor = $c.ConditionalTextColor
|
||||||
|
}
|
||||||
if ($c.Range) { $cfParams.Range = $c.Range }
|
if ($c.Range) { $cfParams.Range = $c.Range }
|
||||||
else { $cfParams.Range = $ws.Dimension.Address }
|
else { $cfParams.Range = $ws.Dimension.Address }
|
||||||
Add-ConditionalFormatting -Worksheet $ws @cfParams
|
Add-ConditionalFormatting -Worksheet $ws @cfParams
|
||||||
@@ -672,21 +679,7 @@
|
|||||||
else { $pkg.Save() }
|
else { $pkg.Save() }
|
||||||
Write-Verbose -Message "Saved workbook $($pkg.File)"
|
Write-Verbose -Message "Saved workbook $($pkg.File)"
|
||||||
if ($ReZip) {
|
if ($ReZip) {
|
||||||
Write-Verbose -Message "Re-Zipping $($pkg.file) using .NET ZIP library"
|
Invoke-ExcelReZipFile -ExcelPackage $pkg
|
||||||
try {
|
|
||||||
Add-Type -AssemblyName 'System.IO.Compression.Filesystem' -ErrorAction stop
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
Write-Error "The -ReZip parameter requires .NET Framework 4.5 or later to be installed. Recommend to install Powershell v4+"
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
$TempZipPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.IO.Path]::GetRandomFileName())
|
|
||||||
$null = [io.compression.zipfile]::ExtractToDirectory($pkg.File, $TempZipPath)
|
|
||||||
Remove-Item $pkg.File -Force
|
|
||||||
$null = [io.compression.zipfile]::CreateFromDirectory($TempZipPath, $pkg.File)
|
|
||||||
}
|
|
||||||
catch {throw "Error resizipping $path : $_"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$pkg.Dispose()
|
$pkg.Dispose()
|
||||||
|
|||||||
@@ -35,7 +35,8 @@
|
|||||||
[string[]]$AsText,
|
[string[]]$AsText,
|
||||||
[string[]]$AsDate,
|
[string[]]$AsDate,
|
||||||
[ValidateNotNullOrEmpty()]
|
[ValidateNotNullOrEmpty()]
|
||||||
[String]$Password
|
[String]$Password,
|
||||||
|
[Int[]]$ImportColumns
|
||||||
)
|
)
|
||||||
end {
|
end {
|
||||||
$sw = [System.Diagnostics.Stopwatch]::StartNew()
|
$sw = [System.Diagnostics.Stopwatch]::StartNew()
|
||||||
@@ -62,6 +63,16 @@
|
|||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if ($ImportColumns) {
|
||||||
|
$end = $Worksheet.Dimension.End.Column
|
||||||
|
# Check $ImportColumns
|
||||||
|
if ($ImportColumns[0] -le 0) { throw "The first entry in ImportColumns must be equal or greater 1" ; return }
|
||||||
|
# Check $StartColumn and $EndColumn
|
||||||
|
if (($StartColumn -ne 1) -or ($EndColumn -ne $end)) { Write-Warning -Message "If ImportColumns is set, then individual StartColumn and EndColumn will be ignored." }
|
||||||
|
# Replace $Columns with $ImportColumns
|
||||||
|
$Columns = $ImportColumns
|
||||||
|
}
|
||||||
|
|
||||||
if ($HeaderName) {
|
if ($HeaderName) {
|
||||||
$i = 0
|
$i = 0
|
||||||
foreach ($H in $HeaderName) {
|
foreach ($H in $HeaderName) {
|
||||||
|
|||||||
@@ -1,9 +1,18 @@
|
|||||||
|
#Requires -Modules Pester
|
||||||
|
|
||||||
|
if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||||
|
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||||
|
}
|
||||||
|
|
||||||
Describe "Import-Excel on a sheet with no headings" {
|
Describe "Import-Excel on a sheet with no headings" {
|
||||||
BeforeAll {
|
BeforeAll {
|
||||||
|
|
||||||
$xlfile = "TestDrive:\testImportExcel.xlsx"
|
$xlfile = "$PSScriptRoot\testImportExcel.xlsx"
|
||||||
$xlfileHeaderOnly = "TestDrive:\testImportExcelHeaderOnly.xlsx"
|
$xlfileHeaderOnly = "$PSScriptRoot\testImportExcelHeaderOnly.xlsx"
|
||||||
|
$xlfileImportColumns = "$PSScriptRoot\testImportExcelImportColumns.xlsx"
|
||||||
|
|
||||||
|
# Create $xlfile if it does not exist
|
||||||
|
if (!(Test-Path -Path $xlfile)) {
|
||||||
$xl = "" | Export-excel $xlfile -PassThru
|
$xl = "" | Export-excel $xlfile -PassThru
|
||||||
|
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'A'
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'A'
|
||||||
@@ -19,8 +28,10 @@ Describe "Import-Excel on a sheet with no headings" {
|
|||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C3 -Value 'I'
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C3 -Value 'I'
|
||||||
|
|
||||||
Close-ExcelPackage $xl
|
Close-ExcelPackage $xl
|
||||||
|
}
|
||||||
|
|
||||||
# crate $xlfileHeaderOnly
|
# Create $xlfileHeaderOnly if it does not exist
|
||||||
|
if (!(Test-Path -Path $xlfileHeaderOnly)) {
|
||||||
$xl = "" | Export-excel $xlfileHeaderOnly -PassThru
|
$xl = "" | Export-excel $xlfileHeaderOnly -PassThru
|
||||||
|
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'A'
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'A'
|
||||||
@@ -30,6 +41,28 @@ Describe "Import-Excel on a sheet with no headings" {
|
|||||||
Close-ExcelPackage $xl
|
Close-ExcelPackage $xl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Create $xlfileImportColumns if it does not exist
|
||||||
|
if (!(Test-Path -Path $xlfileImportColumns)) {
|
||||||
|
$xl = "" | Export-Excel $xlfileImportColumns -PassThru
|
||||||
|
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'A'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B1 -Value 'B'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C1 -Value 'C'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range D1 -Value 'D'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range E1 -Value 'E'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range F1 -Value 'F'
|
||||||
|
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A2 -Value '1'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B2 -Value '2'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C2 -Value '3'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range D2 -Value '4'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range E2 -Value '5'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range F2 -Value '6'
|
||||||
|
|
||||||
|
Close-ExcelPackage $xl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
It "Import-Excel should have this shape" {
|
It "Import-Excel should have this shape" {
|
||||||
$actual = @(Import-Excel $xlfile)
|
$actual = @(Import-Excel $xlfile)
|
||||||
|
|
||||||
@@ -167,7 +200,7 @@ Describe "Import-Excel on a sheet with no headings" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
It "Should" {
|
It "Should" {
|
||||||
$xlfile = "TestDrive:\testImportExcelSparse.xlsx"
|
$xlfile = "$PSScriptRoot\testImportExcelSparse.xlsx"
|
||||||
$xl = "" | Export-excel $xlfile -PassThru
|
$xl = "" | Export-excel $xlfile -PassThru
|
||||||
|
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'Chuck'
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'Chuck'
|
||||||
@@ -224,4 +257,139 @@ Describe "Import-Excel on a sheet with no headings" {
|
|||||||
$actual[0].P2 | Should -Be 'B'
|
$actual[0].P2 | Should -Be 'B'
|
||||||
$actual[0].P3 | Should -Be 'C'
|
$actual[0].P3 | Should -Be 'C'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used with the first column" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(1,2,4,5))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 4
|
||||||
|
$actualNames[0] | Should -Be 'A'
|
||||||
|
$actualNames[2] | Should -Be 'D'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 1
|
||||||
|
$actual[0].A | Should -Be 1
|
||||||
|
$actual[0].B | Should -Be 2
|
||||||
|
$actual[0].D | Should -Be 4
|
||||||
|
$actual[0].E | Should -Be 5
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used with the first column" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(1,3,4,5))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 4
|
||||||
|
$actualNames[0] | Should -Be 'A'
|
||||||
|
$actualNames[2] | Should -Be 'D'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 1
|
||||||
|
$actual[0].A | Should -Be 1
|
||||||
|
$actual[0].C | Should -Be 3
|
||||||
|
$actual[0].D | Should -Be 4
|
||||||
|
$actual[0].E | Should -Be 5
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used without the first column" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(2,3,6))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 3
|
||||||
|
$actualNames[0] | Should -Be 'B'
|
||||||
|
$actualNames[2] | Should -Be 'F'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 1
|
||||||
|
$actual[0].B | Should -Be 2
|
||||||
|
$actual[0].C | Should -Be 3
|
||||||
|
$actual[0].F | Should -Be 6
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used without the first column" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(2,5,6))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 3
|
||||||
|
$actualNames[0] | Should -Be 'B'
|
||||||
|
$actualNames[2] | Should -Be 'F'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 1
|
||||||
|
$actual[0].B | Should -Be 2
|
||||||
|
$actual[0].E | Should -Be 5
|
||||||
|
$actual[0].F | Should -Be 6
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used with only 1 column" {
|
||||||
|
$actual = @(Import-Excel $xlfile -ImportColumns @(2))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 1
|
||||||
|
$actualNames[0] | Should -Be 'B'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 2
|
||||||
|
$actual[0].B | Should -Be 'E'
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used with only 1 column which is also the last" {
|
||||||
|
$actual = @(Import-Excel $xlfile -ImportColumns @(3))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 1
|
||||||
|
$actualNames[0] | Should -Be 'C'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 2
|
||||||
|
$actual[1].C | Should -Be 'I'
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns contains all columns" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(1,2,3,4,5,6))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 6
|
||||||
|
$actualNames[0] | Should -Be 'A'
|
||||||
|
$actualNames[2] | Should -Be 'C'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 1
|
||||||
|
$actual[0].A | Should -Be 1
|
||||||
|
$actual[0].B | Should -Be 2
|
||||||
|
$actual[0].C | Should -Be 3
|
||||||
|
$actual[0].D | Should -Be 4
|
||||||
|
$actual[0].E | Should -Be 5
|
||||||
|
$actual[0].F | Should -Be 6
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should ignore -StartColumn and -EndColumn if -ImportColumns is set aswell" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(5) -StartColumn 2 -EndColumn 7)
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 1
|
||||||
|
$actualNames[0] | Should -Be 'E'
|
||||||
|
|
||||||
|
$actual[0].E | Should -Be '5'
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should arrange the columns if -ImportColumns is not in order" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(5,1,4))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 3
|
||||||
|
$actualNames[0] | Should -Be 'E'
|
||||||
|
$actualNames[1] | Should -Be 'A'
|
||||||
|
$actualNames[2] | Should -Be 'D'
|
||||||
|
|
||||||
|
$actual[0].E | Should -Be '5'
|
||||||
|
$actual[0].A | Should -Be '1'
|
||||||
|
$actual[0].D | Should -Be '4'
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should arrange the columns if -ImportColumns is not in order and -NoHeader is used" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(5,1,4) -NoHeader -StartRow 2)
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 3
|
||||||
|
$actualNames[0] | Should -Be 'P1'
|
||||||
|
$actualNames[1] | Should -Be 'P2'
|
||||||
|
$actualNames[2] | Should -Be 'P3'
|
||||||
|
|
||||||
|
$actual[0].P1 | Should -Be '5'
|
||||||
|
$actual[0].P2 | Should -Be '1'
|
||||||
|
$actual[0].P3 | Should -Be '4'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
BIN
__tests__/testImportExcel.xlsx
Normal file
BIN
__tests__/testImportExcel.xlsx
Normal file
Binary file not shown.
BIN
__tests__/testImportExcelHeaderOnly.xlsx
Normal file
BIN
__tests__/testImportExcelHeaderOnly.xlsx
Normal file
Binary file not shown.
BIN
__tests__/testImportExcelImportColumns.xlsx
Normal file
BIN
__tests__/testImportExcelImportColumns.xlsx
Normal file
Binary file not shown.
BIN
__tests__/testImportExcelSparse.xlsx
Normal file
BIN
__tests__/testImportExcelSparse.xlsx
Normal file
Binary file not shown.
@@ -20,6 +20,25 @@ jobs:
|
|||||||
vmImage: 'windows-latest'
|
vmImage: 'windows-latest'
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
|
# BEGIN - ACE support for Invoke-ExcelQuery testing
|
||||||
|
- task: Cache@2
|
||||||
|
inputs:
|
||||||
|
key: v2 | "$(Agent.OS)" | ace
|
||||||
|
path: ace
|
||||||
|
cacheHitVar: CACHE_RESTORED
|
||||||
|
displayName: Cache ACE
|
||||||
|
|
||||||
|
- bash: |
|
||||||
|
mkdir ./ace
|
||||||
|
curl -o ./ace/ace.exe https://download.microsoft.com/download/3/5/C/35C84C36-661A-44E6-9324-8786B8DBE231/accessdatabaseengine_X64.exe
|
||||||
|
displayName: 'Download ACE'
|
||||||
|
condition: ne(variables.CACHE_RESTORED, 'true')
|
||||||
|
|
||||||
|
- powershell: Start-Process ./ace/ace.exe -Wait -ArgumentList "/quiet /passive /norestart"
|
||||||
|
displayName: 'Install ACE for Invoke-ExcelQuery testing'
|
||||||
|
# END - ACE support for Invoke-ExcelQuery testing
|
||||||
|
|
||||||
- powershell: 'Install-Module -Name Pester -Force -SkipPublisherCheck'
|
- powershell: 'Install-Module -Name Pester -Force -SkipPublisherCheck'
|
||||||
displayName: 'Update Pester'
|
displayName: 'Update Pester'
|
||||||
- powershell: './CI/CI.ps1 -Test'
|
- powershell: './CI/CI.ps1 -Test'
|
||||||
@@ -49,6 +68,25 @@ jobs:
|
|||||||
vmImage: 'windows-latest'
|
vmImage: 'windows-latest'
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
|
# BEGIN - ACE support for Invoke-ExcelQuery testing
|
||||||
|
- task: Cache@2
|
||||||
|
inputs:
|
||||||
|
key: v2 | "$(Agent.OS)" | ace
|
||||||
|
path: ace
|
||||||
|
cacheHitVar: CACHE_RESTORED
|
||||||
|
displayName: Cache ACE
|
||||||
|
|
||||||
|
- bash: |
|
||||||
|
mkdir ./ace
|
||||||
|
curl -o ./ace/ace.exe https://download.microsoft.com/download/3/5/C/35C84C36-661A-44E6-9324-8786B8DBE231/accessdatabaseengine_X64.exe
|
||||||
|
displayName: 'Download ACE'
|
||||||
|
condition: ne(variables.CACHE_RESTORED, 'true')
|
||||||
|
|
||||||
|
- powershell: Start-Process ./ace/ace.exe -Wait -ArgumentList "/quiet /passive /norestart"
|
||||||
|
displayName: 'Install ACE for Invoke-ExcelQuery testing'
|
||||||
|
# END - ACE support for Invoke-ExcelQuery testing
|
||||||
|
|
||||||
- pwsh: 'Install-Module -Name Pester -Force'
|
- pwsh: 'Install-Module -Name Pester -Force'
|
||||||
displayName: 'Update Pester'
|
displayName: 'Update Pester'
|
||||||
- pwsh: './CI/CI.ps1 -Test'
|
- pwsh: './CI/CI.ps1 -Test'
|
||||||
|
|||||||
16
changelog.md
16
changelog.md
@@ -1,3 +1,19 @@
|
|||||||
|
# v7.4.1
|
||||||
|
|
||||||
|
- Implements: https://github.com/dfinke/ImportExcel/issues/1111
|
||||||
|
- Refactored ReZip into separate function
|
||||||
|
- Deletes temp folder after rezipping
|
||||||
|
- Added -ReZip to `Close-ExcelPackage`
|
||||||
|
|
||||||
|
# v7.4.0
|
||||||
|
|
||||||
|
- Thank you to [Max Goczall](https://github.com/muschebubusche) for this contribution!
|
||||||
|
- `ImportColumns` parameter added to `ImportExcel`. It is used to define which columns of the ExcelPackage should be imported.
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
Import-Excel -Path $xlFile -ImportColumns @(6,7,12,25,46)
|
||||||
|
```
|
||||||
|
|
||||||
# v7.3.1
|
# v7.3.1
|
||||||
|
|
||||||
- Added query Excel spreadsheets, with SQL queries!
|
- Added query Excel spreadsheets, with SQL queries!
|
||||||
|
|||||||
BIN
images/FAQ_Images/DataStructureExcelPkg.png
Normal file
BIN
images/FAQ_Images/DataStructureExcelPkg.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.8 KiB |
BIN
images/FAQ_Images/ExcelFileContents.png
Normal file
BIN
images/FAQ_Images/ExcelFileContents.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.3 KiB |
BIN
images/FAQ_Images/ExcelFileContentsPostAdd.png
Normal file
BIN
images/FAQ_Images/ExcelFileContentsPostAdd.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.1 KiB |
BIN
images/FAQ_Images/ExcelFileDebugImg.jpg
Normal file
BIN
images/FAQ_Images/ExcelFileDebugImg.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
BIN
images/FAQ_Images/ValueAtIndexData.png
Normal file
BIN
images/FAQ_Images/ValueAtIndexData.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
Reference in New Issue
Block a user