Compare commits

..

12 Commits

Author SHA1 Message Date
dfinke
c641eea10a Improve auto-detection of data on the clipboard 2021-07-24 19:41:57 -04:00
dfinke
99de9a9854 Pass parameters from Read-Clipboard to implementation 2021-07-24 13:48:18 -04:00
Doug Finke
3121c5eaed Merge pull request #1054 from dfinke/Add-new-function-Read-Clipboard
Added Read-Clipboard
2021-07-24 13:37:32 -04:00
dfinke
d02f757568 add change log 2021-07-24 13:26:06 -04:00
dfinke
a693725949 Add Tests 2021-07-24 13:01:51 -04:00
dfinke
5e19d0ca1a Add ReadClipboardImpl function 2021-07-24 13:01:48 -04:00
dfinke
204b82144f Refactor for testing 2021-07-24 13:01:32 -04:00
dfinke
91eefbd48b Added Read-Clipboard 2021-07-24 11:20:04 -04:00
dfinke
f86556a89d Updated, added Open in VS Code badge 2021-07-10 08:46:36 -04:00
dfinke
14e1dfd8db Bump version. Use ProviderPath 2021-06-20 12:11:14 -04:00
Doug Finke
1d961c679e Merge pull request #1031 from sporkabob/master
Fix UNC paths for ConvertFrom-ExcelSheet
2021-05-13 15:54:39 -04:00
sporkabob
7026803415 Fix UNC paths for ConvertFrom-ExcelSheet 2021-05-13 15:22:15 -04:00
8 changed files with 229 additions and 68 deletions

56
.vscode/launch.json vendored
View File

@@ -1,56 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Pester Tests",
"script": "Import-Module -Name '.\\' -Force ; Invoke-Pester", // Change to '.\\ModuleName.psd1' if Git name different
"args": [""],
"cwd": "${workspaceFolder}"
},
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch Current File",
"script": "${file}",
"args": [],
"cwd": "${file}"
},
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch Current File in Temporary Console",
"script": "${file}",
"args": [],
"cwd": "${file}",
"createTemporaryIntegratedConsole": true
},
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch Current File w/Args Prompt",
"script": "${file}",
"args": [
"${command:SpecifyScriptArgs}"
],
"cwd": "${file}"
},
{
"type": "PowerShell",
"request": "attach",
"name": "PowerShell Attach to Host Process",
"processId": "${command:PickPSHostProcess}",
"runspaceId": 1
},
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Interactive Session",
"cwd": ""
}
]
}

View File

@@ -6,7 +6,7 @@
RootModule = 'ImportExcel.psm1'
# Version number of this module.
ModuleVersion = '7.1.2'
ModuleVersion = '7.2.1'
# 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',

View File

@@ -19,7 +19,7 @@ function ConvertFrom-ExcelSheet {
[string[]]$AsDate = @()
)
$Path = (Resolve-Path $Path).Path
$Path = (Resolve-Path $Path).ProviderPath
$xl = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path
$workbook = $xl.Workbook

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

@@ -0,0 +1,90 @@
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) {
$cvtParams = @{
Data = Get-Clipboard -Raw
}
if ($Delimiter) {
$cvtParams.Delimiter = $Delimiter
}
if ($Header) {
$cvtParams.Header = $Header
}
ReadClipboardImpl @cvtParams
}
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 {
$dataLines = @($data -split "`r`n?" | Select-Object -First 1)
if ($dataLines[0].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
}
}

6
PublishToGallery.ps1 Normal file
View File

@@ -0,0 +1,6 @@
$p = @{
Name = "ImportExcel"
NuGetApiKey = $NuGetApiKey
}
Publish-Module @p

View File

@@ -1,14 +1,18 @@
# README
# PowerShell + Excel = Better Together
### Donation
Automate Excel via PowerShell without having Excel installed. Runs on Windows, Linux and MAC. Creating Tables, Pivot Tables, Charts and much more has just become a lot easier.
If this project helped you reduce the time to get your job done, let me know.
<br/>
[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=UCSB9YVPFSNCY)
[![](https://img.shields.io/powershellgallery/v/ImportExcel.svg)](https://www.powershellgallery.com/packages/ImportExcel) [![](https://img.shields.io/powershellgallery/dt/ImportExcel.svg)](https://www.powershellgallery.com/packages/ImportExcel) [![](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/dfinke/ImportExcel/tree/70ab9e46c776e96fb287682d5b9b4b51a0ec3bac/LICENSE.txt)
![](https://media.giphy.com/media/hpXxJ78YtpT0s/giphy.gif)
<br/>
[![](https://img.shields.io/powershellgallery/v/ImportExcel.svg)](https://www.powershellgallery.com/packages/ImportExcel) [![](https://img.shields.io/powershellgallery/dt/ImportExcel.svg)](https://www.powershellgallery.com/packages/ImportExcel) [![](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/dfinke/ImportExcel/tree/70ab9e46c776e96fb287682d5b9b4b51a0ec3bac/LICENSE.txt)
Open `ImportExcel` as a remote repo in VS Code, without cloning it.
[![Open in Visual Studio Code](https://open.vscode.dev/badges/open-in-vscode.svg)](https://open.vscode.dev/dfinke/importexcel)
<br/>
| CI System | Environment | Status |
| :--- | :--- | :--- |
@@ -17,11 +21,22 @@ If this project helped you reduce the time to get your job done, let me know.
| Azure DevOps | Ubuntu | [![Build Status](https://dougfinke.visualstudio.com/ImportExcel/_apis/build/status/dfinke.ImportExcel?branchName=master&jobName=Ubuntu)](https://dougfinke.visualstudio.com/ImportExcel/_build/latest?definitionId=21&branchName=master) |
| Azure DevOps | macOS | [![Build Status](https://dougfinke.visualstudio.com/ImportExcel/_apis/build/status/dfinke.ImportExcel?branchName=master&jobName=macOS)](https://dougfinke.visualstudio.com/ImportExcel/_build/latest?definitionId=21&branchName=master) |
PowerShell Import-Excel -
<br/>
Install from the [PowerShell Gallery](https://www.powershellgallery.com/packages/ImportExcel/).
Install from the [PowerShell Gallery](https://www.powershellgallery.com/packages/ImportExcel/).
This PowerShell Module allows you to read and write Excel files without installing Microsoft Excel on your system. No need to bother with the cumbersome Excel COM-object. Creating Tables, Pivot Tables, Charts and much more has just become a lot easier.
```powershell
Install-Module -Name ImportExcel
```
### Donation
If this project helped you reduce the time to get your job done, let me know, send a coffee.
<br/>
[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=UCSB9YVPFSNCY)
![](https://media.giphy.com/media/hpXxJ78YtpT0s/giphy.gif)
![](https://raw.githubusercontent.com/dfinke/ImportExcel/master/images/testimonial.png)
@@ -55,6 +70,10 @@ Plus, wiring the [PowerShell ScriptAnalyzer Excel report](https://github.com/dfi
![](.gitbook/assets/ScriptAnalyzerReport.png)
## What's new 7.1.3
- Changed to `ProviderPath`. Thanks [Trevor Walker](https://github.com/sporkabob)
## What's new 7.1.2
- `Get-ExcelFileSummary` - Gets summary information on an Excel file like number of rows, columns, and more

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

10
changelog.md Normal file
View File

@@ -0,0 +1,10 @@
# v7.2.1
- Improve auto-detection of data on the clipboard
# 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>