diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index 6651fd4..c8837fd 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -6,7 +6,7 @@ RootModule = 'ImportExcel.psm1' # Version number of this module. - ModuleVersion = '7.1.3' + ModuleVersion = '7.2.0' # ID used to uniquely identify this module GUID = '60dd4136-feff-401a-ba27-a84458c57ede' @@ -78,7 +78,9 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5 'Open-ExcelPackage', 'PieChart', 'Pivot', - 'Remove-Worksheet' + 'Read-Clipboard', + 'ReadClipboardImpl', + 'Remove-Worksheet', 'Select-Worksheet', 'Send-SQLDataToExcel', 'Set-CellStyle', diff --git a/Public/Read-Clipboard.ps1 b/Public/Read-Clipboard.ps1 new file mode 100644 index 0000000..9e20879 --- /dev/null +++ b/Public/Read-Clipboard.ps1 @@ -0,0 +1,76 @@ +function Read-Clipboard { + <# + .SYNOPSIS + Read text from clipboard and pass to either ConvertFrom-Csv or ConvertFrom-Json. + Check out the how to video - https://youtu.be/dv2GOH5sbpA + + .DESCRIPTION + Read text from clipboard. It can read CSV or JSON. Plus, you can specify the delimiter and headers. + + .EXAMPLE + Read-Clipboard # Detects if the clipboard contains CSV, JSON, or Tab delimited data. + + .EXAMPLE + Read-Clipboard -Delimiter '|' # Converts data using a pipe delimiter + + .EXAMPLE + Read-Clipboard -Header 'P1', 'P2', 'P3' # Specify the header columns to be used + + #> + param( + $Delimiter, + $Header + ) + + if ($IsWindows) { + $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem + if ($osInfo.ProductType -eq 1) { + ReadClipboardImpl (Get-Clipboard -Raw) + } + else { + Write-Error "This command is only supported on the desktop." + } + } + else { + Write-Error "This function is only available on Windows desktop" + } +} + +function ReadClipboardImpl { + param( + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [String] $data, + $Delimiter, + $Header + ) + + if (!$PSBoundParameters.ContainsKey('Delimiter') -and !$PSBoundParameters.ContainsKey('Header')) { + try { + ConvertFrom-Json $data + } + catch { + if ($data.indexof(",") -gt -1) { + ConvertFrom-Csv $data + } + else { + ConvertFrom-Csv $data -Delimiter "`t" + } + } + } + else { + $cvtParams = @{ + InputObject = $data + } + + if ($Delimiter) { + $cvtParams.Delimiter = $Delimiter + } + + if ($Header) { + $cvtParams.Header = $Header + } + + ConvertFrom-Csv @cvtParams + } +} \ No newline at end of file diff --git a/__tests__/Read-Clipboard.tests.ps1 b/__tests__/Read-Clipboard.tests.ps1 new file mode 100644 index 0000000..152a9fe --- /dev/null +++ b/__tests__/Read-Clipboard.tests.ps1 @@ -0,0 +1,90 @@ +#Requires -Modules Pester +# if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) { +Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force +# } +Describe "Read Clipboard" -Tag "Read-Clipboard" { + + It 'Should return $null if it cannot detect data format on the clipboard' { + $testData = 'abc' + $actual = ReadClipboardImpl $testData + $actual.Count | Should -Be 0 + $actual | Should -BeNullOrEmpty + } + + It 'Should return converted csv data' { + $testData = @" +Region,State,Units,Price +West,Texas,927,923.71 +North,Tennessee,466,770.67 +East,Florida,520,458.68 +"@ + $actual = ReadClipboardImpl $testData + $actual.count | Should -Be 3 + } + + It 'Should return converted tab delimited data' { + $testData = @" +YEAR PRESIDENT FIRST LADY VICE PRESIDENT +2021- Joseph R. Biden Jill Biden Kamala Harris +2017-2021 Donald J. Trump Melania Trump Mike Pence +2009-2017 Barack Obama Michelle Obama Joseph R. Biden +"@ + $actual = ReadClipboardImpl $testData + $actual.count | Should -Be 3 + } + + It 'Should return converted json data' { + $testData = @" +[ +{ + "YEAR": "2021-", + "PRESIDENT": "Joseph R. Biden", + "FIRST LADY": "Jill Biden", + "VICE PRESIDENT": "Kamala Harris" +}, +{ + "YEAR": "2017-2021", + "PRESIDENT": "Donald J. Trump", + "FIRST LADY": "Melania Trump", + "VICE PRESIDENT": "Mike Pence" +}, +{ + "YEAR": "2009-2017", + "PRESIDENT": "Barack Obama", + "FIRST LADY": "Michelle Obama", + "VICE PRESIDENT": "Joseph R. Biden" +} +] +"@ + $actual = ReadClipboardImpl $testData + $actual.count | Should -Be 3 + } + + It 'Should return converted "|" delimited data' { + $testData = @" +Region|State|Units|Price +West|Texas|927|923.71 +North|Tennessee|466|770.67 +East|Florida|520|458.68 +"@ + $actual = ReadClipboardImpl $testData -Delimiter '|' + $actual.count | Should -Be 3 + } + + It 'Should return converted data with headers' { + $testData = @" +West,Texas,927,923.71 +North,Tennessee,466,770.67 +East,Florida,520,458.68 +"@ + + $actual = ReadClipboardImpl $testData -Header 'P1', 'P2', 'p3', 'P4' + $actual.count | Should -Be 3 + + $propertyNames = $actual[0].psobject.Properties.Name + $propertyNames[0] | Should -BeExactly 'P1' + $propertyNames[1] | Should -BeExactly 'P2' + $propertyNames[2] | Should -BeExactly 'p3' + $propertyNames[3] | Should -BeExactly 'P4' + } +} \ No newline at end of file diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..d97f7a3 --- /dev/null +++ b/changelog.md @@ -0,0 +1,7 @@ + +# v7.2.0 + +- Added `Read-Clipboard` support for Windows. Read text from clipboard. It can read CSV or JSON. Plus, you can specify the delimiter and headers. + +### Check out the video + \ No newline at end of file