Add helper method

This commit is contained in:
Roy Ashbrook
2021-11-03 10:35:22 -04:00
parent 91a1240408
commit 5e7b404daf
4 changed files with 70 additions and 4 deletions

View File

@@ -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;'"
}
$Results = Read-OleDbData @ReadDataArgs
#>
param(
[Parameter(Mandatory)]
@@ -61,7 +60,7 @@ function Read-OleDbData {
# and get rid of the extra fields that come back with the datatable or rows.
$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.Dispose()
$DataTable.Rows | Select-Object $DataTable.Columns.ColumnName

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

View File

@@ -47,7 +47,7 @@ Describe "Read-OleDbData" -Tag "Read-OleDbData" {
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)
$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" {
@@ -69,4 +69,4 @@ Describe "Read-OleDbData" -Tag "Read-OleDbData" {
@($Results).length + @($Results[0].psobject.Properties).length | Should -Be 9
}
}
}
}

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