mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Add helper method
This commit is contained in:
@@ -26,7 +26,6 @@ function Read-OleDbData {
|
|||||||
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=file.xlsx;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'"
|
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=file.xlsx;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'"
|
||||||
}
|
}
|
||||||
$Results = Read-OleDbData @ReadDataArgs
|
$Results = Read-OleDbData @ReadDataArgs
|
||||||
|
|
||||||
#>
|
#>
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)]
|
[Parameter(Mandatory)]
|
||||||
@@ -61,7 +60,7 @@ function Read-OleDbData {
|
|||||||
# and get rid of the extra fields that come back with the datatable or rows.
|
# and get rid of the extra fields that come back with the datatable or rows.
|
||||||
|
|
||||||
$DataTable = new-object System.Data.DataTable
|
$DataTable = new-object System.Data.DataTable
|
||||||
$DataAdapter = new-object System.Data.OleDb.OleDbDataAdapter $SqlStatement,$ConnectionString
|
$DataAdapter = new-object System.Data.OleDb.OleDbDataAdapter $SqlStatement, $ConnectionString
|
||||||
$null = $DataAdapter.Fill($DataTable)
|
$null = $DataAdapter.Fill($DataTable)
|
||||||
$null = $DataAdapter.Dispose()
|
$null = $DataAdapter.Dispose()
|
||||||
$DataTable.Rows | Select-Object $DataTable.Columns.ColumnName
|
$DataTable.Rows | Select-Object $DataTable.Columns.ColumnName
|
||||||
|
|||||||
47
Public/Read-XlsxUsingOleDb.ps1
Normal file
47
Public/Read-XlsxUsingOleDb.ps1
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#Requires -Version 5
|
||||||
|
function Read-XlsxUsingOleDb {
|
||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Helper method for executing Read-OleDbData with some basic defaults.
|
||||||
|
|
||||||
|
For additional help, see documentation for Read-OleDbData cmdlet.
|
||||||
|
|
||||||
|
.DESCRIPTION
|
||||||
|
Uses Read-OleDbData to execute a sql statement against a xlsx file. For finer grained control over the interaction, you may use that cmdlet. This cmdlet assumes a file path will be passed in and the connection string will be built with no headers and treating all results as text.
|
||||||
|
|
||||||
|
Running this command is equivalent to running the following:
|
||||||
|
|
||||||
|
$FullName = (Get-ChildItem $Path).FullName
|
||||||
|
Read-OleDbData `
|
||||||
|
-ConnectionString "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$FullName;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" `
|
||||||
|
-SqlStatement $Query
|
||||||
|
|
||||||
|
.EXAMPLE
|
||||||
|
Read-XlsxUsingOleDb .\test.xlsx 'select ROUND(F1) as [A1] from [sheet3$A1:A1]'
|
||||||
|
|
||||||
|
.EXAMPLE
|
||||||
|
$Path = (Get-ChildItem 'test.xlsx').FullName
|
||||||
|
$Query = "select ROUND(F1) as [A] from [sheet1$A1:A1]"
|
||||||
|
Read-XlsxUsingOleDb -Path $Path -Query $Query
|
||||||
|
|
||||||
|
.EXAMPLE
|
||||||
|
$ReadDataArgs = @{
|
||||||
|
Path = .\test.xlsx
|
||||||
|
Query = Get-Content query.sql -Raw
|
||||||
|
}
|
||||||
|
$Results = Read-XlsxUsingOleDb @ReadDataArgs
|
||||||
|
#>
|
||||||
|
param(
|
||||||
|
#The path to the file to open.
|
||||||
|
[Parameter(Mandatory)]
|
||||||
|
[ValidateNotNullOrEmpty()]
|
||||||
|
[String] $Path, # var name consistent with Import-Excel
|
||||||
|
[Parameter(Mandatory)]
|
||||||
|
[ValidateNotNullOrEmpty()]
|
||||||
|
[String] $Query # var name consistent with Invoke-Sqlcmd
|
||||||
|
)
|
||||||
|
$FullName = (Get-ChildItem $Path).FullName
|
||||||
|
Read-OleDbData `
|
||||||
|
-ConnectionString "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$FullName;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" `
|
||||||
|
-SqlStatement $Query
|
||||||
|
}
|
||||||
@@ -47,7 +47,7 @@ Describe "Read-OleDbData" -Tag "Read-OleDbData" {
|
|||||||
It "Should return 7 result with where sum values 1-6 = value 7" {
|
It "Should return 7 result with where sum values 1-6 = value 7" {
|
||||||
$Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestB.sql" -raw)
|
$Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestB.sql" -raw)
|
||||||
$a = $Results.A1
|
$a = $Results.A1
|
||||||
$a.length + ($a[0..5] | Measure-Object -sum).sum | Should -Be (7+$a[6])
|
$a.length + ($a[0..5] | Measure-Object -sum).sum | Should -Be (7 + $a[6])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Context "Sheet1`$:A1:E10, Sql from file" {
|
Context "Sheet1`$:A1:E10, Sql from file" {
|
||||||
@@ -69,4 +69,4 @@ Describe "Read-OleDbData" -Tag "Read-OleDbData" {
|
|||||||
@($Results).length + @($Results[0].psobject.Properties).length | Should -Be 9
|
@($Results).length + @($Results[0].psobject.Properties).length | Should -Be 9
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
20
__tests__/Read-OleDbDataTests/Read-XlsxUsingOleDb.Tests..ps1
Normal file
20
__tests__/Read-OleDbDataTests/Read-XlsxUsingOleDb.Tests..ps1
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#Requires -Modules Pester
|
||||||
|
$scriptPath = $PSScriptRoot
|
||||||
|
Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force
|
||||||
|
$tfp = "$scriptPath\Read-OleDbData.xlsx"
|
||||||
|
$ACEnotWorking = $false
|
||||||
|
try {
|
||||||
|
$Results = Read-XlsxUsingOleDb $tfp "select 1"
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$ACEnotWorking = $true
|
||||||
|
}
|
||||||
|
Describe "Read-XlsxUsingOleDb" -Tag "Read-XlsxUsingOleDb" {
|
||||||
|
$PSDefaultParameterValues = @{ 'It:Skip' = $ACEnotWorking }
|
||||||
|
Context "Sheet1`$A1" {
|
||||||
|
It "Should return 1 result with a value of 1" {
|
||||||
|
$Results = Read-XlsxUsingOleDb $tfp "select ROUND(F1) as [A1] from [sheet1`$A1:A1]"
|
||||||
|
@($Results).length + $Results.A1 | Should -Be 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user