Compare commits

..

22 Commits

Author SHA1 Message Date
Doug Finke
09f35b5fd5 Merge pull request #1078 from dfinke/Add-Checks-And-Warnings-for-Add-Member-Open-Excel-Package
Add-Checks-And-Warnings-for-Add-Member-Open-Excel-Package
2021-09-14 18:15:47 -04:00
dfinke
23a2ac394f Add try/catch, warning, and tests 2021-09-14 15:44:32 -04:00
dfinke
50928e8e3b Update 2021-09-14 15:44:11 -04:00
dfinke
26827b0764 bump version 2021-09-14 15:44:03 -04:00
Doug Finke
6c35d5b018 Updated 2021-09-10 18:41:03 -04:00
Doug Finke
ab5600c9ba bump version 2021-09-10 18:40:57 -04:00
Doug Finke
47dcc636d9 Merge pull request #1077 from stahler/Open-ExcelPackage
Update open-excelpackage.md
2021-09-10 18:39:00 -04:00
Stahler
1768c0ac92 Update open-excelpackage.md 2021-09-10 14:17:44 -04:00
Doug Finke
694fcdd8c1 Merge pull request #1059 from dfinke/add-test-for-ps-5-to-7-and-linux
Improved checks for Linux, Mac and PS 5.1 - #1056, #1057
2021-07-30 19:27:06 -04:00
dfinke
0192f4d822 Improved checks for Linux, Mac and PS 5.1 - #1056, #1057 2021-07-30 19:21:37 -04:00
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
12 changed files with 292 additions and 80 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.3.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',

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

View File

@@ -1,10 +1,10 @@
function Open-ExcelPackage {
function Open-ExcelPackage {
[CmdLetBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword","")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")]
[OutputType([OfficeOpenXml.ExcelPackage])]
param(
#The path to the file to open.
[Parameter(Mandatory=$true)]$Path,
[Parameter(Mandatory = $true)]$Path,
#If specified, any running instances of Excel will be terminated before opening the file.
[switch]$KillExcel,
#The password for a protected worksheet, as a [normal] string (not a secure string).
@@ -13,7 +13,7 @@
[switch]$Create
)
if($KillExcel) {
if ($KillExcel) {
Get-Process -Name "excel" -ErrorAction Ignore | Stop-Process
while (Get-Process -Name "excel" -ErrorAction Ignore) {}
}
@@ -24,21 +24,26 @@
#Create the directory if required.
$targetPath = Split-Path -Parent -Path $Path
if (!(Test-Path -Path $targetPath)) {
Write-Debug "Base path $($targetPath) does not exist, creating"
$null = New-item -ItemType Directory -Path $targetPath -ErrorAction Ignore
Write-Debug "Base path $($targetPath) does not exist, creating"
$null = New-item -ItemType Directory -Path $targetPath -ErrorAction Ignore
}
New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path
}
elseif (Test-Path -Path $path) {
if ($Password) {$pkgobj = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path , $Password }
else {$pkgobj = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path }
if ($Password) { $pkgobj = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path , $Password }
else { $pkgobj = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path }
if ($pkgobj) {
foreach ($w in $pkgobj.Workbook.Worksheets) {
$sb = [scriptblock]::Create(('$this.workbook.Worksheets["{0}"]' -f $w.name))
Add-Member -InputObject $pkgobj -MemberType ScriptProperty -Name $w.name -Value $sb
try {
Add-Member -InputObject $pkgobj -MemberType ScriptProperty -Name $w.name -Value $sb -ErrorAction Stop
}
catch {
Write-Warning "Could not add sheet $($w.name) as 'short cut', you need to access it via `$wb.Worksheets['$($w.name)'] "
}
}
return $pkgobj
}
}
else {Write-Warning "Could not find $path" }
}
else { Write-Warning "Could not find $path" }
}

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

@@ -0,0 +1,85 @@
#Requires -Version 5
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 ($IsLinux -or $IsMacOS) {
Write-Error "Read-Clipboard only runs on Windows"
return
}
$cvtParams = @{
Data = Get-Clipboard -Raw
}
if ($Delimiter) {
$cvtParams.Delimiter = $Delimiter
}
if ($Header) {
$cvtParams.Header = $Header
}
ReadClipboardImpl @cvtParams
}
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,39 @@
#Requires -Modules Pester
if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) {
Import-Module $PSScriptRoot\..\ImportExcel.psd1
}
<#
Methods
-------
Dispose
Equals
GetAsByteArray
GetHashCode
GetType
Load
Save
SaveAs
ToString
Properties
----------
Compatibility
Compression
DoAdjustDrawings
Encryption
File
Package
Stream
Workbook
#>
Describe "Test Open Excel Package" -Tag Open-ExcelPackage {
It "Should handle opening a workbook with Worksheet Names that will cause errors" {
$xlFilename = "$PSScriptRoot\UnsupportedWorkSheetNames.xlsx"
{ Open-ExcelPackage -Path $xlFilename -ErrorAction Stop } | Should -Not -Throw
}
}

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

Binary file not shown.

22
changelog.md Normal file
View File

@@ -0,0 +1,22 @@
# v7.3.0
- Fix throwing error when a Worksheet name collides with a method, or property name on the `OfficeOpenXml.ExcelPackage` package
# v7.2.3
- Fix inline help, thank you [Wes Stahler](https://github.com/stahler)
# v7.2.2
- Improved checks for Linux, Mac and PS 5.1
# 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>

View File

@@ -9,7 +9,7 @@ schema: 2.0.0
## SYNOPSIS
Returns an ExcelPackage object for the specified XLSX fil.e
Returns an ExcelPackage object for the specified XLSX file.
## SYNTAX