mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-10 13:23:29 +00:00
Merge pull request #706 from jhoneill/master
SQL -Force , import -asText , test fixes, better Linux behavior
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
Describe "Test adding trendlines to charts" {
|
||||
if (-not (get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||
}
|
||||
Describe "Test adding trendlines to charts" {
|
||||
BeforeAll {
|
||||
$script:data = ConvertFrom-Csv @"
|
||||
Region,Item,TotalSold
|
||||
|
||||
@@ -21,7 +21,7 @@ if ($Initialize) {
|
||||
}
|
||||
if ($Test) {
|
||||
function Get-EnvironmentInfo {
|
||||
if ($null -eq $IsWindows -or $IsWindows) {
|
||||
if ([environment]::OSVersion.Platform -like "win*") {
|
||||
# Get Windows Version
|
||||
try {
|
||||
$WinRelease, $WinVer = Get-ItemPropertyValue "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" ReleaseId, CurrentMajorVersionNumber, CurrentMinorVersionNumber, CurrentBuildNumber, UBR
|
||||
@@ -30,8 +30,18 @@ if ($Test) {
|
||||
catch {
|
||||
$WindowsVersion = [System.Environment]::OSVersion.Version
|
||||
}
|
||||
#TODO FIXME BUG this gets the latest version of the .NET Framework on the machine (ok for powershell.exe), not the version of .NET CORE in use by PWSH.EXE
|
||||
<#
|
||||
$VersionFilePath = (Get-Process -Id $PID | Select-Object -ExpandProperty Modules |
|
||||
Where-Object -Property modulename -eq "clrjit.dll").FileName
|
||||
if (-not $VersionFilePath) {
|
||||
$VersionFilePath = [System.Reflection.Assembly]::LoadWithPartialName("System.Core").location
|
||||
}
|
||||
(Get-ItemProperty -Path $VersionFilePath).VersionInfo |
|
||||
Select-Object -Property @{n="Version"; e={$_.ProductName + " " + $_.FileVersion}}, ProductName, FileVersionRaw, FileName
|
||||
#>
|
||||
|
||||
# Get .Net Version
|
||||
# Get .Net Version
|
||||
# https://stackoverflow.com/questions/3487265/powershell-script-to-return-versions-of-net-framework-on-a-machine
|
||||
$Lookup = @{
|
||||
378389 = [version]'4.5'
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#Requires -Modules Pester
|
||||
#Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
|
||||
if (-not (get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||
}
|
||||
Describe "Compare Worksheet" {
|
||||
BeforeAll {
|
||||
if ($PSVersionTable.PSVersion.Major -gt 5) {
|
||||
@@ -54,18 +55,9 @@ Describe "Compare Worksheet" {
|
||||
}
|
||||
}
|
||||
|
||||
Context "Setting the background to highlight different rows, use of grid view." {
|
||||
Context "Setting the background to highlight different rows" {
|
||||
BeforeAll {
|
||||
$useGrid = ($PSVersionTable.PSVersion.Major -LE 5)
|
||||
if ($useGrid) {
|
||||
$ModulePath = (Get-Command -Name 'Compare-WorkSheet').Module.Path
|
||||
$PowerShellExec = if ($PSEdition -eq 'Core') {'pwsh.exe'} else {'powershell.exe'}
|
||||
$PowerShellPath = Join-Path -Path $PSHOME -ChildPath $PowerShellExec
|
||||
. $PowerShellPath -Command ('Import-Module {0}; $null = Compare-WorkSheet "{1}server1.xlsx" "{1}server2.xlsx" -BackgroundColor ([System.Drawing.Color]::LightGreen) -GridView; Start-Sleep -sec 5' -f $ModulePath, (Resolve-Path 'TestDrive:').ProviderPath)
|
||||
}
|
||||
else {
|
||||
$null = Compare-WorkSheet "TestDrive:\server1.xlsx" "TestDrive:\server2.xlsx" -BackgroundColor ([System.Drawing.Color]::LightGreen) -GridView:$useGrid
|
||||
}
|
||||
$null = Compare-WorkSheet "TestDrive:\server1.xlsx" "TestDrive:\server2.xlsx" -BackgroundColor ([System.Drawing.Color]::LightGreen)
|
||||
$xl1 = Open-ExcelPackage -Path "TestDrive:\server1.xlsx"
|
||||
$xl2 = Open-ExcelPackage -Path "TestDrive:\server2.xlsx"
|
||||
$s1Sheet = $xl1.Workbook.Worksheets[1]
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
|
||||
if (-not (get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||
}
|
||||
$xlFile = "TestDrive:\testSQL.xlsx"
|
||||
|
||||
Describe "ConvertFrom-ExcelToSQLInsert" {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#Requires -Modules Pester
|
||||
|
||||
#Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
|
||||
if (-not (get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||
}
|
||||
if ($null -eq $IsWindows) {$IsWindows = [environment]::OSVersion.Platform -like "win*"}
|
||||
$WarningAction = "SilentlyContinue"
|
||||
Describe ExportExcel {
|
||||
. "$PSScriptRoot\Samples\Samples.ps1"
|
||||
if (Get-process -Name Excel,xlim -ErrorAction SilentlyContinue) {
|
||||
@@ -654,20 +657,21 @@ Describe ExportExcel {
|
||||
#Catch warning
|
||||
$warnvar = $null
|
||||
#Test create two data pages; as part of adding the second give both their own pivot table, test -autosize switch
|
||||
Get-Service | Select-Object -Property Status, Name, DisplayName, StartType, CanPauseAndContinue | Export-Excel -Path $path -AutoSize -TableName "All Services" -TableStyle Medium1 -WarningAction SilentlyContinue -WarningVariable warnvar
|
||||
Get-Service | Select-Object -Property Status, Name, DisplayName, StartType, CanPauseAndContinue | Export-Excel -Path $path -AutoSize -TableName "All Services" -TableStyle Medium1 -WarningVariable warnvar
|
||||
Get-Process | Select-Object -Property Name, Company, Handles, CPU, VM | Export-Excel -Path $path -AutoSize -WorkSheetname 'sheet2' -TableName "Processes" -TableStyle Light1 -Title "Processes" -TitleFillPattern Solid -TitleBackgroundColor ([System.Drawing.Color]::AliceBlue) -TitleBold -TitleSize 22 -PivotTableDefinition $ptDef
|
||||
$Excel = Open-ExcelPackage $path
|
||||
$ws1 = $Excel.Workbook.Worksheets["Sheet1"]
|
||||
$ws2 = $Excel.Workbook.Worksheets["Sheet2"]
|
||||
|
||||
|
||||
it "Set Column widths (with autosize) " {
|
||||
if ($isWindows) {
|
||||
it "Set Column widths (with autosize) " {
|
||||
$ws1.Column(2).Width | Should not be $ws1.DefaultColWidth
|
||||
$ws2.Column(1).width | Should not be $ws2.DefaultColWidth
|
||||
}
|
||||
}
|
||||
|
||||
it "Added tables to both sheets (handling illegal chars) and a title in sheet 2 " {
|
||||
$warnvar.count | Should be 1
|
||||
$warnvar.count | Should beGreaterThan 0
|
||||
$ws1.tables.Count | Should be 1
|
||||
$ws2.tables.Count | Should be 1
|
||||
$ws1.Tables[0].Address.Start.Row | Should be 1
|
||||
@@ -722,7 +726,8 @@ Describe ExportExcel {
|
||||
#Test freezing top row/first column, adding formats and a pivot table - from Add-Pivot table not a specification variable - after the export
|
||||
$excel = Get-Process | Select-Object -Property Name, Company, Handles, CPU, PM, NPM, WS | Export-Excel -Path $path -ClearSheet -WorkSheetname "Processes" -FreezeTopRowFirstColumn -PassThru
|
||||
$sheet = $excel.Workbook.Worksheets["Processes"]
|
||||
$sheet.Column(1) | Set-ExcelRange -Bold -AutoFit
|
||||
if ($isWindows) {$sheet.Column(1) | Set-ExcelRange -Bold -AutoFit }
|
||||
else {$sheet.Column(1) | Set-ExcelRange -Bold }
|
||||
$sheet.Column(2) | Set-ExcelRange -Width 29 -WrapText
|
||||
$sheet.Column(3) | Set-ExcelRange -HorizontalAlignment Right -NFormat "#,###"
|
||||
Set-ExcelRange -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NFormat "#,###"
|
||||
@@ -733,7 +738,7 @@ Describe ExportExcel {
|
||||
$rule = Add-ConditionalFormatting -passthru -Address $sheet.cells["C:C"] -RuleType TopPercent -ConditionValue 20 -Bold -StrikeThru
|
||||
Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor ([System.Drawing.Color]::Red) -Bold -Italic -Underline -BackgroundColor ([System.Drawing.Color]::Beige) -BackgroundPattern LightUp -PatternColor ([System.Drawing.Color]::Gray)
|
||||
#Test Set-ExcelRange with a column
|
||||
foreach ($c in 5..9) {Set-ExcelRange $sheet.Column($c) -AutoFit }
|
||||
if ($isWindows) { foreach ($c in 5..9) {Set-ExcelRange $sheet.Column($c) -AutoFit } }
|
||||
Add-PivotTable -PivotTableName "PT_Procs" -ExcelPackage $excel -SourceWorkSheet 1 -PivotRows Company -PivotData @{'Name' = 'Count'} -IncludePivotChart -ChartType ColumnClustered -NoLegend
|
||||
Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -AutoNameRange #Test adding named ranges seperately from adding data.
|
||||
|
||||
@@ -746,8 +751,10 @@ Describe ExportExcel {
|
||||
}
|
||||
it "Applied the formating " {
|
||||
$sheet | Should not beNullOrEmpty
|
||||
$sheet.Column(1).wdith | Should not be $sheet.DefaultColWidth
|
||||
$sheet.Column(7).wdith | Should not be $sheet.DefaultColWidth
|
||||
if ($isWindows) {
|
||||
$sheet.Column(1).width | Should not be $sheet.DefaultColWidth
|
||||
$sheet.Column(7).width | Should not be $sheet.DefaultColWidth
|
||||
}
|
||||
$sheet.Column(1).style.font.bold | Should be $true
|
||||
$sheet.Column(2).style.wraptext | Should be $true
|
||||
$sheet.Column(2).width | Should be 29
|
||||
@@ -1035,7 +1042,9 @@ Describe ExportExcel {
|
||||
$ExcelPackage.File | Should BeLike ([IO.Path]::GetTempPath() + '*')
|
||||
$Worksheet.Tables[0].Name | Should Be 'Table1'
|
||||
$Worksheet.AutoFilterAddress | Should BeNullOrEmpty
|
||||
$Worksheet.Column(5).Width | Should BeGreaterThan 9.5
|
||||
if ($isWindows) {
|
||||
$Worksheet.Column(5).Width | Should BeGreaterThan 9.5
|
||||
}
|
||||
}
|
||||
it "Now allows override of Path and TableName".PadRight(87) {
|
||||
$ExcelPackage = $Processes | Export-Excel -Now -PassThru -Path $Path -TableName:$false
|
||||
@@ -1044,7 +1053,9 @@ Describe ExportExcel {
|
||||
$ExcelPackage.File | Should Be $Path
|
||||
$Worksheet.Tables | Should BeNullOrEmpty
|
||||
$Worksheet.AutoFilterAddress | Should BeNullOrEmpty
|
||||
$Worksheet.Column(5).Width | Should BeGreaterThan 9.5
|
||||
if ($isWindows) {
|
||||
$Worksheet.Column(5).Width | Should BeGreaterThan 9.5
|
||||
}
|
||||
}
|
||||
<# Mock looks unreliable need to check
|
||||
Mock -CommandName 'Invoke-Item'
|
||||
@@ -1071,7 +1082,9 @@ Describe ExportExcel {
|
||||
|
||||
$Worksheet.Tables[0].Name | Should Be 'Data'
|
||||
$Worksheet.AutoFilterAddress | Should BeNullOrEmpty
|
||||
$Worksheet.Column(5).Width | Should BeGreaterThan 9.5
|
||||
if ($isWindows) {
|
||||
$Worksheet.Column(5).Width | Should BeGreaterThan 9.5
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
$scriptPath = Split-Path -Path $MyInvocation.MyCommand.path -Parent
|
||||
$dataPath = Join-Path -Path $scriptPath -ChildPath "First10Races.csv"
|
||||
$WarningAction = "SilentlyContinue"
|
||||
|
||||
Describe "Creating small named ranges with hyperlinks" {
|
||||
BeforeAll {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#Requires -Modules Pester
|
||||
#remove-module importExcel -erroraction silentlyContinue
|
||||
#Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
|
||||
if (-not (get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||
}
|
||||
|
||||
Describe "Check if Function aliases exist" {
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#Import-Module $PSScriptRoot\..\..\ImportExcel.psd1
|
||||
|
||||
if (-not (get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1
|
||||
}
|
||||
Describe "Tests" {
|
||||
BeforeAll {
|
||||
$data = $null
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
Describe "Exporting with -Inputobject" {
|
||||
Describe "Exporting with -Inputobject; table handling, Send SQL Data and import as " {
|
||||
BeforeAll {
|
||||
$path = "TestDrive:\Results.xlsx"
|
||||
Remove-Item -Path $path -ErrorAction SilentlyContinue
|
||||
#Read race results, and group by race name : export 1 row to get headers, leaving enough rows aboce to put in a link for each race
|
||||
. "$PSScriptRoot\Samples\Samples.ps1"
|
||||
$results = ((Get-Process) + (Get-Process -id $PID)) | Select-Object -last 10 -Property Name, cpu, pm, handles, StartTime
|
||||
$DataTable = [System.Data.DataTable]::new('Test')
|
||||
$null = $DataTable.Columns.Add('Name')
|
||||
@@ -16,6 +16,9 @@ Describe "Exporting with -Inputobject" {
|
||||
export-excel -Path $path -InputObject $results -WorksheetName Sheet1 -RangeName "Whole"
|
||||
export-excel -Path $path -InputObject $DataTable -WorksheetName Sheet2 -AutoNameRange
|
||||
Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet3 -TableName "Data"
|
||||
$DataTable.Rows.Clear()
|
||||
Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet4 -force -WarningVariable WVOne -WarningAction SilentlyContinue
|
||||
Send-SQLDataToExcel -path $path -DataTable ([System.Data.DataTable]::new('Test2')) -WorkSheetname Sheet5 -force -WarningVariable wvTwo -WarningAction SilentlyContinue
|
||||
$excel = Open-ExcelPackage $path
|
||||
$sheet = $excel.Sheet1
|
||||
}
|
||||
@@ -60,7 +63,7 @@ Describe "Exporting with -Inputobject" {
|
||||
}
|
||||
$sheet = $excel.Sheet3
|
||||
Context "Table of processes via Send-SQLDataToExcel" {
|
||||
it "Put the correct rows and columns into the sheet " {
|
||||
it "Put the correct data rows and columns into the sheet " {
|
||||
$sheet.Dimension.Rows | should be ($results.Count + 1)
|
||||
$sheet.Dimension.Columns | should be 5
|
||||
$sheet.cells["A1"].Value | should be "Name"
|
||||
@@ -76,4 +79,33 @@ Describe "Exporting with -Inputobject" {
|
||||
$sheet.Cells["E11"].Style.Numberformat.NumFmtID | should be 22
|
||||
}
|
||||
}
|
||||
$Sheet = $excel.Sheet4
|
||||
Context "Zero-row Data Table sent with Send-SQLDataToExcel -Force" {
|
||||
it "Raised a warning and put the correct data headers into the sheet " {
|
||||
$sheet.Dimension.Rows | should be 1
|
||||
$sheet.Dimension.Columns | should be 5
|
||||
$sheet.cells["A1"].Value | should be "Name"
|
||||
$sheet.cells["E1"].Value | should be "StartTime"
|
||||
$sheet.cells["A3"].Value | should beNullOrEmpty
|
||||
$wvone | should not beNullOrEmpty
|
||||
}
|
||||
}
|
||||
$Sheet = $excel.Sheet5
|
||||
Context "Zero-column Data Table handled by Send-SQLDataToExcel -Force" {
|
||||
it "Put Created a blank Sheet and raised a warning " {
|
||||
$sheet.Dimension | should beNullOrEmpty
|
||||
$wvTwo | should not beNullOrEmpty
|
||||
}
|
||||
|
||||
}
|
||||
Close-ExcelPackage $excel
|
||||
Context "Import As Text returns text values" {
|
||||
$x = import-excel $path -WorksheetName sheet3 -AsText StartTime,hand* | Select-Object -last 1
|
||||
it "Had fields of type string, not date or int, where specified as ASText " {
|
||||
$x.Handles.GetType().Name | should be "String"
|
||||
$x.StartTime.GetType().Name | should be "String"
|
||||
$x.CPU.GetType().Name | should not be "String"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#Requires -Modules Pester
|
||||
#Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
|
||||
|
||||
if (-not (get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||
}
|
||||
Describe "Remove Worksheet" {
|
||||
Context "Remove a worksheet output" {
|
||||
BeforeEach {
|
||||
|
||||
@@ -142,8 +142,10 @@ Describe "Set-ExcelColumn, Set-ExcelRow and Set-ExcelRange" {
|
||||
Set-ExcelRange -WorkSheet $ws -Range "E1" -ResetFont -HorizontalAlignment General -FontName "Courier New" -fontSize 9
|
||||
Set-ExcelRange -Address $ws.Cells["E7"] -ResetFont -WrapText -BackgroundColor ([System.Drawing.Color]::AliceBlue) -BackgroundPattern DarkTrellis -PatternColor ([System.Drawing.Color]::Red) -NumberFormat "£#,###.00"
|
||||
Set-ExcelRange -Address $ws.Column(1) -Width 0
|
||||
Set-ExcelRange -Address $ws.Column(2) -AutoFit
|
||||
Set-ExcelRange -Address $ws.Cells["E:E"] -AutoFit
|
||||
if (-not $env:NoAutoSize) {
|
||||
Set-ExcelRange -Address $ws.Column(2) -AutoFit
|
||||
Set-ExcelRange -Address $ws.Cells["E:E"] -AutoFit
|
||||
}
|
||||
#Test alias
|
||||
Set-Format -Address $ws.row(5) -Height 0
|
||||
$rr = $r.row
|
||||
@@ -323,36 +325,36 @@ Describe "AutoNameRange data with a single property name" {
|
||||
Remove-Item $xlfile -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
it "Should have a single item as a named range" {
|
||||
$excel = ConvertFrom-Csv @"
|
||||
it "Should have a single item as a named range " {
|
||||
$excel = ConvertFrom-Csv @"
|
||||
Sold
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
"@ | Export-Excel $xlfile -PassThru -AutoNameRange
|
||||
"@ | Export-Excel $xlfile -PassThru -AutoNameRange
|
||||
|
||||
$ws = $excel.Workbook.Worksheets["Sheet1"]
|
||||
$ws = $excel.Workbook.Worksheets["Sheet1"]
|
||||
|
||||
$ws.Names.Count | Should Be 1
|
||||
$ws.Names[0].Name | Should Be 'Sold'
|
||||
}
|
||||
$ws.Names.Count | Should Be 1
|
||||
$ws.Names[0].Name | Should Be 'Sold'
|
||||
}
|
||||
|
||||
it "Should have a more than a single item as a named range" {
|
||||
$excel = ConvertFrom-Csv @"
|
||||
it "Should have a more than a single item as a named range " {
|
||||
$excel = ConvertFrom-Csv @"
|
||||
Sold,ID
|
||||
1,a
|
||||
2,b
|
||||
3,c
|
||||
4,d
|
||||
"@ | Export-Excel $xlfile -PassThru -AutoNameRange
|
||||
"@ | Export-Excel $xlfile -PassThru -AutoNameRange
|
||||
|
||||
$ws = $excel.Workbook.Worksheets["Sheet1"]
|
||||
$ws = $excel.Workbook.Worksheets["Sheet1"]
|
||||
|
||||
$ws.Names.Count | Should Be 2
|
||||
$ws.Names[0].Name | Should Be 'Sold'
|
||||
$ws.Names[1].Name | Should Be 'ID'
|
||||
}
|
||||
$ws.Names.Count | Should Be 2
|
||||
$ws.Names[0].Name | Should Be 'Sold'
|
||||
$ws.Names[1].Name | Should Be 'ID'
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Table Formatting" {
|
||||
|
||||
Reference in New Issue
Block a user