From 91eefbd48b5c6365ae20c5034926198b946c4c0c Mon Sep 17 00:00:00 2001 From: dfinke Date: Sat, 24 Jul 2021 11:20:04 -0400 Subject: [PATCH 1/5] Added Read-Clipboard --- ImportExcel.psd1 | 5 ++-- Public/Read-Clipboard.ps1 | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 Public/Read-Clipboard.ps1 diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index 6651fd4..c9d77af 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,8 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5 'Open-ExcelPackage', 'PieChart', 'Pivot', - 'Remove-Worksheet' + 'Read-Clipboard', + '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..2c3b380 --- /dev/null +++ b/Public/Read-Clipboard.ps1 @@ -0,0 +1,53 @@ +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 is tuned to read tab delimited data. It can read CSV or JSON + + .EXAMPLE + Read-Clipboard # delimter default is tab `t + + .EXAMPLE + Read-Clipboard -Delimiter ',' # Converts CSV + + .EXAMPLE + Read-Clipboard -AsJson # Converts JSON + + #> + param( + $Delimiter = "`t", + $Header, + [Switch]$AsJson + ) + + if ($IsWindows) { + $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem + if ($osInfo.ProductType -eq 1) { + $clipboardData = Get-Clipboard -Raw + if ($AsJson) { + ConvertFrom-Json -InputObject $clipboardData + } + else { + $cvtParams = @{ + InputObject = $clipboardData + Delimiter = $Delimiter + } + + if ($Header) { + $cvtParams.Header = $Header + } + + ConvertFrom-Csv @cvtParams + } + } + else { + Write-Error "This command is only supported on the desktop." + } + } + else { + Write-Error "This function is only available on Windows desktop" + } +} \ No newline at end of file From 204b82144f8b2c34f5e5e8ae949a89dada02f56c Mon Sep 17 00:00:00 2001 From: dfinke Date: Sat, 24 Jul 2021 13:01:32 -0400 Subject: [PATCH 2/5] Refactor for testing --- Public/Read-Clipboard.ps1 | 69 ++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/Public/Read-Clipboard.ps1 b/Public/Read-Clipboard.ps1 index 2c3b380..9e20879 100644 --- a/Public/Read-Clipboard.ps1 +++ b/Public/Read-Clipboard.ps1 @@ -5,43 +5,27 @@ function Read-Clipboard { Check out the how to video - https://youtu.be/dv2GOH5sbpA .DESCRIPTION - Read text from clipboard. It is tuned to read tab delimited data. It can read CSV or JSON + Read text from clipboard. It can read CSV or JSON. Plus, you can specify the delimiter and headers. .EXAMPLE - Read-Clipboard # delimter default is tab `t + Read-Clipboard # Detects if the clipboard contains CSV, JSON, or Tab delimited data. .EXAMPLE - Read-Clipboard -Delimiter ',' # Converts CSV + Read-Clipboard -Delimiter '|' # Converts data using a pipe delimiter .EXAMPLE - Read-Clipboard -AsJson # Converts JSON + Read-Clipboard -Header 'P1', 'P2', 'P3' # Specify the header columns to be used #> param( - $Delimiter = "`t", - $Header, - [Switch]$AsJson + $Delimiter, + $Header ) if ($IsWindows) { $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem if ($osInfo.ProductType -eq 1) { - $clipboardData = Get-Clipboard -Raw - if ($AsJson) { - ConvertFrom-Json -InputObject $clipboardData - } - else { - $cvtParams = @{ - InputObject = $clipboardData - Delimiter = $Delimiter - } - - if ($Header) { - $cvtParams.Header = $Header - } - - ConvertFrom-Csv @cvtParams - } + ReadClipboardImpl (Get-Clipboard -Raw) } else { Write-Error "This command is only supported on the desktop." @@ -50,4 +34,43 @@ function Read-Clipboard { 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 From 5e19d0ca1aae23d651c1605cb66ba14badfd2835 Mon Sep 17 00:00:00 2001 From: dfinke Date: Sat, 24 Jul 2021 13:01:48 -0400 Subject: [PATCH 3/5] Add ReadClipboardImpl function --- ImportExcel.psd1 | 1 + 1 file changed, 1 insertion(+) diff --git a/ImportExcel.psd1 b/ImportExcel.psd1 index c9d77af..c8837fd 100644 --- a/ImportExcel.psd1 +++ b/ImportExcel.psd1 @@ -79,6 +79,7 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5 'PieChart', 'Pivot', 'Read-Clipboard', + 'ReadClipboardImpl', 'Remove-Worksheet', 'Select-Worksheet', 'Send-SQLDataToExcel', From a69372594943a9281b2c8eae7e74c33f356ab94b Mon Sep 17 00:00:00 2001 From: dfinke Date: Sat, 24 Jul 2021 13:01:51 -0400 Subject: [PATCH 4/5] Add Tests --- __tests__/Read-Clipboard.tests.ps1 | 90 ++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 __tests__/Read-Clipboard.tests.ps1 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 From d02f757568b90ed223451dc54a0c7191c96dd55d Mon Sep 17 00:00:00 2001 From: dfinke Date: Sat, 24 Jul 2021 13:26:06 -0400 Subject: [PATCH 5/5] add change log --- changelog.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 changelog.md 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