From a942f2133dfc0ee480e5dcd8208311a3a4dce37d Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Fri, 29 Oct 2021 13:21:52 -0400 Subject: [PATCH 01/24] Add basic function, tests, and sample file --- Public/Read-OleDbData.TestA.sql | 4 ++ Public/Read-OleDbData.TestB.sql | 7 ++ Public/Read-OleDbData.TestC.sql | 4 ++ Public/Read-OleDbData.TestD.sql | 10 +++ Public/Read-OleDbData.TestE.sql | 31 +++++++++ Public/Read-OleDbData.Tests.ps1 | 113 ++++++++++++++++++++++++++++++++ Public/Read-OleDbData.ps1 | 68 +++++++++++++++++++ Public/Read-OleDbData.xlsx | Bin 0 -> 16588 bytes 8 files changed, 237 insertions(+) create mode 100644 Public/Read-OleDbData.TestA.sql create mode 100644 Public/Read-OleDbData.TestB.sql create mode 100644 Public/Read-OleDbData.TestC.sql create mode 100644 Public/Read-OleDbData.TestD.sql create mode 100644 Public/Read-OleDbData.TestE.sql create mode 100644 Public/Read-OleDbData.Tests.ps1 create mode 100644 Public/Read-OleDbData.ps1 create mode 100644 Public/Read-OleDbData.xlsx diff --git a/Public/Read-OleDbData.TestA.sql b/Public/Read-OleDbData.TestA.sql new file mode 100644 index 0000000..576c6af --- /dev/null +++ b/Public/Read-OleDbData.TestA.sql @@ -0,0 +1,4 @@ +select + ROUND(F1) as [A1] +from + [sheet3$A1:A1] \ No newline at end of file diff --git a/Public/Read-OleDbData.TestB.sql b/Public/Read-OleDbData.TestB.sql new file mode 100644 index 0000000..9334b90 --- /dev/null +++ b/Public/Read-OleDbData.TestB.sql @@ -0,0 +1,7 @@ +select ROUND(F1) as [A1] from [sheet1$A1:A1] +union all select ROUND(F1) as [A1] from [sheet2$A1:A1] +union all select ROUND(F1) as [A1] from [sheet3$A1:A1] +union all select ROUND(F1) as [A1] from [sheet4$A1:A1] +union all select ROUND(F1) as [A1] from [sheet5$A1:A1] +union all select ROUND(F1) as [A1] from [sheet6$A1:A1] +union all select ROUND(F1) as [A1] from [sheet7$A1:A1] \ No newline at end of file diff --git a/Public/Read-OleDbData.TestC.sql b/Public/Read-OleDbData.TestC.sql new file mode 100644 index 0000000..9e3f5f7 --- /dev/null +++ b/Public/Read-OleDbData.TestC.sql @@ -0,0 +1,4 @@ +select + * +from + [sheet1$A1:E10] diff --git a/Public/Read-OleDbData.TestD.sql b/Public/Read-OleDbData.TestD.sql new file mode 100644 index 0000000..efb169c --- /dev/null +++ b/Public/Read-OleDbData.TestD.sql @@ -0,0 +1,10 @@ +select top 1 + 'All A1s' as [A1], + F1 as [Sheet1], + (select F1 FROM [sheet2$a1:a1]) as [Sheet2], + (select F1 FROM [sheet3$a1:a1]) as [Sheet3], + (select F1 FROM [sheet4$a1:a1]) as [Sheet4], + (select F1 FROM [sheet5$a1:a1]) as [Sheet5], + (select F1 FROM [sheet6$a1:a1]) as [Sheet6], + (select F1 FROM [sheet7$a1:a1]) as [Sheet7] +FROM [sheet1$a1:a1] \ No newline at end of file diff --git a/Public/Read-OleDbData.TestE.sql b/Public/Read-OleDbData.TestE.sql new file mode 100644 index 0000000..bd98cfa --- /dev/null +++ b/Public/Read-OleDbData.TestE.sql @@ -0,0 +1,31 @@ +select top 1 + 'All A1s Start from Sheet1' as [A1], + F1 as [Sheet1], + (select F1 FROM [sheet2$a1:a1]) as [Sheet2], + (select F1 FROM [sheet3$a1:a1]) as [Sheet3], + (select F1 FROM [sheet4$a1:a1]) as [Sheet4] +FROM [sheet1$a1:a1] +UNION ALL +select top 1 + 'All A1s Start from Sheet2' as [A1], + (select F1 FROM [sheet1$a1:a1]) as [Sheet1], + F1 as [Sheet2], + (select F1 FROM [sheet3$a1:a1]) as [Sheet3], + (select F1 FROM [sheet4$a1:a1]) as [Sheet4] +FROM [sheet2$a1:a1] +UNION ALL +select top 1 + 'All A1s Start from Sheet3' as [A1], + (select F1 FROM [sheet1$a1:a1]) as [Sheet1], + (select F1 FROM [sheet2$a1:a1]) as [Sheet2], + F1 as [Sheet3], + (select F1 FROM [sheet4$a1:a1]) as [Sheet4] +FROM [sheet3$a1:a1] +UNION ALL +select top 1 + 'All A1s Start from Sheet4' as [A1], + (select F1 FROM [sheet1$a1:a1]) as [Sheet1], + (select F1 FROM [sheet2$a1:a1]) as [Sheet2], + (select F1 FROM [sheet3$a1:a1]) as [Sheet3], + F1 as [Sheet4] +FROM [sheet4$a1:a1] diff --git a/Public/Read-OleDbData.Tests.ps1 b/Public/Read-OleDbData.Tests.ps1 new file mode 100644 index 0000000..c2cf65b --- /dev/null +++ b/Public/Read-OleDbData.Tests.ps1 @@ -0,0 +1,113 @@ +. .\Read-OleDbData.ps1 +Describe "Read-OleDbData" { + BeforeAll{ + $tfp = (Get-ChildItem Read-OleDbData.xlsx).fullname # test file path + $cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" + } + Context "When Read-OleDbData.xlsx and we want sheet1 a1" { + BeforeAll{ + $sql = "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + } + It "should be PSCustomObject" { + $Results.GetType().Name | Should -Be 'PSCustomObject' + } + It "should have length of 1" { + @($Results).length | Should -Be 1 + } + It "should be value of 1" { + $Results.A1 | Should -Be 1 + } + } + Context "When Read-OleDbData.xlsx and we want sheet2 a1" { + BeforeAll{ + $sql = "select ROUND(F1) as [A1] from [sheet2`$A1:A1]" + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + } + It "should be PSCustomObject" { + $Results.GetType().Name | Should -Be 'PSCustomObject' + } + It "should have length of 1" { + @($Results).length | Should -Be 1 + } + It "should be value of 2" { + $Results.A1 | Should -Be 2 + } + } + Context "When Read-OleDbData.xlsx and we want a1 on sheet3 and sql is in a file" { + BeforeAll{ + $sql = Get-Content .\Read-OleDbData.TestA.sql -raw + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + } + It "should be PSCustomObject" { + $Results.GetType().Name | Should -Be 'PSCustomObject' + } + It "should have length of 1" { + @($Results).length | Should -Be 1 + } + It "should be value of 2" { + $Results.A1 | Should -Be 3 + } + } + Context "When Read-OleDbData.xlsx, we want a1 on sheets1-7, want to validate the values match properly, and sql is in a file" { + BeforeAll{ + $sql = Get-Content .\Read-OleDbData.TestB.sql -raw + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + } + It "should be PSCustomObject" { + $Results[0].GetType().Name | Should -Be 'PSCustomObject' + } + It "should have length of 7" { + @($Results).length | Should -Be 7 + } + It "should have data where sum of all initial records match the value of the last record" { + $a = $Results.A1 + ($a[0..5] | Measure-Object -sum).sum | Should -Be $a[6] + } + } + Context "When Read-OleDbData.xlsx, select range sheet1 A1:E10, and sql is in a file" { + #note, this spreadsheet doesn't have the fields populated other than A1, so it will, correctly, return only one value + BeforeAll{ + $sql = Get-Content .\Read-OleDbData.TestC.sql -raw + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + } + It "should be PSCustomObject" { + $Results.GetType().Name | Should -Be 'PSCustomObject' + } + It "should have length of 1" { + @($Results).length | Should -Be 1 + } + } + Context "When Read-OleDbData.xlsx, select a1 from all sheets as a single record, and sql is in a file" { + #note, this spreadsheet doesn't have the fields populated other than A1, so it will, correctly, return only one value + BeforeAll{ + $sql = Get-Content .\Read-OleDbData.TestD.sql -raw + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + } + It "should be PSCustomObject" { + $Results.GetType().Name | Should -Be 'PSCustomObject' + } + It "should have length of 1" { + @($Results).length | Should -Be 1 + } + It "should have 8 properties" { + @($Results.psobject.Properties).length | Should -Be 8 + } + } + Context "When Read-OleDbData.xlsx, select a1 from all sheets as a single record multiple times to create a range, and sql is in a file" { + #note, this spreadsheet doesn't have the fields populated other than A1, so it will, correctly, return only one value + BeforeAll{ + $sql = Get-Content .\Read-OleDbData.TestE.sql -raw + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + } + It "should be Object[]" { + $Results.GetType().Name | Should -Be 'Object[]' + } + It "should have length of 4" { + @($Results).length | Should -Be 4 + } + It "should have 5 properties on first record" { + @($Results[0].psobject.Properties).length | Should -Be 5 + } + } +} diff --git a/Public/Read-OleDbData.ps1 b/Public/Read-OleDbData.ps1 new file mode 100644 index 0000000..579d904 --- /dev/null +++ b/Public/Read-OleDbData.ps1 @@ -0,0 +1,68 @@ +#Requires -Version 5 +function Read-OleDbData { + <# + .SYNOPSIS + Read data from an OleDb source using dotnet classes. This allows for OleDb queries against excel spreadsheets. Examples will only be for querying xlsx files. + + For additional documentation, see Microsoft's documentation on the System.Data OleDb namespace here: + https://docs.microsoft.com/en-us/dotnet/api/system.data.oledb + + .DESCRIPTION + Read data from an OleDb source using dotnet classes. This allows for OleDb queries against excel spreadsheets. Examples will only be for querying xlsx files using ACE. + + .EXAMPLE + Read-OleDbData ` + -ConnectionString "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=file.xlsx;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" ` + -SqlStatement "select ROUND(F1) as [A] from [sheet1$A1:A1]" + + .EXAMPLE + $ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=file.xlsx;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" + $SqlStatement = "select ROUND(F1) as [A] from [sheet1$A1:A1]" + Read-OleDbData -ConnectionString $ConnectionString -SqlStatement $SqlStatement + + .EXAMPLE + $ReadDataArgs = @{ + SqlStatement = Get-Content query.sql -Raw + 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)] + [ValidateNotNullOrEmpty()] + [String] $ConnectionString, + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [String] $SqlStatement + ) + + if ($IsLinux -or $IsMacOS) { + #todo: possibly add support for linux/mac somehow. i haven't researched this at all as i + # don't have a need for that and i'm not sure anyone else would in this context, but it does + # appear that once upon a time mono had support for oledb, so maybe it is (or was) supported. + # mono link here: https://www.mono-project.com/archived/ole_db/ + Write-Error "Read-OleDbData only runs on Windows" + return + } + + #todo: add checks for dotnet libs + #todo: possibly add checks for ace drivers, but maybe only needed if we want to restrict usage. + # i currently just pass through the query and connection string so user is only limited by + # their own machine setup, but they have to check for those dependencies themselves. + #todo: possibly try/catch. i personally do not do this, as i let them throw naturally and catch + # them/handle them outside of this function. + #todo: possibly allow for DataSets instead of just DataTable. I used to use a similar method before + # switching to the sqlcmd module and use datasets as I had multiple tables coming back from + # sql sometimes. i think in this case, it's best to just keep it simple, but maybe someone + # out there would prefer to be able to get multiple tables back. i have not tested that + # with the OleDbDataAdapter. + #todo: possibly just return the datatable, i do it as below because i prefer to simplify the output + # 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 + $null = $DataAdapter.Fill($DataTable) + $null = $DataAdapter.Dispose() + $DataTable.Rows | Select-Object $DataTable.Columns.ColumnName +} \ No newline at end of file diff --git a/Public/Read-OleDbData.xlsx b/Public/Read-OleDbData.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..5458ccfb403ef5417928af40c4dff8fe476cbc0c GIT binary patch literal 16588 zcmeHuWl)^kwk;Ceogl&8-AT~k8rzQaW@I9S zaxILP1aU#>Qt<(E{chr;RNP3!Jz#|K+)mmtIro0Z1x$vXpYow1Hj=FyBtA0x>1E@6 zm*0WHD@^FnvfAd~a&S!$Vn*lp42gB$ko@9PoEcG)p4THX#FyeKY(^FT&7R;WDmA7@ zW2c``$w&T&RHU;s=e|YzksMlNPl~OhdHd0+ES|41^Py|=O=1Kyg|{3$4T9?)6NZQ^ zr)2h8R+hZ8(MkF~lTTPib}b^+JZ~COSww7M!;%=Zr(79aoR^T>hS-R!U5}iR4dUes zn-`TW7jPgT_xIo+a({8)Y9&UJQy?p)fc^;wbYLBOBTEMc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Lp!C3@$N#&c@Ypl|K4|lAfj)M=zYi+K_&Ym&>|cK$ zboRGEAG^@s2Q_EPAUpm64tN+?`{ag#2&tV1u`CAM2bopP4 j=zm_$#`aIk|17EHBq4xx00_tn;9nu|Do%LLhgbgxu1 Date: Fri, 29 Oct 2021 16:58:16 -0400 Subject: [PATCH 02/24] move tests into test folder --- {Public => __tests__}/Read-OleDbData.TestA.sql | 0 {Public => __tests__}/Read-OleDbData.TestB.sql | 0 {Public => __tests__}/Read-OleDbData.TestC.sql | 0 {Public => __tests__}/Read-OleDbData.TestD.sql | 0 {Public => __tests__}/Read-OleDbData.TestE.sql | 0 {Public => __tests__}/Read-OleDbData.Tests.ps1 | 1 - {Public => __tests__}/Read-OleDbData.xlsx | Bin 7 files changed, 1 deletion(-) rename {Public => __tests__}/Read-OleDbData.TestA.sql (100%) rename {Public => __tests__}/Read-OleDbData.TestB.sql (100%) rename {Public => __tests__}/Read-OleDbData.TestC.sql (100%) rename {Public => __tests__}/Read-OleDbData.TestD.sql (100%) rename {Public => __tests__}/Read-OleDbData.TestE.sql (100%) rename {Public => __tests__}/Read-OleDbData.Tests.ps1 (99%) rename {Public => __tests__}/Read-OleDbData.xlsx (100%) diff --git a/Public/Read-OleDbData.TestA.sql b/__tests__/Read-OleDbData.TestA.sql similarity index 100% rename from Public/Read-OleDbData.TestA.sql rename to __tests__/Read-OleDbData.TestA.sql diff --git a/Public/Read-OleDbData.TestB.sql b/__tests__/Read-OleDbData.TestB.sql similarity index 100% rename from Public/Read-OleDbData.TestB.sql rename to __tests__/Read-OleDbData.TestB.sql diff --git a/Public/Read-OleDbData.TestC.sql b/__tests__/Read-OleDbData.TestC.sql similarity index 100% rename from Public/Read-OleDbData.TestC.sql rename to __tests__/Read-OleDbData.TestC.sql diff --git a/Public/Read-OleDbData.TestD.sql b/__tests__/Read-OleDbData.TestD.sql similarity index 100% rename from Public/Read-OleDbData.TestD.sql rename to __tests__/Read-OleDbData.TestD.sql diff --git a/Public/Read-OleDbData.TestE.sql b/__tests__/Read-OleDbData.TestE.sql similarity index 100% rename from Public/Read-OleDbData.TestE.sql rename to __tests__/Read-OleDbData.TestE.sql diff --git a/Public/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbData.Tests.ps1 similarity index 99% rename from Public/Read-OleDbData.Tests.ps1 rename to __tests__/Read-OleDbData.Tests.ps1 index c2cf65b..c0b10bc 100644 --- a/Public/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbData.Tests.ps1 @@ -1,4 +1,3 @@ -. .\Read-OleDbData.ps1 Describe "Read-OleDbData" { BeforeAll{ $tfp = (Get-ChildItem Read-OleDbData.xlsx).fullname # test file path diff --git a/Public/Read-OleDbData.xlsx b/__tests__/Read-OleDbData.xlsx similarity index 100% rename from Public/Read-OleDbData.xlsx rename to __tests__/Read-OleDbData.xlsx From e8c3d96f43c89a1c8f8a618eb010cc432cb0e7b4 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Fri, 29 Oct 2021 21:58:05 -0400 Subject: [PATCH 03/24] Tests relocated and updated. All passing. --- .../Read-OleDbData.TestA.sql | 0 .../Read-OleDbData.TestB.sql | 0 .../Read-OleDbData.TestC.sql | 0 .../Read-OleDbData.TestD.sql | 0 .../Read-OleDbData.TestE.sql | 0 .../Read-OleDbData.Tests.ps1 | 42 ++++++++---------- .../Read-OleDbData.xlsx | Bin 7 files changed, 19 insertions(+), 23 deletions(-) rename __tests__/{ => Read-OleDbDataTests}/Read-OleDbData.TestA.sql (100%) rename __tests__/{ => Read-OleDbDataTests}/Read-OleDbData.TestB.sql (100%) rename __tests__/{ => Read-OleDbDataTests}/Read-OleDbData.TestC.sql (100%) rename __tests__/{ => Read-OleDbDataTests}/Read-OleDbData.TestD.sql (100%) rename __tests__/{ => Read-OleDbDataTests}/Read-OleDbData.TestE.sql (100%) rename __tests__/{ => Read-OleDbDataTests}/Read-OleDbData.Tests.ps1 (81%) rename __tests__/{ => Read-OleDbDataTests}/Read-OleDbData.xlsx (100%) diff --git a/__tests__/Read-OleDbData.TestA.sql b/__tests__/Read-OleDbDataTests/Read-OleDbData.TestA.sql similarity index 100% rename from __tests__/Read-OleDbData.TestA.sql rename to __tests__/Read-OleDbDataTests/Read-OleDbData.TestA.sql diff --git a/__tests__/Read-OleDbData.TestB.sql b/__tests__/Read-OleDbDataTests/Read-OleDbData.TestB.sql similarity index 100% rename from __tests__/Read-OleDbData.TestB.sql rename to __tests__/Read-OleDbDataTests/Read-OleDbData.TestB.sql diff --git a/__tests__/Read-OleDbData.TestC.sql b/__tests__/Read-OleDbDataTests/Read-OleDbData.TestC.sql similarity index 100% rename from __tests__/Read-OleDbData.TestC.sql rename to __tests__/Read-OleDbDataTests/Read-OleDbData.TestC.sql diff --git a/__tests__/Read-OleDbData.TestD.sql b/__tests__/Read-OleDbDataTests/Read-OleDbData.TestD.sql similarity index 100% rename from __tests__/Read-OleDbData.TestD.sql rename to __tests__/Read-OleDbDataTests/Read-OleDbData.TestD.sql diff --git a/__tests__/Read-OleDbData.TestE.sql b/__tests__/Read-OleDbDataTests/Read-OleDbData.TestE.sql similarity index 100% rename from __tests__/Read-OleDbData.TestE.sql rename to __tests__/Read-OleDbDataTests/Read-OleDbData.TestE.sql diff --git a/__tests__/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 similarity index 81% rename from __tests__/Read-OleDbData.Tests.ps1 rename to __tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index c0b10bc..7c2b141 100644 --- a/__tests__/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -1,10 +1,13 @@ -Describe "Read-OleDbData" { - BeforeAll{ - $tfp = (Get-ChildItem Read-OleDbData.xlsx).fullname # test file path +#Requires -Modules Pester +Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force +Describe "All tests for Read-OleDbData" -Tag "Read-OleDbData" { + BeforeAll { + $scriptPath = $PSScriptRoot + $tfp = "$scriptPath\Read-OleDbData.xlsx" $cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" } Context "When Read-OleDbData.xlsx and we want sheet1 a1" { - BeforeAll{ + BeforeAll { $sql = "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql } @@ -19,7 +22,7 @@ Describe "Read-OleDbData" { } } Context "When Read-OleDbData.xlsx and we want sheet2 a1" { - BeforeAll{ + BeforeAll { $sql = "select ROUND(F1) as [A1] from [sheet2`$A1:A1]" $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql } @@ -34,9 +37,8 @@ Describe "Read-OleDbData" { } } Context "When Read-OleDbData.xlsx and we want a1 on sheet3 and sql is in a file" { - BeforeAll{ - $sql = Get-Content .\Read-OleDbData.TestA.sql -raw - $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + BeforeAll { + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestA.sql" -raw) } It "should be PSCustomObject" { $Results.GetType().Name | Should -Be 'PSCustomObject' @@ -49,9 +51,8 @@ Describe "Read-OleDbData" { } } Context "When Read-OleDbData.xlsx, we want a1 on sheets1-7, want to validate the values match properly, and sql is in a file" { - BeforeAll{ - $sql = Get-Content .\Read-OleDbData.TestB.sql -raw - $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + BeforeAll { + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestB.sql" -raw) } It "should be PSCustomObject" { $Results[0].GetType().Name | Should -Be 'PSCustomObject' @@ -65,23 +66,20 @@ Describe "Read-OleDbData" { } } Context "When Read-OleDbData.xlsx, select range sheet1 A1:E10, and sql is in a file" { - #note, this spreadsheet doesn't have the fields populated other than A1, so it will, correctly, return only one value - BeforeAll{ - $sql = Get-Content .\Read-OleDbData.TestC.sql -raw - $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + BeforeAll { + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestC.sql" -raw) } It "should be PSCustomObject" { $Results.GetType().Name | Should -Be 'PSCustomObject' } + #note, this spreadsheet doesn't have the fields populated other than A1, so it will, correctly, return only one value It "should have length of 1" { @($Results).length | Should -Be 1 } } Context "When Read-OleDbData.xlsx, select a1 from all sheets as a single record, and sql is in a file" { - #note, this spreadsheet doesn't have the fields populated other than A1, so it will, correctly, return only one value - BeforeAll{ - $sql = Get-Content .\Read-OleDbData.TestD.sql -raw - $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + BeforeAll { + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestD.sql" -raw) } It "should be PSCustomObject" { $Results.GetType().Name | Should -Be 'PSCustomObject' @@ -94,10 +92,8 @@ Describe "Read-OleDbData" { } } Context "When Read-OleDbData.xlsx, select a1 from all sheets as a single record multiple times to create a range, and sql is in a file" { - #note, this spreadsheet doesn't have the fields populated other than A1, so it will, correctly, return only one value - BeforeAll{ - $sql = Get-Content .\Read-OleDbData.TestE.sql -raw - $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql + BeforeAll { + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestE.sql" -raw) } It "should be Object[]" { $Results.GetType().Name | Should -Be 'Object[]' diff --git a/__tests__/Read-OleDbData.xlsx b/__tests__/Read-OleDbDataTests/Read-OleDbData.xlsx similarity index 100% rename from __tests__/Read-OleDbData.xlsx rename to __tests__/Read-OleDbDataTests/Read-OleDbData.xlsx From 91a124040890cdfc3083e1cf0d88f0c30ead9802 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Sat, 30 Oct 2021 11:20:27 -0400 Subject: [PATCH 04/24] add skip test check for ace --- .../Read-OleDbData.Tests.ps1 | 120 ++++++------------ 1 file changed, 42 insertions(+), 78 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 7c2b141..6af536e 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -1,108 +1,72 @@ #Requires -Modules Pester Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force -Describe "All tests for Read-OleDbData" -Tag "Read-OleDbData" { - BeforeAll { - $scriptPath = $PSScriptRoot - $tfp = "$scriptPath\Read-OleDbData.xlsx" - $cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" +$scriptPath = $PSScriptRoot +$tfp = "$scriptPath\Read-OleDbData.xlsx" +$cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" +$ACEnotWorking = $false +try { + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement "select 1" +} +catch { + $ACEnotWorking = $true +} +Describe "Read-OleDbData" -Tag "Read-OleDbData" { + $PSDefaultParameterValues = @{ 'It:Skip' = $ACEnotWorking } + Context "Basic Tests" { + It "should be able to open spreadsheet" { + $null = Read-OleDbData -ConnectionString $cs -SqlStatement "select 1" + $true | Should -Be $true + } + It "should return PSCustomObject for single result" { + #multiple records will come back as Object[], but not going to test for that + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement "select 1" + $Results.GetType().Name | Should -Be 'PSCustomObject' + } } - Context "When Read-OleDbData.xlsx and we want sheet1 a1" { - BeforeAll { + Context "Sheet1`$A1" { + It "Should return 1 result with a value of 1" { $sql = "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql - } - It "should be PSCustomObject" { - $Results.GetType().Name | Should -Be 'PSCustomObject' - } - It "should have length of 1" { - @($Results).length | Should -Be 1 - } - It "should be value of 1" { - $Results.A1 | Should -Be 1 + @($Results).length + $Results.A1 | Should -Be 2 } } - Context "When Read-OleDbData.xlsx and we want sheet2 a1" { - BeforeAll { + Context "Sheet2`$A1" { + It "Should return 1 result with value of 2" { $sql = "select ROUND(F1) as [A1] from [sheet2`$A1:A1]" $Results = Read-OleDbData -ConnectionString $cs -SqlStatement $sql - } - It "should be PSCustomObject" { - $Results.GetType().Name | Should -Be 'PSCustomObject' - } - It "should have length of 1" { - @($Results).length | Should -Be 1 - } - It "should be value of 2" { - $Results.A1 | Should -Be 2 + @($Results).length + $Results.A1 | Should -Be 3 } } - Context "When Read-OleDbData.xlsx and we want a1 on sheet3 and sql is in a file" { - BeforeAll { + Context "Sheet3`$A1, Sql from file" { + It "Should return 1 result with value of 3" { $Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestA.sql" -raw) - } - It "should be PSCustomObject" { - $Results.GetType().Name | Should -Be 'PSCustomObject' - } - It "should have length of 1" { - @($Results).length | Should -Be 1 - } - It "should be value of 2" { - $Results.A1 | Should -Be 3 + @($Results).length + $Results.A1 | Should -Be 4 } } - Context "When Read-OleDbData.xlsx, we want a1 on sheets1-7, want to validate the values match properly, and sql is in a file" { - BeforeAll { + Context "Sheets[1-7]`$A1, Sql from file" { + 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) - } - It "should be PSCustomObject" { - $Results[0].GetType().Name | Should -Be 'PSCustomObject' - } - It "should have length of 7" { - @($Results).length | Should -Be 7 - } - It "should have data where sum of all initial records match the value of the last record" { $a = $Results.A1 - ($a[0..5] | Measure-Object -sum).sum | Should -Be $a[6] + $a.length + ($a[0..5] | Measure-Object -sum).sum | Should -Be (7+$a[6]) } } - Context "When Read-OleDbData.xlsx, select range sheet1 A1:E10, and sql is in a file" { - BeforeAll { - $Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestC.sql" -raw) - } - It "should be PSCustomObject" { - $Results.GetType().Name | Should -Be 'PSCustomObject' - } + Context "Sheet1`$:A1:E10, Sql from file" { #note, this spreadsheet doesn't have the fields populated other than A1, so it will, correctly, return only one value - It "should have length of 1" { - @($Results).length | Should -Be 1 + It "Should return 1 result with value of 1" { + $Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestC.sql" -raw) + @($Results).length + $Results.F1 | Should -Be 2 } } Context "When Read-OleDbData.xlsx, select a1 from all sheets as a single record, and sql is in a file" { - BeforeAll { + It "should return one row with 8 columns" { $Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestD.sql" -raw) - } - It "should be PSCustomObject" { - $Results.GetType().Name | Should -Be 'PSCustomObject' - } - It "should have length of 1" { - @($Results).length | Should -Be 1 - } - It "should have 8 properties" { - @($Results.psobject.Properties).length | Should -Be 8 + @($Results).length + @($Results.psobject.Properties).length | Should -Be 9 } } Context "When Read-OleDbData.xlsx, select a1 from all sheets as a single record multiple times to create a range, and sql is in a file" { - BeforeAll { + It "should return 4 records with 5 columns" { $Results = Read-OleDbData -ConnectionString $cs -SqlStatement (Get-Content "$scriptPath\Read-OleDbData.TestE.sql" -raw) - } - It "should be Object[]" { - $Results.GetType().Name | Should -Be 'Object[]' - } - It "should have length of 4" { - @($Results).length | Should -Be 4 - } - It "should have 5 properties on first record" { - @($Results[0].psobject.Properties).length | Should -Be 5 + @($Results).length + @($Results[0].psobject.Properties).length | Should -Be 9 } } } From 5e7b404dafe6c47c636002e9cda9ffc7322a63e6 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Wed, 3 Nov 2021 10:35:22 -0400 Subject: [PATCH 05/24] Add helper method --- Public/Read-OleDbData.ps1 | 3 +- Public/Read-XlsxUsingOleDb.ps1 | 47 +++++++++++++++++++ .../Read-OleDbData.Tests.ps1 | 4 +- .../Read-XlsxUsingOleDb.Tests..ps1 | 20 ++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 Public/Read-XlsxUsingOleDb.ps1 create mode 100644 __tests__/Read-OleDbDataTests/Read-XlsxUsingOleDb.Tests..ps1 diff --git a/Public/Read-OleDbData.ps1 b/Public/Read-OleDbData.ps1 index 579d904..ccd0fdc 100644 --- a/Public/Read-OleDbData.ps1 +++ b/Public/Read-OleDbData.ps1 @@ -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 diff --git a/Public/Read-XlsxUsingOleDb.ps1 b/Public/Read-XlsxUsingOleDb.ps1 new file mode 100644 index 0000000..0d75f41 --- /dev/null +++ b/Public/Read-XlsxUsingOleDb.ps1 @@ -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 +} \ No newline at end of file diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 6af536e..1abb368 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -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 } } -} +} \ No newline at end of file diff --git a/__tests__/Read-OleDbDataTests/Read-XlsxUsingOleDb.Tests..ps1 b/__tests__/Read-OleDbDataTests/Read-XlsxUsingOleDb.Tests..ps1 new file mode 100644 index 0000000..d88ec4b --- /dev/null +++ b/__tests__/Read-OleDbDataTests/Read-XlsxUsingOleDb.Tests..ps1 @@ -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 + } + } +} From 85f2433ffc99b240c6e75b119b731c0a0be7d279 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Thu, 4 Nov 2021 10:44:14 -0400 Subject: [PATCH 06/24] Rename helper function to make it more helpful. =) --- Public/{Read-XlsxUsingOleDb.ps1 => Invoke-ExcelQuery.ps1} | 8 ++++---- ...xUsingOleDb.Tests..ps1 => Invoke-ExcelQuery.Tests.ps1} | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) rename Public/{Read-XlsxUsingOleDb.ps1 => Invoke-ExcelQuery.ps1} (89%) rename __tests__/Read-OleDbDataTests/{Read-XlsxUsingOleDb.Tests..ps1 => Invoke-ExcelQuery.Tests.ps1} (67%) diff --git a/Public/Read-XlsxUsingOleDb.ps1 b/Public/Invoke-ExcelQuery.ps1 similarity index 89% rename from Public/Read-XlsxUsingOleDb.ps1 rename to Public/Invoke-ExcelQuery.ps1 index 0d75f41..00fb4a0 100644 --- a/Public/Read-XlsxUsingOleDb.ps1 +++ b/Public/Invoke-ExcelQuery.ps1 @@ -1,5 +1,5 @@ #Requires -Version 5 -function Read-XlsxUsingOleDb { +function Invoke-ExcelQuery { <# .SYNOPSIS Helper method for executing Read-OleDbData with some basic defaults. @@ -17,7 +17,7 @@ function Read-XlsxUsingOleDb { -SqlStatement $Query .EXAMPLE - Read-XlsxUsingOleDb .\test.xlsx 'select ROUND(F1) as [A1] from [sheet3$A1:A1]' + Invoke-ExcelQuery .\test.xlsx 'select ROUND(F1) as [A1] from [sheet3$A1:A1]' .EXAMPLE $Path = (Get-ChildItem 'test.xlsx').FullName @@ -29,7 +29,7 @@ function Read-XlsxUsingOleDb { Path = .\test.xlsx Query = Get-Content query.sql -Raw } - $Results = Read-XlsxUsingOleDb @ReadDataArgs + $Results = Invoke-ExcelQuery @ReadDataArgs #> param( #The path to the file to open. @@ -41,7 +41,7 @@ function Read-XlsxUsingOleDb { [String] $Query # var name consistent with Invoke-Sqlcmd ) $FullName = (Get-ChildItem $Path).FullName - Read-OleDbData ` + Invoke-ExcelQuery ` -ConnectionString "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$FullName;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" ` -SqlStatement $Query } \ No newline at end of file diff --git a/__tests__/Read-OleDbDataTests/Read-XlsxUsingOleDb.Tests..ps1 b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 similarity index 67% rename from __tests__/Read-OleDbDataTests/Read-XlsxUsingOleDb.Tests..ps1 rename to __tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 index d88ec4b..3d0842f 100644 --- a/__tests__/Read-OleDbDataTests/Read-XlsxUsingOleDb.Tests..ps1 +++ b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 @@ -4,16 +4,16 @@ Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force $tfp = "$scriptPath\Read-OleDbData.xlsx" $ACEnotWorking = $false try { - $Results = Read-XlsxUsingOleDb $tfp "select 1" + $Results = Invoke-ExcelQuery $tfp "select 1" } catch { $ACEnotWorking = $true } -Describe "Read-XlsxUsingOleDb" -Tag "Read-XlsxUsingOleDb" { +Describe "Invoke-ExcelQuery" -Tag "Invoke-ExcelQuery" { $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 = Invoke-ExcelQuery $tfp "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" @($Results).length + $Results.A1 | Should -Be 2 } } From 7a8bbb97710256a4f2eb01b0d234e0671ae3ac3b Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Thu, 4 Nov 2021 20:54:51 -0400 Subject: [PATCH 07/24] add to exported functions, formatting, fix typo. --- ImportExcel.psd1 | 2 ++ Public/Invoke-ExcelQuery.ps1 | 2 +- __tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index 8a267c3..8143435 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -64,6 +64,7 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5 'Import-USPS', 'Invoke-AllTests', 'Invoke-Sum', + 'Invoke-ExcelQuery', 'Join-Worksheet', 'LineChart', 'Merge-MultipleSheets', @@ -80,6 +81,7 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5 'Pivot', 'Read-Clipboard', 'ReadClipboardImpl', + 'Read-OleDbData', 'Remove-Worksheet', 'Select-Worksheet', 'Send-SQLDataToExcel', diff --git a/Public/Invoke-ExcelQuery.ps1 b/Public/Invoke-ExcelQuery.ps1 index 00fb4a0..f799a11 100644 --- a/Public/Invoke-ExcelQuery.ps1 +++ b/Public/Invoke-ExcelQuery.ps1 @@ -41,7 +41,7 @@ function Invoke-ExcelQuery { [String] $Query # var name consistent with Invoke-Sqlcmd ) $FullName = (Get-ChildItem $Path).FullName - Invoke-ExcelQuery ` + Read-OleDbData ` -ConnectionString "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$FullName;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" ` -SqlStatement $Query } \ No newline at end of file diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 1abb368..500772f 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -1,9 +1,9 @@ #Requires -Modules Pester -Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force $scriptPath = $PSScriptRoot +Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force $tfp = "$scriptPath\Read-OleDbData.xlsx" -$cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" $ACEnotWorking = $false +$cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" try { $Results = Read-OleDbData -ConnectionString $cs -SqlStatement "select 1" } From eabaab79c5e4431d522a8d0542f8549947a5747a Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 14:43:57 -0500 Subject: [PATCH 08/24] add ace check --- Public/Invoke-ExcelQuery.ps1 | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Public/Invoke-ExcelQuery.ps1 b/Public/Invoke-ExcelQuery.ps1 index f799a11..3b8817e 100644 --- a/Public/Invoke-ExcelQuery.ps1 +++ b/Public/Invoke-ExcelQuery.ps1 @@ -16,6 +16,10 @@ function Invoke-ExcelQuery { -ConnectionString "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$FullName;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" ` -SqlStatement $Query + Note that this command uses the MICROSOFT.ACE.OLEDB provider and will not work without it. + + If needed, please download the appropriate package from https://www.microsoft.com/en-us/download/details.aspx?id=54920. + .EXAMPLE Invoke-ExcelQuery .\test.xlsx 'select ROUND(F1) as [A1] from [sheet3$A1:A1]' @@ -40,6 +44,13 @@ function Invoke-ExcelQuery { [ValidateNotNullOrEmpty()] [String] $Query # var name consistent with Invoke-Sqlcmd ) + + $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") + if($IsMissingACE){ + Write-Error "MICROSOFT.ACE.OLEDB is missing! Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" + return + } + $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;'" ` From aa1b0018abc216b56ab7c9ea514cfc90a108d18c Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 15:14:46 -0500 Subject: [PATCH 09/24] Add examples --- Examples/InvokeExcelQuery/Examples.ps1 | 13 +++++++++++++ Examples/InvokeExcelQuery/testOleDb.xlsx | Bin 0 -> 2955 bytes 2 files changed, 13 insertions(+) create mode 100644 Examples/InvokeExcelQuery/Examples.ps1 create mode 100644 Examples/InvokeExcelQuery/testOleDb.xlsx diff --git a/Examples/InvokeExcelQuery/Examples.ps1 b/Examples/InvokeExcelQuery/Examples.ps1 new file mode 100644 index 0000000..c565292 --- /dev/null +++ b/Examples/InvokeExcelQuery/Examples.ps1 @@ -0,0 +1,13 @@ +try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return} + +$queries = + 'select * from [sheet1$A:A]', + 'select * from [sheet1$]', + 'select * from [sheet1$A2:E11]', + 'select F2,F5 from [sheet1$A2:E11]', + 'select * from [sheet1$A2:E11] where F2 = "Grocery"', + 'select F2 as [Category], F5 as [Discount], F5*2 as [DiscountPlus] from [sheet1$A2:E11]' + +foreach ($query in $queries) { + Invoke-ExcelQuery .\testOleDb.xlsx $query| Format-Table +} diff --git a/Examples/InvokeExcelQuery/testOleDb.xlsx b/Examples/InvokeExcelQuery/testOleDb.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..944751c9ec1543e296e4688272d7a6b35e089c93 GIT binary patch literal 2955 zcmZ{m2{@E%8^=eOtTpzXP$$g6Oty|BTWF-lU^=o)WNc$8GX|4{VvucYW6%0kiex`Z znGA8#XD3TYc12kdlbm-t-}RY%-?`uGn&+Bp=6SC7{@wrk`QJzjb`D_>7w~A9xLHq` zmpu|^1A#QyK_DRz2xM>U=S#r)5->KwSFm_TwICnwn&eIdI~>xA!M59GRm++bkI0tF z+E(Z{WpogYR}(v`YyJGRp$a7tvZSoT;~g8@TpAeJr?@W}?X6w0nK`lGn+dH^me8?* z#1#8(h%s!!r3o!fq5H!zazZF;H};xB_j7>=%2GFbdj?Fn_Y#aA~&QE`IrD0QAricdaWlRLfb-gJgw-)Mm$RXJ5Gv?v5#56E{K) znUp=ZpqYP9^RON@v3+3Txq{1}UHmT#x$26^C{tLUVr5_@dY%>qU*kM>DqOvMVWQ8h zV`M|TuF^}4O5i-M;e(!c4^I-~p`w$#wi+(?W4 zT&(zzKY`5{Uc6ln06hx81$l36FnTMUW@1iulfxf|DZC0Vur1z?;}{)&wZ!$SiDyktTVa?8 zd!KD^%b8Y!;1-QDollBm4c;I$Yq);=RT9NGTU%z+u2qxNoNH60*|rfJI`O?XtgKy9 zwMnM|G*EtE=kfhTaHKmbQNrh&)#o(x$~ACN7{R`t13_s7mstSMin2HmM-bmdGk9Q__T-+tMA<&RSBN2(#I7opss^$r$(C_Yy?tYit6(?-3;pZ za{aq3$DlU**oFi(y5(+K@A9u5Q=@9lbyp5)orv`s8r=plnXqR5Gm~(a5g{wJib!Tg?f>&C#^1oOY?o?bJ z`I0_Xv@NaNmdkZLVY4mIoJSaM<}#t{vhA2viZmRj+UcR9u#$gboGe4Po=@J9lU#e12?eX7~XwlMX9`pXuO@tb(inGVi!RAn{$r;61Qd0$%;c0{cPYmQXK_|o^a@ak`j zRh?^J-`>d)D`-7;z@93wpzCsPx;1)k{oN^+z5Rtr>m_2%Q=6%#d4hMnddgaBPb4nkYC8KQI}tWy ziGov&=?f2hudIIPnqSZ%nZ>v*>Q`E}XDkKX!s|CXePBaSM>LzFva3=!fh<*+}C0@6nfrWfzgA|#vKxU?#e6!mKvGAl_i zZx!Lg+(!I-HaBFn9FWF`4nn*<3JTqTAGg?IP`7wE1%`#O9{H64eIADKmo#okzC9f| z#CD!ryFa?<68B9)u7=a`%k^$_iSg?U*oipv2q%Oy-Krvv-z+7d$5q}^e=PUORCy6B zn;{U><-)6E{(0%VeifsaQ!8>@&&X2MCgdE=TU6-jb6)Yp$M^3WN|vP@+tTM(a?p|G z=!f$@^0l{lw1|4a)4!Oq*w42L5owTF?@ zff=>QVVGfkd@HKwk3-1{BR+KpGY>nwD@7JPJaW8m!OAe^x+_}b>;vA^_Z(lR4iXez zCb2W`lQ{j($0CF)(`9-j&H;2`$FV(|D8yK$RK9XA3;l7KveN&#E@Qzeja@)Jh(K+G z|5FzaXMe1#HNoH0*ZoI%ut~IY9P$v88ys8)Lex`a}IIZD=XKLq2C19wpXWZ98;2^#xUAs12%&j$4L)*deG# zqpPlavke<}#g?JR=fsv}njMBer=EJkLEyQACQApy#!a{86KW7dnyI}`lW*JrX zZe%q*rrqy`0nj&l%zy7@R!?C1;%?Xg( Date: Mon, 8 Nov 2021 15:25:26 -0500 Subject: [PATCH 10/24] update tests to match --- .../Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 | 11 ++++------- .../Read-OleDbDataTests/Read-OleDbData.Tests.ps1 | 11 ++++------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 index 3d0842f..4f428e2 100644 --- a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 @@ -2,15 +2,12 @@ $scriptPath = $PSScriptRoot Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force $tfp = "$scriptPath\Read-OleDbData.xlsx" -$ACEnotWorking = $false -try { - $Results = Invoke-ExcelQuery $tfp "select 1" -} -catch { - $ACEnotWorking = $true +$IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") +if($IsMissingACE){ + Write-Host "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped. Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" } Describe "Invoke-ExcelQuery" -Tag "Invoke-ExcelQuery" { - $PSDefaultParameterValues = @{ 'It:Skip' = $ACEnotWorking } + $PSDefaultParameterValues = @{ 'It:Skip' = $IsMissingACE } Context "Sheet1`$A1" { It "Should return 1 result with a value of 1" { $Results = Invoke-ExcelQuery $tfp "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 500772f..db8ff73 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -2,16 +2,13 @@ $scriptPath = $PSScriptRoot Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force $tfp = "$scriptPath\Read-OleDbData.xlsx" -$ACEnotWorking = $false $cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" -try { - $Results = Read-OleDbData -ConnectionString $cs -SqlStatement "select 1" -} -catch { - $ACEnotWorking = $true +$IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") +if($IsMissingACE){ + Write-Host "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped. Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" } Describe "Read-OleDbData" -Tag "Read-OleDbData" { - $PSDefaultParameterValues = @{ 'It:Skip' = $ACEnotWorking } + $PSDefaultParameterValues = @{ 'It:Skip' = $IsMissingACE } Context "Basic Tests" { It "should be able to open spreadsheet" { $null = Read-OleDbData -ConnectionString $cs -SqlStatement "select 1" From 5ec299ff2d5b546c77407a81cb5f4c3d613dcb3d Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 15:43:36 -0500 Subject: [PATCH 11/24] some debugging messages for troubleshooting CI --- __tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index db8ff73..70e9a8b 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -7,6 +7,10 @@ $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetEle if($IsMissingACE){ Write-Host "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped. Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" } +write-host "`$tfp = '$tfp'" +write-host "`Test-Path $tfp = '$(Test-Path $tfp)'" +write-host "`$cs = '$cs'" +write-host "`$IsMissingACE = '$IsMissingACE'" Describe "Read-OleDbData" -Tag "Read-OleDbData" { $PSDefaultParameterValues = @{ 'It:Skip' = $IsMissingACE } Context "Basic Tests" { From 1e522c562b89a9a16be974718d2c2042a14bfb5c Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 16:00:06 -0500 Subject: [PATCH 12/24] test skips for linux/macos, switch to warnings --- .../Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 | 8 ++++++-- .../Read-OleDbDataTests/Read-OleDbData.Tests.ps1 | 13 +++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 index 4f428e2..314f154 100644 --- a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 @@ -4,10 +4,14 @@ Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force $tfp = "$scriptPath\Read-OleDbData.xlsx" $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") if($IsMissingACE){ - Write-Host "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped. Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" + Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped. Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" } +Write-Warning "`$tfp = '$tfp'" +Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" +Write-Warning "`$IsMissingACE = '$IsMissingACE'" +$skip = $IsLinux -or $IsMacOS -or $IsMissingACE Describe "Invoke-ExcelQuery" -Tag "Invoke-ExcelQuery" { - $PSDefaultParameterValues = @{ 'It:Skip' = $IsMissingACE } + $PSDefaultParameterValues = @{ 'It:Skip' = $skip } Context "Sheet1`$A1" { It "Should return 1 result with a value of 1" { $Results = Invoke-ExcelQuery $tfp "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 70e9a8b..3697b35 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -5,14 +5,15 @@ $tfp = "$scriptPath\Read-OleDbData.xlsx" $cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") if($IsMissingACE){ - Write-Host "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped. Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" + Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped. Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" } -write-host "`$tfp = '$tfp'" -write-host "`Test-Path $tfp = '$(Test-Path $tfp)'" -write-host "`$cs = '$cs'" -write-host "`$IsMissingACE = '$IsMissingACE'" +Write-Warning "`$tfp = '$tfp'" +Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" +Write-Warning "`$cs = '$cs'" +Write-Warning "`$IsMissingACE = '$IsMissingACE'" +$skip = $IsLinux -or $IsMacOS -or $IsMissingACE Describe "Read-OleDbData" -Tag "Read-OleDbData" { - $PSDefaultParameterValues = @{ 'It:Skip' = $IsMissingACE } + $PSDefaultParameterValues = @{ 'It:Skip' = $skip } Context "Basic Tests" { It "should be able to open spreadsheet" { $null = Read-OleDbData -ConnectionString $cs -SqlStatement "select 1" From a3dced10e4ecc15bc3ff5b19e820927668380b13 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 16:37:00 -0500 Subject: [PATCH 13/24] update tests --- .../Invoke-ExcelQuery.Tests.ps1 | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 index 314f154..8c62e95 100644 --- a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 @@ -2,16 +2,37 @@ $scriptPath = $PSScriptRoot Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force $tfp = "$scriptPath\Read-OleDbData.xlsx" -$IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") -if($IsMissingACE){ - Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped. Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" +$skip = $IsLinux -or $IsMacOS #init default, not supported on mac or linux +try { + $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") + if ($IsMissingACE) { + Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped." + } + $skip = $skip -or $IsMissingACE } +catch { + Write-Warning "Unable to get sources from System.Data.OleDb. Tests will be skipped." + $skip = $true #this will fail if the call to get the sources fails, usually means System.Data.OleDb isn't installed/supported +} + + Write-Warning "`$tfp = '$tfp'" Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" Write-Warning "`$IsMissingACE = '$IsMissingACE'" -$skip = $IsLinux -or $IsMacOS -or $IsMissingACE + Describe "Invoke-ExcelQuery" -Tag "Invoke-ExcelQuery" { $PSDefaultParameterValues = @{ 'It:Skip' = $skip } + Context "Basic Checks" { + It "Should have a valid Test file" { + Test-Path $tfp | Should -Be $true + } + It "Should have the Read-OleDbData command loaded" { + (Get-Command Read-OleDbData) -ne $null | Should -Be $true + } + It "Should have the Invoke-ExcelQuery command loaded" { + (Get-Command Invoke-ExcelQuery) -ne $null | Should -Be $true + } + } Context "Sheet1`$A1" { It "Should return 1 result with a value of 1" { $Results = Invoke-ExcelQuery $tfp "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" From ebe49f1650d8b18cc16a41adb2d28815849c8e27 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 16:56:23 -0500 Subject: [PATCH 14/24] fix or/and --- __tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 | 2 +- __tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 index 8c62e95..6bf6951 100644 --- a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 @@ -8,7 +8,7 @@ try { if ($IsMissingACE) { Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped." } - $skip = $skip -or $IsMissingACE + $skip = $skip -and $IsMissingACE } catch { Write-Warning "Unable to get sources from System.Data.OleDb. Tests will be skipped." diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 3697b35..6611a4d 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -11,7 +11,7 @@ Write-Warning "`$tfp = '$tfp'" Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" Write-Warning "`$cs = '$cs'" Write-Warning "`$IsMissingACE = '$IsMissingACE'" -$skip = $IsLinux -or $IsMacOS -or $IsMissingACE +$skip = ($IsLinux -or $IsMacOS) -and $IsMissingACE Describe "Read-OleDbData" -Tag "Read-OleDbData" { $PSDefaultParameterValues = @{ 'It:Skip' = $skip } Context "Basic Tests" { From d41fbf8a05da0a03c049ead66a9e801fa9afb3bb Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 17:13:40 -0500 Subject: [PATCH 15/24] updating tests --- .../Invoke-ExcelQuery.Tests.ps1 | 78 +++++++++---------- .../Read-OleDbData.Tests.ps1 | 42 ++++++---- 2 files changed, 67 insertions(+), 53 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 index 6bf6951..2b02de9 100644 --- a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 @@ -1,42 +1,42 @@ -#Requires -Modules Pester -$scriptPath = $PSScriptRoot -Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force -$tfp = "$scriptPath\Read-OleDbData.xlsx" -$skip = $IsLinux -or $IsMacOS #init default, not supported on mac or linux -try { - $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") - if ($IsMissingACE) { - Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped." - } - $skip = $skip -and $IsMissingACE -} -catch { - Write-Warning "Unable to get sources from System.Data.OleDb. Tests will be skipped." - $skip = $true #this will fail if the call to get the sources fails, usually means System.Data.OleDb isn't installed/supported -} +# #Requires -Modules Pester +# $scriptPath = $PSScriptRoot +# Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force +# $tfp = "$scriptPath\Read-OleDbData.xlsx" +# $skip = $IsLinux -or $IsMacOS #init default, not supported on mac or linux +# try { +# $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") +# if ($IsMissingACE) { +# Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped." +# } +# $skip = $skip -and $IsMissingACE +# } +# catch { +# Write-Warning "Unable to get sources from System.Data.OleDb. Tests will be skipped." +# $skip = $true #this will fail if the call to get the sources fails, usually means System.Data.OleDb isn't installed/supported +# } -Write-Warning "`$tfp = '$tfp'" -Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" -Write-Warning "`$IsMissingACE = '$IsMissingACE'" +# Write-Warning "`$tfp = '$tfp'" +# Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" +# Write-Warning "`$IsMissingACE = '$IsMissingACE'" -Describe "Invoke-ExcelQuery" -Tag "Invoke-ExcelQuery" { - $PSDefaultParameterValues = @{ 'It:Skip' = $skip } - Context "Basic Checks" { - It "Should have a valid Test file" { - Test-Path $tfp | Should -Be $true - } - It "Should have the Read-OleDbData command loaded" { - (Get-Command Read-OleDbData) -ne $null | Should -Be $true - } - It "Should have the Invoke-ExcelQuery command loaded" { - (Get-Command Invoke-ExcelQuery) -ne $null | Should -Be $true - } - } - Context "Sheet1`$A1" { - It "Should return 1 result with a value of 1" { - $Results = Invoke-ExcelQuery $tfp "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" - @($Results).length + $Results.A1 | Should -Be 2 - } - } -} +# Describe "Invoke-ExcelQuery" -Tag "Invoke-ExcelQuery" { +# $PSDefaultParameterValues = @{ 'It:Skip' = $skip } +# Context "Basic Checks" { +# It "Should have a valid Test file" { +# Test-Path $tfp | Should -Be $true +# } +# It "Should have the Read-OleDbData command loaded" { +# (Get-Command Read-OleDbData) -ne $null | Should -Be $true +# } +# It "Should have the Invoke-ExcelQuery command loaded" { +# (Get-Command Invoke-ExcelQuery) -ne $null | Should -Be $true +# } +# } +# Context "Sheet1`$A1" { +# It "Should return 1 result with a value of 1" { +# $Results = Invoke-ExcelQuery $tfp "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" +# @($Results).length + $Results.A1 | Should -Be 2 +# } +# } +# } diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 6611a4d..9302801 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -1,25 +1,39 @@ #Requires -Modules Pester -$scriptPath = $PSScriptRoot -Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force -$tfp = "$scriptPath\Read-OleDbData.xlsx" -$cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" -$IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") -if($IsMissingACE){ - Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped. Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" +Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force +$skip = $true +try { + $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") + if ($IsMissingACE) { + Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped." + } + $skip = $IsMissingACE } -Write-Warning "`$tfp = '$tfp'" -Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" -Write-Warning "`$cs = '$cs'" -Write-Warning "`$IsMissingACE = '$IsMissingACE'" -$skip = ($IsLinux -or $IsMacOS) -and $IsMissingACE +catch { + Write-Warning "Unable to get sources from System.Data.OleDb. Tests will be skipped." +} + Describe "Read-OleDbData" -Tag "Read-OleDbData" { $PSDefaultParameterValues = @{ 'It:Skip' = $skip } + BeforeAll{ + $scriptPath = $PSScriptRoot + $tfp = "$scriptPath\Read-OleDbData.xlsx" + $cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" + Write-Warning "`$tfp = '$tfp'" + Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" + Write-Warning "`$cs = '$cs'" + } Context "Basic Tests" { - It "should be able to open spreadsheet" { + It "Should have a valid Test file" { + Test-Path $tfp | Should -Be $true + } + It "Should have the Read-OleDbData command loaded" { + (Get-Command Read-OleDbData) -ne $null | Should -Be $true + } + It "Should be able to open spreadsheet" { $null = Read-OleDbData -ConnectionString $cs -SqlStatement "select 1" $true | Should -Be $true } - It "should return PSCustomObject for single result" { + It "Should return PSCustomObject for single result" { #multiple records will come back as Object[], but not going to test for that $Results = Read-OleDbData -ConnectionString $cs -SqlStatement "select 1" $Results.GetType().Name | Should -Be 'PSCustomObject' From 5f00f5cbbc40993f20835f08be6227748a9caf00 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 17:40:33 -0500 Subject: [PATCH 16/24] update tests --- .../Read-OleDbData.Tests.ps1 | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 9302801..8031ba8 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -1,33 +1,43 @@ #Requires -Modules Pester -Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force -$skip = $true +if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) { + Import-Module $PSScriptRoot\..\ImportExcel.psd1 +} + +$skip = $false +if ($IsLinux -or $IsMacOS) { + $skip = $true + Write-Warning "Read-OleDbData: Linux and MacOs are not supported. Skipping tests." +} try { - $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") - if ($IsMissingACE) { - Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped." + if ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -notcontains "Microsoft.ACE.OLEDB.12.0") { + $skip = $true + Write-Warning "Read-OleDbData: Microsoft.ACE.OLEDB.12.0 provider not found. Skipping tests." } $skip = $IsMissingACE } catch { - Write-Warning "Unable to get sources from System.Data.OleDb. Tests will be skipped." + $skip = $true + Write-Warning "Read-OleDbData: Calls to System.Data.OleDb failed. Skipping tests." } Describe "Read-OleDbData" -Tag "Read-OleDbData" { $PSDefaultParameterValues = @{ 'It:Skip' = $skip } - BeforeAll{ + BeforeAll { $scriptPath = $PSScriptRoot $tfp = "$scriptPath\Read-OleDbData.xlsx" $cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" - Write-Warning "`$tfp = '$tfp'" - Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" - Write-Warning "`$cs = '$cs'" + if (!$skip) { + Write-Warning "`$tfp = '$tfp'" + Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" + Write-Warning "`$cs = '$cs'" + } } Context "Basic Tests" { It "Should have a valid Test file" { Test-Path $tfp | Should -Be $true } It "Should have the Read-OleDbData command loaded" { - (Get-Command Read-OleDbData) -ne $null | Should -Be $true + (Get-Command Read-OleDbData -ErrorAction SilentlyContinue) -ne $null | Should -Be $true } It "Should be able to open spreadsheet" { $null = Read-OleDbData -ConnectionString $cs -SqlStatement "select 1" From f0b4cb28f612855335cacb63c877fc4e8076c525 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 17:44:03 -0500 Subject: [PATCH 17/24] update tests to skip other pre-checks if linux/mac --- .../Read-OleDbData.Tests.ps1 | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 8031ba8..8b7f07d 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -7,17 +7,18 @@ $skip = $false if ($IsLinux -or $IsMacOS) { $skip = $true Write-Warning "Read-OleDbData: Linux and MacOs are not supported. Skipping tests." -} -try { - if ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -notcontains "Microsoft.ACE.OLEDB.12.0") { - $skip = $true - Write-Warning "Read-OleDbData: Microsoft.ACE.OLEDB.12.0 provider not found. Skipping tests." +}else{ + try { + if ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -notcontains "Microsoft.ACE.OLEDB.12.0") { + $skip = $true + Write-Warning "Read-OleDbData: Microsoft.ACE.OLEDB.12.0 provider not found. Skipping tests." + } + $skip = $IsMissingACE + } + catch { + $skip = $true + Write-Warning "Read-OleDbData: Calls to System.Data.OleDb failed. Skipping tests." } - $skip = $IsMissingACE -} -catch { - $skip = $true - Write-Warning "Read-OleDbData: Calls to System.Data.OleDb failed. Skipping tests." } Describe "Read-OleDbData" -Tag "Read-OleDbData" { From 9daefc2925b12e64a4b7b65eafca5d4dbf712555 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 17:45:33 -0500 Subject: [PATCH 18/24] fix typo in skip logic --- __tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 8b7f07d..8ad8ff5 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -13,7 +13,6 @@ if ($IsLinux -or $IsMacOS) { $skip = $true Write-Warning "Read-OleDbData: Microsoft.ACE.OLEDB.12.0 provider not found. Skipping tests." } - $skip = $IsMissingACE } catch { $skip = $true From 9ba20b8ec8245ce440c2de7ce884f223b2b11736 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 17:57:44 -0500 Subject: [PATCH 19/24] add back invoke tests, sync skip methods --- .../Invoke-ExcelQuery.Tests.ps1 | 84 ++++++++++--------- .../Read-OleDbData.Tests.ps1 | 6 -- 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 index 2b02de9..89405d8 100644 --- a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 @@ -1,42 +1,46 @@ -# #Requires -Modules Pester -# $scriptPath = $PSScriptRoot -# Import-Module $scriptPath\..\..\ImportExcel.psd1 -Force -# $tfp = "$scriptPath\Read-OleDbData.xlsx" -# $skip = $IsLinux -or $IsMacOS #init default, not supported on mac or linux -# try { -# $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") -# if ($IsMissingACE) { -# Write-Warning "MICROSOFT.ACE.OLEDB is missing! Tests will be skipped." -# } -# $skip = $skip -and $IsMissingACE -# } -# catch { -# Write-Warning "Unable to get sources from System.Data.OleDb. Tests will be skipped." -# $skip = $true #this will fail if the call to get the sources fails, usually means System.Data.OleDb isn't installed/supported -# } +#Requires -Modules Pester +if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) { + Import-Module $PSScriptRoot\..\ImportExcel.psd1 +} + +$skip = $false +if ($IsLinux -or $IsMacOS) { + $skip = $true + Write-Warning "Read-OleDbData: Linux and MacOs are not supported. Skipping tests." +}else{ + try { + if ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -notcontains "Microsoft.ACE.OLEDB.12.0") { + $skip = $true + Write-Warning "Read-OleDbData: Microsoft.ACE.OLEDB.12.0 provider not found. Skipping tests." + } + } + catch { + $skip = $true + Write-Warning "Read-OleDbData: Calls to System.Data.OleDb failed. Skipping tests." + } +} -# Write-Warning "`$tfp = '$tfp'" -# Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" -# Write-Warning "`$IsMissingACE = '$IsMissingACE'" - -# Describe "Invoke-ExcelQuery" -Tag "Invoke-ExcelQuery" { -# $PSDefaultParameterValues = @{ 'It:Skip' = $skip } -# Context "Basic Checks" { -# It "Should have a valid Test file" { -# Test-Path $tfp | Should -Be $true -# } -# It "Should have the Read-OleDbData command loaded" { -# (Get-Command Read-OleDbData) -ne $null | Should -Be $true -# } -# It "Should have the Invoke-ExcelQuery command loaded" { -# (Get-Command Invoke-ExcelQuery) -ne $null | Should -Be $true -# } -# } -# Context "Sheet1`$A1" { -# It "Should return 1 result with a value of 1" { -# $Results = Invoke-ExcelQuery $tfp "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" -# @($Results).length + $Results.A1 | Should -Be 2 -# } -# } -# } +Describe "Invoke-ExcelQuery" -Tag "Invoke-ExcelQuery" { + $PSDefaultParameterValues = @{ 'It:Skip' = $skip } + BeforeAll { + $tfp = "$PSScriptRoot\Read-OleDbData.xlsx" + } + Context "Basic Checks" { + It "Should have a valid Test file" { + Test-Path $tfp | Should -Be $true + } + It "Should have the Read-OleDbData command loaded" { + (Get-Command Read-OleDbData) -ne $null | Should -Be $true + } + It "Should have the Invoke-ExcelQuery command loaded" { + (Get-Command Invoke-ExcelQuery) -ne $null | Should -Be $true + } + } + Context "Sheet1`$A1" { + It "Should return 1 result with a value of 1" { + $Results = Invoke-ExcelQuery $tfp "select ROUND(F1) as [A1] from [sheet1`$A1:A1]" + @($Results).length + $Results.A1 | Should -Be 2 + } + } +} diff --git a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 index 8ad8ff5..7a9ef5c 100644 --- a/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Read-OleDbData.Tests.ps1 @@ -19,18 +19,12 @@ if ($IsLinux -or $IsMacOS) { Write-Warning "Read-OleDbData: Calls to System.Data.OleDb failed. Skipping tests." } } - Describe "Read-OleDbData" -Tag "Read-OleDbData" { $PSDefaultParameterValues = @{ 'It:Skip' = $skip } BeforeAll { $scriptPath = $PSScriptRoot $tfp = "$scriptPath\Read-OleDbData.xlsx" $cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$tfp;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" - if (!$skip) { - Write-Warning "`$tfp = '$tfp'" - Write-Warning "`Test-Path $tfp = '$(Test-Path $tfp)'" - Write-Warning "`$cs = '$cs'" - } } Context "Basic Tests" { It "Should have a valid Test file" { From ae31e9d39bd5ae6545f19c0e6c7b65d9fc64723a Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 18:08:45 -0500 Subject: [PATCH 20/24] update skip/help msgs in invoke cmd --- Public/Invoke-ExcelQuery.ps1 | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Public/Invoke-ExcelQuery.ps1 b/Public/Invoke-ExcelQuery.ps1 index 3b8817e..4cb01a9 100644 --- a/Public/Invoke-ExcelQuery.ps1 +++ b/Public/Invoke-ExcelQuery.ps1 @@ -45,9 +45,14 @@ function Invoke-ExcelQuery { [String] $Query # var name consistent with Invoke-Sqlcmd ) - $IsMissingACE = $null -eq ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -like "Microsoft.ACE.OLEDB*") - if($IsMissingACE){ - Write-Error "MICROSOFT.ACE.OLEDB is missing! Please see https://www.microsoft.com/en-us/download/details.aspx?id=54920" + try { + if ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -notcontains "Microsoft.ACE.OLEDB.12.0") { + Write-Error "Microsoft.ACE.OLEDB.12.0 provider is missing! Please install from https://www.microsoft.com/en-us/download/details.aspx?id=54920" + return + } + } + catch { + Write-Error "System.Data.OleDb is not working or you are on an unsupported platform." return } From 487cf51abc372a4223f3a0656eb5d86c82c515dc Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 18:19:23 -0500 Subject: [PATCH 21/24] fix test skip label name --- __tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 index 89405d8..46e964d 100644 --- a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 @@ -6,17 +6,17 @@ if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) { $skip = $false if ($IsLinux -or $IsMacOS) { $skip = $true - Write-Warning "Read-OleDbData: Linux and MacOs are not supported. Skipping tests." + Write-Warning "Invoke-ExcelQuery: Linux and MacOs are not supported. Skipping tests." }else{ try { if ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -notcontains "Microsoft.ACE.OLEDB.12.0") { $skip = $true - Write-Warning "Read-OleDbData: Microsoft.ACE.OLEDB.12.0 provider not found. Skipping tests." + Write-Warning "Invoke-ExcelQuery: Microsoft.ACE.OLEDB.12.0 provider not found. Skipping tests." } } catch { $skip = $true - Write-Warning "Read-OleDbData: Calls to System.Data.OleDb failed. Skipping tests." + Write-Warning "Invoke-ExcelQuery: Calls to System.Data.OleDb failed. Skipping tests." } } From 7177623104c63aaf6bdf2bda0416b97e746d5b2c Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Mon, 8 Nov 2021 18:21:11 -0500 Subject: [PATCH 22/24] add silent continue to cmd check tests --- __tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 index 46e964d..e79f96d 100644 --- a/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 +++ b/__tests__/Read-OleDbDataTests/Invoke-ExcelQuery.Tests.ps1 @@ -31,10 +31,10 @@ Describe "Invoke-ExcelQuery" -Tag "Invoke-ExcelQuery" { Test-Path $tfp | Should -Be $true } It "Should have the Read-OleDbData command loaded" { - (Get-Command Read-OleDbData) -ne $null | Should -Be $true + (Get-Command Read-OleDbData -ErrorAction SilentlyContinue) -ne $null | Should -Be $true } It "Should have the Invoke-ExcelQuery command loaded" { - (Get-Command Invoke-ExcelQuery) -ne $null | Should -Be $true + (Get-Command Invoke-ExcelQuery -ErrorAction SilentlyContinue) -ne $null | Should -Be $true } } Context "Sheet1`$A1" { From 4fa6fbda012feb2d830ae19a9ed4c674babe8c03 Mon Sep 17 00:00:00 2001 From: Roy Ashbrook Date: Tue, 9 Nov 2021 09:05:15 -0500 Subject: [PATCH 23/24] update warning msg for Read-OleDbData --- Public/Read-OleDbData.ps1 | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/Public/Read-OleDbData.ps1 b/Public/Read-OleDbData.ps1 index ccd0fdc..491b54c 100644 --- a/Public/Read-OleDbData.ps1 +++ b/Public/Read-OleDbData.ps1 @@ -36,28 +36,15 @@ function Read-OleDbData { [String] $SqlStatement ) - if ($IsLinux -or $IsMacOS) { - #todo: possibly add support for linux/mac somehow. i haven't researched this at all as i - # don't have a need for that and i'm not sure anyone else would in this context, but it does - # appear that once upon a time mono had support for oledb, so maybe it is (or was) supported. - # mono link here: https://www.mono-project.com/archived/ole_db/ - Write-Error "Read-OleDbData only runs on Windows" + try { + if ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -notcontains "Microsoft.ACE.OLEDB.12.0") { + Write-Warning "Microsoft.ACE.OLEDB.12.0 provider is missing! You will not be able to query Excel files without it. Please install from https://www.microsoft.com/en-us/download/details.aspx?id=54920" + } + } + catch { + Write-Error "System.Data.OleDb is not working or you are on an unsupported platform." return } - - #todo: add checks for dotnet libs - #todo: possibly add checks for ace drivers, but maybe only needed if we want to restrict usage. - # i currently just pass through the query and connection string so user is only limited by - # their own machine setup, but they have to check for those dependencies themselves. - #todo: possibly try/catch. i personally do not do this, as i let them throw naturally and catch - # them/handle them outside of this function. - #todo: possibly allow for DataSets instead of just DataTable. I used to use a similar method before - # switching to the sqlcmd module and use datasets as I had multiple tables coming back from - # sql sometimes. i think in this case, it's best to just keep it simple, but maybe someone - # out there would prefer to be able to get multiple tables back. i have not tested that - # with the OleDbDataAdapter. - #todo: possibly just return the datatable, i do it as below because i prefer to simplify the output - # 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 From 1ee9493bc005702f8f983e120d838b4e9df4cd9e Mon Sep 17 00:00:00 2001 From: dfinke Date: Fri, 12 Nov 2021 17:25:45 -0500 Subject: [PATCH 24/24] Minor tweak. Display query executed --- Examples/InvokeExcelQuery/Examples.ps1 | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Examples/InvokeExcelQuery/Examples.ps1 b/Examples/InvokeExcelQuery/Examples.ps1 index c565292..a42da76 100644 --- a/Examples/InvokeExcelQuery/Examples.ps1 +++ b/Examples/InvokeExcelQuery/Examples.ps1 @@ -1,13 +1,14 @@ -try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return} +try { Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 } catch { throw ; return } $queries = - 'select * from [sheet1$A:A]', - 'select * from [sheet1$]', - 'select * from [sheet1$A2:E11]', - 'select F2,F5 from [sheet1$A2:E11]', - 'select * from [sheet1$A2:E11] where F2 = "Grocery"', - 'select F2 as [Category], F5 as [Discount], F5*2 as [DiscountPlus] from [sheet1$A2:E11]' +'select * from [sheet1$A:A]', +'select * from [sheet1$]', +'select * from [sheet1$A2:E11]', +'select F2,F5 from [sheet1$A2:E11]', +'select * from [sheet1$A2:E11] where F2 = "Grocery"', +'select F2 as [Category], F5 as [Discount], F5*2 as [DiscountPlus] from [sheet1$A2:E11]' foreach ($query in $queries) { - Invoke-ExcelQuery .\testOleDb.xlsx $query| Format-Table + "query: $($query)" + Invoke-ExcelQuery .\testOleDb.xlsx $query | Format-Table }