Merge branch 'master' of https://github.com/dfinke/ImportExcel into adding-to-import-excel

This commit is contained in:
dfinke
2022-04-30 07:25:58 -04:00
10 changed files with 187 additions and 4 deletions

View File

@@ -0,0 +1,64 @@
$xlfile = "$env:temp\test.xlsm"
Remove-Item $xlfile -ErrorAction SilentlyContinue
ConvertFrom-Csv @"
Region,Item,TotalSold
West,screwdriver,98
West,kiwi,19
North,kiwi,47
West,screws,48
West,avocado,52
East,avocado,40
South,drill,61
North,orange,92
South,drill,29
South,saw,36
"@ | Export-Excel $xlfile -TableName 'Sales' -WorksheetName 'Sales' -AutoSize
$Excel = ConvertFrom-Csv @"
Supplier,Item,TotalBought
Hardware,screwdriver,98
Groceries,kiwi,19
Hardware,screws,48
Groceries,avocado,52
Hardware,drill,61
Groceries,orange,92
Hardware,drill,29
HArdware,saw,36
"@ | Export-Excel $xlfile -TableName 'Purchases' -WorksheetName 'Purchases' -PassThru -AutoSize
$wb = $Excel.Workbook
$wb.CreateVBAProject()
# Create a module with a sub to highlight the selected row & column of the active table.
# https://docs.microsoft.com/en-gb/office/vba/excel/Concepts/Cells-and-Ranges/highlight-the-active-cell-row-or-column
$codeModule = @"
Public Sub HighlightSelection(ByVal Target As Range)
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
With ActiveCell
' Highlight the row and column that contain the active cell, within the current region
Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Interior.ColorIndex = 38
Range(Cells(.CurrentRegion.Row, .Column), Cells(.CurrentRegion.Rows.Count + .CurrentRegion.Row - 1, .Column)).Interior.ColorIndex = 24
End With
Application.ScreenUpdating = True
End Sub
"@
$module = $wb.VbaProject.Modules.AddModule("PSExcelModule")
$module.Code = $codeModule
# Add a call to the row & column highlight sub on each worksheet.
$codeSheet = @"
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
HighlightSelection Target
End Sub
"@
foreach ($sheet in $wb.Worksheets) {
$sheet.CodeModule.Code = $codeSheet
}
Close-ExcelPackage $Excel -Show

View File

@@ -0,0 +1,41 @@
$xlfile = "$env:temp\test.xlsm"
Remove-Item $xlfile -ErrorAction SilentlyContinue
$Excel = ConvertFrom-Csv @"
Region,Item,TotalSold
West,screwdriver,98
West,kiwi,19
North,kiwi,47
West,screws,48
West,avocado,52
East,avocado,40
South,drill,61
North,orange,92
South,drill,29
South,saw,36
"@ | Export-Excel $xlfile -TableName 'Sales' -WorksheetName 'Sales' -AutoSize -PassThru
$wb = $Excel.Workbook
$sheet = $wb.Worksheets["Sales"]
$wb.CreateVBAProject()
# Add a sub to the 'Worksheet_SelectionChange' event of the worksheet to highlight the selected row & column of the active table.
# https://docs.microsoft.com/en-gb/office/vba/excel/Concepts/Cells-and-Ranges/highlight-the-active-cell-row-or-column
$code = @"
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
With ActiveCell
' Highlight the row and column that contain the active cell, within the current region
Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Interior.ColorIndex = 38
Range(Cells(.CurrentRegion.Row, .Column), Cells(.CurrentRegion.Rows.Count + .CurrentRegion.Row - 1, .Column)).Interior.ColorIndex = 24
End With
Application.ScreenUpdating = True
End Sub
"@
$sheet.CodeModule.Code = $code
Close-ExcelPackage $Excel -Show

View File

@@ -6,7 +6,7 @@
RootModule = 'ImportExcel.psm1'
# Version number of this module.
ModuleVersion = '7.4.1'
ModuleVersion = '7.5.0'
# ID used to uniquely identify this module
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'

View File

@@ -16,12 +16,15 @@ function ConvertFrom-ExcelToSQLInsert {
[switch]$NoHeader,
[switch]$DataOnly,
[switch]$ConvertEmptyStringsToNull,
[switch]$UseMsSqlSyntax
[switch]$UseMsSqlSyntax,
[Parameter(Mandatory = $false)]
$SingleQuoteStyle
)
$null = $PSBoundParameters.Remove('TableName')
$null = $PSBoundParameters.Remove('ConvertEmptyStringsToNull')
$null = $PSBoundParameters.Remove('UseMsSqlSyntax')
$null = $PSBoundParameters.Remove('SingleQuoteStyle')
$params = @{} + $PSBoundParameters
@@ -38,11 +41,16 @@ function ConvertFrom-ExcelToSQLInsert {
'NULL'
}
else {
"'" + $record.$propertyName + "'"
if ( $SingleQuoteStyle ) {
"'" + $record.$propertyName.ToString().Replace("'",${SingleQuoteStyle}) + "'"
}
else {
"'" + $record.$propertyName + "'"
}
}
}
$targetValues = ($values -join ", ")
"INSERT INTO {0} ({1}) Values({2});" -f $TableName, $ColumnNames, $targetValues
}
}
}

View File

@@ -233,6 +233,8 @@
}
catch { throw "Failed importing the Excel workbook '$Path' with worksheet '$WorksheetName': $_"; return }
finally {
$EndRow = 0
$EndColumn = 0
if ($Path) { $stream.close(); $ExcelPackage.Dispose() }
if ($NotAsDictionary) {

View File

@@ -234,6 +234,7 @@ Describe "Import-Excel on a sheet with no headings" {
$actual.Count | Should -Be 1
Remove-Item $xlfile
# Looks like -DataOnly does not handle empty columns
# $actual[0].FirstName | Should -BeExactly 'Jean-Claude'
# $actual[0].SecondName | Should -BeExactly 'Vandamme'

View File

@@ -0,0 +1,57 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'False Positives')]
Param()
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force
Describe "Test reading multiple XLSX files of differernt row count" -Tag ReadMultipleXLSX {
It "Should find these xlsx files" {
Test-Path -Path $PSScriptRoot\rows05.xlsx | Should -BeTrue
Test-Path -Path $PSScriptRoot\rows10.xlsx | Should -BeTrue
}
It "Should find two xlsx files" {
(Get-ChildItem $PSScriptRoot\row*xlsx).Count | Should -Be 2
}
It "Should get 5 rows" {
(Import-Excel $PSScriptRoot\rows05.xlsx).Count | Should -Be 5
}
It "Should get 10 rows" {
(Import-Excel $PSScriptRoot\rows10.xlsx).Count | Should -Be 10
}
It "Should get 15 rows" {
$actual = Get-ChildItem $PSScriptRoot\row*xlsx | Import-Excel
$actual.Count | Should -Be 15
}
It "Should get 4 property names" {
$actual = Get-ChildItem $PSScriptRoot\row*xlsx | Import-Excel
$names = $actual[0].psobject.properties.name
$names.Count | Should -Be 4
$names[0] | Should -BeExactly "Region"
$names[1] | Should -BeExactly "State"
$names[2] | Should -BeExactly "Units"
$names[3] | Should -BeExactly "Price"
}
It "Should have the correct data" {
$actual = Get-ChildItem $PSScriptRoot\row*xlsx | Import-Excel
# rows05.xlsx
$actual[0].Region | Should -BeExactly "South"
$actual[0].Price | Should -Be 181.52
$actual[4].Region | Should -BeExactly "West"
$actual[4].Price | Should -Be 216.56
# rows10.xlsx
$actual[5].Region | Should -BeExactly "South"
$actual[5].Price | Should -Be 199.85
$actual[14].Region | Should -BeExactly "East"
$actual[14].Price | Should -Be 965.25
}
}

Binary file not shown.

Binary file not shown.

View File

@@ -1,3 +1,13 @@
# v7.5.0
## Fixes
- Importing multiple files with Import-Excel by pipeline uses only the first file for the row count https://github.com/dfinke/ImportExcel/issues/1172
# v7.4.2
- Thank you [James Mueller](https://github.com/jamesmmueller) Updated `ConvertFrom-ExcelToSQLInsert` to handle single quotes in the SQL statement.
- Thank you to Josh Hendricks
- Add images to spreadsheets. [Check it out](https://github.com/dfinke/ImportExcel/tree/master/Examples/AddImage)
- Catch up with him on [GitHub](https://github.com/joshooaj) and [Twitter](https://twitter.com/joshooaj) for the idea