mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-15 07:43:23 +00:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09f35b5fd5 | ||
|
|
23a2ac394f | ||
|
|
50928e8e3b | ||
|
|
26827b0764 | ||
|
|
6c35d5b018 | ||
|
|
ab5600c9ba | ||
|
|
47dcc636d9 | ||
|
|
1768c0ac92 | ||
|
|
694fcdd8c1 | ||
|
|
0192f4d822 | ||
|
|
c641eea10a | ||
|
|
99de9a9854 | ||
|
|
3121c5eaed | ||
|
|
d02f757568 | ||
|
|
a693725949 | ||
|
|
5e19d0ca1a | ||
|
|
204b82144f | ||
|
|
91eefbd48b | ||
|
|
f86556a89d | ||
|
|
14e1dfd8db | ||
|
|
1d961c679e | ||
|
|
7026803415 |
56
.vscode/launch.json
vendored
56
.vscode/launch.json
vendored
@@ -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": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
85
Public/Read-Clipboard.ps1
Normal 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
6
PublishToGallery.ps1
Normal file
@@ -0,0 +1,6 @@
|
||||
$p = @{
|
||||
Name = "ImportExcel"
|
||||
NuGetApiKey = $NuGetApiKey
|
||||
}
|
||||
|
||||
Publish-Module @p
|
||||
37
README.md
37
README.md
@@ -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/>
|
||||
|
||||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=UCSB9YVPFSNCY)
|
||||
[](https://www.powershellgallery.com/packages/ImportExcel) [](https://www.powershellgallery.com/packages/ImportExcel) [](https://github.com/dfinke/ImportExcel/tree/70ab9e46c776e96fb287682d5b9b4b51a0ec3bac/LICENSE.txt)
|
||||
|
||||

|
||||
<br/>
|
||||
|
||||
[](https://www.powershellgallery.com/packages/ImportExcel) [](https://www.powershellgallery.com/packages/ImportExcel) [](https://github.com/dfinke/ImportExcel/tree/70ab9e46c776e96fb287682d5b9b4b51a0ec3bac/LICENSE.txt)
|
||||
Open `ImportExcel` as a remote repo in VS Code, without cloning it.
|
||||
|
||||
[](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 | [](https://dougfinke.visualstudio.com/ImportExcel/_build/latest?definitionId=21&branchName=master) |
|
||||
| Azure DevOps | 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/>
|
||||
|
||||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=UCSB9YVPFSNCY)
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
@@ -55,6 +70,10 @@ Plus, wiring the [PowerShell ScriptAnalyzer Excel report](https://github.com/dfi
|
||||
|
||||

|
||||
|
||||
## 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
|
||||
|
||||
39
__tests__/Open-ExcelPackage.tests.ps1
Normal file
39
__tests__/Open-ExcelPackage.tests.ps1
Normal 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
|
||||
}
|
||||
}
|
||||
90
__tests__/Read-Clipboard.tests.ps1
Normal file
90
__tests__/Read-Clipboard.tests.ps1
Normal 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'
|
||||
}
|
||||
}
|
||||
BIN
__tests__/UnsupportedWorkSheetNames.xlsx
Normal file
BIN
__tests__/UnsupportedWorkSheetNames.xlsx
Normal file
Binary file not shown.
22
changelog.md
Normal file
22
changelog.md
Normal 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>
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user