Compare commits

...

24 Commits

Author SHA1 Message Date
copilot-swe-agent[bot]
5040fc0542 Add FullCalcOnLoad fix to Export-Excel direct save path
Co-authored-by: dfinke <67258+dfinke@users.noreply.github.com>
2025-12-26 21:33:03 +00:00
copilot-swe-agent[bot]
f62aff64db Fix formula corruption by setting FullCalcOnLoad to false
Co-authored-by: dfinke <67258+dfinke@users.noreply.github.com>
2025-12-26 21:31:01 +00:00
copilot-swe-agent[bot]
bc289afc6b Initial plan 2025-12-26 21:23:00 +00:00
Doug Finke
5387c06146 Merge pull request #1695 from scriptingstudio/master
[Get-HtmlTable] XPath optimization
2025-04-23 08:16:05 -04:00
Matthew Gray
45ed6a06dc [Get-HtmlTable] XPath optimization
```powershell
$rows =    $h.SelectNodes("//table[$TableIndex]//tr")
```
XPath selector in line 53 uses complex expression that can lead to unexpected result. The problem is that HtmlAgilityPack may have specific issues. In particular, on websites containing multiple tables this selector can find not one table. This is aggravated by the fact that tables can have different structures.
To avoid ambiguity this PR suggests to separate queries. Oneliner simplifies error checking
```powershell
$rows = try {
  $h.SelectSingleNode("//table[$TableIndex]").SelectNodes(".//tr")
} catch {}
if (-not $rows) {Write-Warning "Could not find rows for `"//table[$TableIndex]`" in $Url ."}
```
This expression doesn't even need testing, it just works.
2025-04-23 13:36:32 +03:00
dfinke
fa447a745c move llms text to root 2025-04-11 07:10:31 -04:00
dfinke
49affcfba7 add llms text of Examples directort for use in LLMs 2025-04-11 06:51:18 -04:00
dfinke
dc4a5e9db9 chore: Update changelog for version 7.8.10 and acknowledge PR contributions 2024-10-21 19:44:36 -04:00
Doug Finke
85e48acf36 Merge pull request #1648 from evenmartinsen/master
Remove flagged URL
2024-10-21 19:42:23 -04:00
dfinke
fb41d3de83 chore: Update module version to 7.8.10 2024-10-21 19:39:44 -04:00
dfinke
db84a59dd0 chore: Update module version to 7.8.10 2024-10-21 19:37:58 -04:00
Even Martinsen
5a61c5dda4 Re-add icon variable (Commented)
re-added variable for easier modification in the future.
2024-10-21 14:37:55 +02:00
Even Martinsen
a2bc50aeb0 Remove flagged URI
Removed due to various AV's flagging the link as malicious thus flagging and quarentining the module.
2024-10-21 14:35:50 +02:00
dfinke
fa907da4a4 chore: Update module version to 7.8.9 2024-05-18 09:41:57 -04:00
dfinke
24c205e65d feat: Improve ConvertTo-ExcelXlsx robustness
This commit improves the `ConvertTo-ExcelXlsx` function by making it more robust. Thanks to Edward Miller for the contribution.

Note: This message follows the established convention of starting with a type (feat for feature) and providing a concise and clear description of the changes made.
2024-05-18 09:41:50 -04:00
Doug Finke
a1418a336e Merge pull request #1603 from edwardmiller-mesirow/read-only
[ConvertTo-ExcelXlsx] open XLS as read-only
2024-05-18 09:37:38 -04:00
dfinke
63683db543 chore: Update module version to 7.8.8 2024-05-18 09:33:54 -04:00
Edward Miller
36b5495bd5 check for null first 2024-05-17 23:28:26 -05:00
Edward Miller
722516de7c use try-finally 2024-05-17 23:24:11 -05:00
Edward Miller
57bb049111 open XLS as read-only 2024-05-17 22:41:53 -05:00
Doug Finke
74cbca8b2f Merge pull request #1591 from pbossman/master
Resolve worksheet ArgumentCompleter Fixes:#1590
2024-05-03 13:50:52 -04:00
Doug Finke
53712d4f7f bump version 2024-05-03 13:46:19 -04:00
Doug Finke
98e2ac96ea update changelog and add image 2024-05-03 13:46:02 -04:00
Phil Bossman
efa73b37a0 Resolve worksheet argumentcompleter Fixes:#1590 2024-04-30 11:59:08 -04:00
10 changed files with 5763 additions and 12 deletions

View File

@@ -6,7 +6,7 @@
RootModule = 'ImportExcel.psm1'
# Version number of this module.
ModuleVersion = '7.8.6'
ModuleVersion = '7.8.10'
# ID used to uniquely identify this module
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
@@ -150,7 +150,7 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5
Tags = @("Excel", "EPPlus", "Export", "Import")
# The web address of an icon which can be used in galleries to represent this module
#IconUri = "http://pesterbdd.com/images/Pester.png"
#IconUri =
# The web address of this module's project or support homepage.
ProjectUri = "https://github.com/dfinke/ImportExcel"
@@ -215,4 +215,4 @@ Check out the How To Videos https://www.youtube.com/watch?v=U3Ne_yX4tYo&list=PL5
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
}
}

View File

@@ -67,9 +67,8 @@ function WorksheetArgumentCompleter {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$xlPath = $fakeBoundParameter['Path']
if (Test-Path -Path $xlPath) {
$xlpkg = Open-ExcelPackage -ReadOnly -Path $xlPath
$WorksheetNames = $xlPkg.Workbook.Worksheets.Name
Close-ExcelPackage -nosave -ExcelPackage $xlpkg
$xlSheet = Get-ExcelSheetInfo -Path $xlPath
$WorksheetNames = $xlSheet.Name
$WorksheetNames.where( { $_ -like "*$wordToComplete*" }) | foreach-object {
New-Object -TypeName System.Management.Automation.CompletionResult -ArgumentList "'$_'",
$_ , ([System.Management.Automation.CompletionResultType]::ParameterValue) , $_

View File

@@ -19,6 +19,9 @@ function Close-ExcelPackage {
try { [OfficeOpenXml.CalculationExtension]::Calculate($ExcelPackage.Workbook) }
catch { Write-Warning "One or more errors occured while calculating, save will continue, but there may be errors in the workbook." }
}
# Set FullCalcOnLoad to false to prevent Excel from corrupting formulas during recalculation
# This fixes issues with table-structured references like [[#This Row],[ColumnName]]
$ExcelPackage.Workbook.FullCalcOnLoad = $false
if ($SaveAs) {
$SaveAs = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($SaveAs)
if ($Password) { $ExcelPackage.SaveAs( $SaveAs, $Password ) }

View File

@@ -45,11 +45,18 @@ function ConvertTo-ExcelXlsx {
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()
try {
$Excel.Visible = $false
$null = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
$Excel.ActiveWorkbook.SaveAs($xlsxPath, $xlFixedFormat)
}
finally {
if ($null -ne $Excel.ActiveWorkbook) {
$Excel.ActiveWorkbook.Close()
}
$Excel.Quit()
}
}
}

View File

@@ -682,6 +682,9 @@
else {
if ($ReturnRange) { $dataRange }
# Set FullCalcOnLoad to false to prevent Excel from corrupting formulas during recalculation
# This fixes issues with table-structured references like [[#This Row],[ColumnName]]
$pkg.Workbook.FullCalcOnLoad = $false
if ($Password) { $pkg.Save($Password) }
else { $pkg.Save() }
Write-Verbose -Message "Saved workbook $($pkg.File)"

View File

@@ -50,7 +50,9 @@ function Get-HtmlTable {
else {
$h = ConvertFrom-Html -Content $r.Content
if ($TableIndex -is [valuetype]) { $TableIndex += 1}
$rows = $h.SelectNodes("//table[$TableIndex]//tr")
$rows = try {
$h.SelectSingleNode("//table[$TableIndex]").SelectNodes(".//tr")
} catch {}
if (-not $rows) {Write-Warning "Could not find rows for `"//table[$TableIndex]`" in $Url ."}
if ( -not $propertyNames) {
if ( $tableHeaders = $rows[$FirstDataRow].SelectNodes("th")) {

View File

@@ -0,0 +1,125 @@
#Requires -Modules @{ ModuleName="Pester"; ModuleVersion="4.0.0" }
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'False Positives')]
param()
Describe "Table Formula Bug Fix" -Tag "TableFormula" {
BeforeAll {
$WarningAction = "SilentlyContinue"
}
Context "FullCalcOnLoad is set to false to prevent formula corruption" {
BeforeAll {
$path = "TestDrive:\table_formula.xlsx"
Remove-Item -Path $path -ErrorAction SilentlyContinue
# Create a table with a blank record
$BlankRecordForFile = [PsCustomObject]@{
'Action Add' = ''
UserName = ''
Address = ''
Name = ''
NewName = ''
}
# Export as a table
$ExcelFile = $BlankRecordForFile | Export-Excel -Path $path -WorksheetName 'Data' `
-TableName 'DataTable' -TableStyle 'Light1' -AutoSize:$false -AutoFilter `
-BoldTopRow -FreezeTopRow -StartRow 1 -PassThru
$Worksheet = $ExcelFile.Workbook.Worksheets['Data']
# Insert a row and add a complex formula with table-structured references
$Worksheet.InsertRow(2, 1)
# This formula uses old-style table references [[#This Row],[ColumnName]]
# which Excel converts to [@ColumnName] when opening
$Formula = '=IFS( [[#This Row],[UserName]]="","", [[#This Row],[Action Add]]=TRUE, CONCAT([[#This Row],[Address]],"-",[[#This Row],[UserName]]), CONCAT([[#This Row],[Address]],"-",[[#This Row],[UserName]]) <> [[#This Row],[Name]], CONCAT([[#This Row],[Address]],"-",[[#This Row],[UserName]]), TRUE, "")'
$Cell = $Worksheet.Cells['e2']
$Cell.Formula = $Formula
Close-ExcelPackage $ExcelFile
# Reopen to verify
$ExcelFile2 = Open-ExcelPackage -Path $path
$Worksheet2 = $ExcelFile2.Workbook.Worksheets['Data']
}
It "Sets fullCalcOnLoad to false in the workbook XML" {
# Extract and check the XML directly
$TempExtractPath = Join-Path -Path $TestDrive -ChildPath "extracted_$(Get-Random)"
Expand-Archive -Path $path -DestinationPath $TempExtractPath -Force
$WorkbookXml = Get-Content (Join-Path -Path $TempExtractPath -ChildPath "xl/workbook.xml") -Raw
$WorkbookXml | Should -Match 'fullCalcOnLoad="0"'
Remove-Item -Path $TempExtractPath -Recurse -Force
}
It "Preserves the formula correctly after save and reopen" {
$Cell2 = $Worksheet2.Cells['e2']
$Cell2.Formula | Should -Not -BeNullOrEmpty
$Cell2.Formula | Should -Match 'IFS\('
$Cell2.Formula | Should -Match 'CONCAT\('
}
It "Does not corrupt the formula with extra @ symbols" {
$Cell2 = $Worksheet2.Cells['e2']
# The formula should not have extra @ symbols added by Excel during recalculation
# The specific bug was an @ being inserted before CONCAT in the middle of the formula
# We can't test this directly without opening in Excel, but we can verify the formula is unchanged
$Cell2.Formula.Length | Should -BeGreaterThan 100
}
AfterAll {
if ($ExcelFile2) {
Close-ExcelPackage -ExcelPackage $ExcelFile2 -NoSave
}
}
}
Context "FullCalcOnLoad setting works with different save methods" {
It "Sets fullCalcOnLoad to false when using SaveAs" {
$path = "TestDrive:\saveas_test.xlsx"
$path2 = "TestDrive:\saveas_test2.xlsx"
Remove-Item -Path $path, $path2 -ErrorAction SilentlyContinue
$data = [PSCustomObject]@{ Name = 'Test' }
$excel = $data | Export-Excel -Path $path -PassThru
Close-ExcelPackage $excel -SaveAs $path2
# Extract and check the XML
$TempExtractPath = Join-Path -Path $TestDrive -ChildPath "extracted_saveas_$(Get-Random)"
Expand-Archive -Path $path2 -DestinationPath $TempExtractPath -Force
$WorkbookXml = Get-Content (Join-Path -Path $TempExtractPath -ChildPath "xl/workbook.xml") -Raw
$WorkbookXml | Should -Match 'fullCalcOnLoad="0"'
Remove-Item -Path $TempExtractPath -Recurse -Force
}
It "Sets fullCalcOnLoad to false when using Calculate flag" {
$path = "TestDrive:\calculate_test.xlsx"
Remove-Item -Path $path -ErrorAction SilentlyContinue
$data = [PSCustomObject]@{ Name = 'Test'; Value = 100 }
$excel = $data | Export-Excel -Path $path -PassThru
# Set a formula
$ws = $excel.Workbook.Worksheets[1]
$ws.Cells['C2'].Formula = 'B2*2'
Close-ExcelPackage $excel -Calculate
# Extract and check the XML
$TempExtractPath = Join-Path -Path $TestDrive -ChildPath "extracted_calc_$(Get-Random)"
Expand-Archive -Path $path -DestinationPath $TempExtractPath -Force
$WorkbookXml = Get-Content (Join-Path -Path $TempExtractPath -ChildPath "xl/workbook.xml") -Raw
$WorkbookXml | Should -Match 'fullCalcOnLoad="0"'
Remove-Item -Path $TempExtractPath -Recurse -Force
}
}
}

View File

@@ -1,3 +1,23 @@
# 7.8.10
- Thank you https://github.com/evenmartinsen for the PR to fix the AV
# 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
![alt text](images/AutoCompleteSheetNames.png)
# 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.

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

5592
llms-examples.txt Normal file

File diff suppressed because it is too large Load Diff