Merge pull request #1054 from dfinke/Add-new-function-Read-Clipboard

Added Read-Clipboard
This commit is contained in:
Doug Finke
2021-07-24 13:37:32 -04:00
committed by GitHub
4 changed files with 177 additions and 2 deletions

View File

@@ -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',

76
Public/Read-Clipboard.ps1 Normal file
View File

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

View File

@@ -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'
}
}

7
changelog.md Normal file
View File

@@ -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
<a href="https://youtu.be/dv2GOH5sbpA"><img src="https://img.youtube.com/vi/dv2GOH5sbpA/0.jpg" width="400"></a>