mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Compare commits
17 Commits
fix-import
...
4502144602
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4502144602 | ||
|
|
34c30eb301 | ||
|
|
37cd7335c7 | ||
|
|
b6034c37d8 | ||
|
|
88a3aac640 | ||
|
|
66c0fee1e9 | ||
|
|
fa907da4a4 | ||
|
|
24c205e65d | ||
|
|
a1418a336e | ||
|
|
63683db543 | ||
|
|
36b5495bd5 | ||
|
|
722516de7c | ||
|
|
57bb049111 | ||
|
|
74cbca8b2f | ||
|
|
53712d4f7f | ||
|
|
98e2ac96ea | ||
|
|
efa73b37a0 |
@@ -6,7 +6,7 @@
|
|||||||
RootModule = 'ImportExcel.psm1'
|
RootModule = 'ImportExcel.psm1'
|
||||||
|
|
||||||
# Version number of this module.
|
# Version number of this module.
|
||||||
ModuleVersion = '7.8.6'
|
ModuleVersion = '7.8.9'
|
||||||
|
|
||||||
# ID used to uniquely identify this module
|
# ID used to uniquely identify this module
|
||||||
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
|
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
|
||||||
|
|||||||
@@ -67,9 +67,8 @@ function WorksheetArgumentCompleter {
|
|||||||
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
|
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
|
||||||
$xlPath = $fakeBoundParameter['Path']
|
$xlPath = $fakeBoundParameter['Path']
|
||||||
if (Test-Path -Path $xlPath) {
|
if (Test-Path -Path $xlPath) {
|
||||||
$xlpkg = Open-ExcelPackage -ReadOnly -Path $xlPath
|
$xlSheet = Get-ExcelSheetInfo -Path $xlPath
|
||||||
$WorksheetNames = $xlPkg.Workbook.Worksheets.Name
|
$WorksheetNames = $xlSheet.Name
|
||||||
Close-ExcelPackage -nosave -ExcelPackage $xlpkg
|
|
||||||
$WorksheetNames.where( { $_ -like "*$wordToComplete*" }) | foreach-object {
|
$WorksheetNames.where( { $_ -like "*$wordToComplete*" }) | foreach-object {
|
||||||
New-Object -TypeName System.Management.Automation.CompletionResult -ArgumentList "'$_'",
|
New-Object -TypeName System.Management.Automation.CompletionResult -ArgumentList "'$_'",
|
||||||
$_ , ([System.Management.Automation.CompletionResultType]::ParameterValue) , $_
|
$_ , ([System.Management.Automation.CompletionResultType]::ParameterValue) , $_
|
||||||
|
|||||||
@@ -3,53 +3,109 @@ function ConvertTo-ExcelXlsx {
|
|||||||
param
|
param
|
||||||
(
|
(
|
||||||
[parameter(Mandatory = $true, ValueFromPipeline)]
|
[parameter(Mandatory = $true, ValueFromPipeline)]
|
||||||
[string]$Path,
|
[string[]]$Path,
|
||||||
[parameter(Mandatory = $false)]
|
[parameter(Mandatory = $false)]
|
||||||
[switch]$Force
|
[switch]$Force,
|
||||||
|
[parameter(Mandatory = $false)]
|
||||||
|
[switch]$CacheToTemp,
|
||||||
|
[parameter(Mandatory = $false)]
|
||||||
|
[string]$CacheDirectory
|
||||||
)
|
)
|
||||||
process {
|
process {
|
||||||
if (-Not ($Path | Test-Path) ) {
|
try {
|
||||||
throw "File not found"
|
foreach ($singlePath in $Path) {
|
||||||
}
|
if (-Not ($singlePath | Test-Path) ) {
|
||||||
if (-Not ($Path | Test-Path -PathType Leaf) ) {
|
throw "File not found"
|
||||||
throw "Folder paths are not allowed"
|
}
|
||||||
}
|
if (-Not ($singlePath | Test-Path -PathType Leaf) ) {
|
||||||
|
throw "Folder paths are not allowed"
|
||||||
|
}
|
||||||
|
|
||||||
$xlFixedFormat = 51 #Constant for XLSX Workbook
|
$xlFixedFormat = 51 #Constant for XLSX Workbook
|
||||||
$xlsFile = Get-Item -Path $Path
|
$xlsFile = Get-Item -Path $singlePath
|
||||||
$xlsxPath = "{0}x" -f $xlsFile.FullName
|
$destinationXlsxPath = [System.IO.Path]::ChangeExtension($xlsFile.FullName, ".xlsx")
|
||||||
|
|
||||||
if ($xlsFile.Extension -ne ".xls") {
|
if ($xlsFile.Extension -ne ".xls") {
|
||||||
throw "Expected .xls extension"
|
throw "Expected .xls extension"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Test-Path -Path $xlsxPath) {
|
if (Test-Path -Path $destinationXlsxPath) {
|
||||||
if ($Force) {
|
if ($Force) {
|
||||||
try {
|
try {
|
||||||
Remove-Item $xlsxPath -Force
|
Remove-Item $destinationXlsxPath -Force
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $destinationXlsxPath
|
||||||
|
}
|
||||||
|
Write-Verbose $("Removed {0}" -f $destinationXlsxPath)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw "{0} already exists!" -f $destinationXlsxPath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($null -eq $Excel)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$Excel = New-Object -ComObject "Excel.Application"
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
throw "Could not create Excel.Application ComObject. Please verify that Excel is installed."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($CacheToTemp) {
|
||||||
|
if (-not $CacheDirectory) {
|
||||||
|
$CacheDirectory = [System.IO.Path]::GetTempPath()
|
||||||
|
}
|
||||||
|
$tempPath = [System.IO.Path]::Combine($CacheDirectory, [System.IO.Path]::GetFileName($xlsFile.FullName))
|
||||||
|
Write-Host ("Using Temp path: {0}" -f $tempPath)
|
||||||
|
Copy-Item -Path $xlsFile.FullName -Destination $tempPath -Force
|
||||||
|
$fileToProcess = $tempPath
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$fileToProcess = $xlsFile.FullName
|
||||||
|
}
|
||||||
|
|
||||||
|
$xlsxPath = [System.IO.Path]::ChangeExtension($fileToProcess, ".xlsx")
|
||||||
|
|
||||||
|
try {
|
||||||
|
$Excel.Visible = $false
|
||||||
|
$workbook = $Excel.Workbooks.Open($fileToProcess, $null, $true)
|
||||||
|
if ($null -eq $workbook) {
|
||||||
|
Write-Host "Failed to open workbook"
|
||||||
|
} else {
|
||||||
|
$workbook.SaveAs($xlsxPath, $xlFixedFormat)
|
||||||
|
|
||||||
|
if ($CacheToTemp) {
|
||||||
|
Copy-Item -Path $xlsxPath -Destination $destinationXlsxPath -Force
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
throw "{0} already exists and cannot be removed. The file may be locked by another application." -f $xlsxPath
|
Write-Error ("Failed to convert {0} to XLSX." -f $xlsFile.FullName)
|
||||||
|
throw
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
if ($null -ne $workbook) {
|
||||||
|
$workbook.Close()
|
||||||
|
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null
|
||||||
|
$workbook = $null
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($CacheToTemp) {
|
||||||
|
Remove-Item -Path $tempPath -Force
|
||||||
|
Remove-Item -Path $xlsxPath -Force
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Write-Verbose $("Removed {0}" -f $xlsxPath)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw "{0} already exists!" -f $xlsxPath
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
try {
|
if ($null -ne $Excel) {
|
||||||
$Excel = New-Object -ComObject "Excel.Application"
|
$Excel.Quit()
|
||||||
|
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Excel) | Out-Null
|
||||||
|
$Excel = $null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch {
|
|
||||||
throw "Could not create Excel.Application ComObject. Please verify that Excel is installed."
|
|
||||||
}
|
|
||||||
|
|
||||||
$Excel.Visible = $false
|
|
||||||
$null = $Excel.Workbooks.Open($xlsFile.FullName)
|
|
||||||
$Excel.ActiveWorkbook.SaveAs($xlsxPath, $xlFixedFormat)
|
|
||||||
$Excel.ActiveWorkbook.Close()
|
|
||||||
$Excel.Quit()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
changelog.md
16
changelog.md
@@ -1,3 +1,19 @@
|
|||||||
|
# 7.8.9
|
||||||
|
|
||||||
|
- Thanks to (Edward Miller)[https://github.com/edwardmiller-mesirow] for improving `ConvertTo-ExcelXlsx`and making it more robust
|
||||||
|
|
||||||
|
# 7.8.8
|
||||||
|
|
||||||
|
- Fix the release
|
||||||
|
|
||||||
|
# 7.8.7
|
||||||
|
|
||||||
|
- Thanks to [Phil Bossman](https://github.com/pbossman) for the PR and fixing this.
|
||||||
|
|
||||||
|
Now, back again, you can type `Import-Excel .\yearlySales.xlsx`, press <ctrl+space> and get a list of the worksheets in the Excel file
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
# Infrastructure change
|
# Infrastructure change
|
||||||
|
|
||||||
- Thank you to [RipFence](https://github.com/RipFence) who asked how to place a chart on a different sheet from the data and then did a PR adding the example.
|
- Thank you to [RipFence](https://github.com/RipFence) who asked how to place a chart on a different sheet from the data and then did a PR adding the example.
|
||||||
|
|||||||
BIN
images/AutoCompleteSheetNames.png
Normal file
BIN
images/AutoCompleteSheetNames.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
Reference in New Issue
Block a user