Compare commits

..

53 Commits

Author SHA1 Message Date
Doug Finke
2ca870a889 Merge pull request #1112 from dfinke/refactor-rezip 2021-11-30 18:44:24 -05:00
Doug Finke
a320cfd28c Merge pull request #1110 from DavisHenckel/FAQ_Docs 2021-11-30 18:32:59 -05:00
dfinke
aa1b042767 Add -ReZip to Close-ExcelPackage like in Export-Excel #1111 2021-11-30 15:50:11 -05:00
dfinke
7e8416d67c Bump version and update change log 2021-11-30 15:49:07 -05:00
Davis Henckel
229b60b25d Remove Unnecessary Import & Add Header
Removed unnecessary import-module statement. Added a header to match the styling of other documents.
2021-11-28 20:03:07 -08:00
Davis Henckel
5700989321 Explain functionality of Open/Close-ExcelPackage 2021-11-28 11:36:37 -08:00
Davis Henckel
56e1704e7e Added missing " || improved readability. 2021-11-28 11:25:21 -08:00
Davis Henckel
8268bbc2e1 Merge pull request #1 from DavisHenckel/FAQ_Docs_2
Faq docs 2
2021-11-26 14:56:17 -08:00
Davis Henckel
5657659331 Add additional dbg info, Doc complete.
Changed variable name in example code, added additional info to Data structure explanation. Final proofread for punctuation and style. Document complete.
2021-11-26 14:53:21 -08:00
Davis Henckel
0d4a32e266 Change phrasing to show better MD style
Added additional headings to read more easily.
2021-11-26 14:42:44 -08:00
Davis Henckel
1c8f8d2a3d Update image to include row/col data num 2021-11-26 14:41:37 -08:00
Davis Henckel
f6a65677df Merge branch 'FAQ_Docs_2' of https://github.com/DavisHenckel/ImportExcel into FAQ_Docs_2 2021-11-26 14:34:23 -08:00
Davis Henckel
86a7865fb2 Add multiple images. 2021-11-26 14:34:17 -08:00
Davis Henckel
8b3bf4f14f Added Debug image showing var contents 2021-11-26 14:33:48 -08:00
Davis Henckel
4d6193f549 Complete Majority of Write to ExistingExcelFile md 2021-11-26 14:28:04 -08:00
Davis Henckel
6ebac7b6dc Add newlines 2021-11-26 12:18:05 -08:00
Davis Henckel
d71dd36d56 First version of file, with Image. 2021-11-26 12:17:39 -08:00
Davis Henckel
3697cdfeee Merge branch 'FAQ_Docs_2' of https://github.com/DavisHenckel/ImportExcel into FAQ_Docs_2 2021-11-26 12:17:06 -08:00
Davis Henckel
1e172cf21f Add Excel Pkg Data Img 2021-11-26 12:16:41 -08:00
Davis Henckel
6da7553c98 Merge branch 'FAQ_Docs' of https://github.com/DavisHenckel/ImportExcel into FAQ_Docs_2 2021-11-26 10:55:43 -08:00
Davis Henckel
5a444c620b Punctuation change 2021-11-26 10:25:30 -08:00
Davis Henckel
4a09fc3570 Punctuation change 2021-11-26 10:25:14 -08:00
Davis Henckel
d706a10276 Complete Document
Demonstrates how to create a blank excel file.
2021-11-26 10:23:09 -08:00
Davis Henckel
1fd2f422cd Create 2 more MD files to create 2021-11-26 10:15:01 -08:00
Davis Henckel
283e50547d Styling Consistency & Punctuation 2021-11-26 09:11:56 -08:00
Davis Henckel
15211a6297 Update Wording.
Punctuation and wording changes.
2021-11-26 09:08:31 -08:00
Davis Henckel
8905b8d401 Fix small typo
Comment in first image should have read "#Loads the Excel file into a PSCustomObject"
2021-11-26 08:58:44 -08:00
Davis Henckel
e9b437af4e Complete first version of ExistingExcelFile FAQ
Added loading a row/col, then mapping to HashTable
2021-11-26 06:56:08 -08:00
Davis Henckel
330e237727 Update Image Paths so they are Relative 2021-11-26 06:31:53 -08:00
Davis Henckel
c56b2cd33a Add first 2 images
Images display debugger and contents of excel file.
2021-11-21 16:56:52 -08:00
Davis Henckel
1aa5c6da45 add initial 2 images for first md FAQ doc 2021-11-21 16:51:14 -08:00
Davis Henckel
9eb894cf59 create directory, add initial md file w/first doc 2021-11-21 16:50:34 -08:00
dfinke
3a4c2d7bd9 bump version 2021-11-20 16:18:12 -05:00
dfinke
42cb5a316a update changelog 2021-11-20 16:18:05 -05:00
dfinke
536cdaa841 tweaked spelling 2021-11-20 15:53:23 -05:00
Doug Finke
6cd9fad7ba Merge pull request #1106 from royashbrook/CI-Updates
Add ACE into testing pipeline
2021-11-20 15:44:13 -05:00
Doug Finke
829d854c3d Merge pull request #1102 from muschebubusche/AddSelectiveColumnImport
Add selective column import
2021-11-20 15:36:05 -05:00
muschebubusche
b33a282740 Add new pester test 2021-11-20 16:51:49 +01:00
muschebubusche
73fc96166c Refactor selective column import
Move code into the function Get-PropertyNames and remove the rest.
Now it only replaces $Columns with $ImportColumns after a couple of checks. So the heavy work like arranging and getting the right values is done in the original way of Import-Excel.
2021-11-20 16:51:40 +01:00
Roy Ashbrook
00f7278115 add back *nix tests 2021-11-16 16:28:03 -05:00
Roy Ashbrook
956cf5aa49 fix 2021-11-16 16:23:54 -05:00
Roy Ashbrook
877310e015 add back pscore 2021-11-16 16:20:59 -05:00
Roy Ashbrook
08bf877535 add back in normal win testing 2021-11-16 16:11:32 -05:00
Roy Ashbrook
88e28a1d6c change path 2021-11-16 16:05:33 -05:00
Roy Ashbrook
eb63fe259a testing caching 2021-11-16 16:02:19 -05:00
Roy Ashbrook
6d97efc5c2 fun with curl... maybe 2021-11-16 15:43:57 -05:00
Roy Ashbrook
c567526eac testing caching 2021-11-16 15:34:36 -05:00
Roy Ashbrook
ed210cc730 testing 2021-11-16 15:29:32 -05:00
Roy Ashbrook
72f44ebcb9 trying a folder 2021-11-16 15:18:37 -05:00
Roy Ashbrook
f1d20ed163 test pipeline cache 2021-11-16 15:13:52 -05:00
Roy Ashbrook
02cf6bb3f3 test ace 2021-11-16 09:03:53 -05:00
muschebubusche
8f0fc7397d Add example and automatic column arrangement
Add example and automatic column arrangement
2021-11-09 22:40:40 +01:00
muschebubusche
62c8d74a59 Add selective column import 2021-11-03 23:15:18 +01:00
21 changed files with 639 additions and 293 deletions

View 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

View 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"
```

View 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
![ExcelFileContents](/images/FAQ_Images/ExcelFileContents.png)
After loading this data into ```$ExcelFile``` the data is stored like:
![ExcelFileDebugImg](/images/FAQ_Images/ExcelFileDebugImg.jpg)
## 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
}
```

View 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:
![ExcelFileContents](/images/FAQ_Images/ExcelFileContents.png)
### 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:
![DataStructureExcelPkg](/images/FAQ_Images/DataStructureExcelPkg.png)
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:
![ExcelFileContentsPostAdd](/images/FAQ_Images/ExcelFileContentsPostAdd.png)
### Load Value at Specific Index
```powershell
$ValueAtIndex = $WorkSheet[2,1].Value #Loads the value at row 2, column A
```
```$ValueAtIndex``` now contains: ![ValueAtIndexData](/images/FAQ_Images/ValueAtIndexData.png)
### 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.

View File

@@ -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'

View 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 : $_" }
}

View File

@@ -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 }
} }

View File

@@ -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()

View File

@@ -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) {

View File

@@ -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'
}
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -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'

View File

@@ -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!

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB