@kevlns/xlmerge 1.3.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +9 -4
- package/README.md +12 -6
- package/package.json +2 -2
- package/python/xlsx_merge_engine/xlsx_git_merge_bridge.py +24 -0
- package/python/xlsx_resolver/__init__.py +1 -1
- package/python/xlsx_resolver/formula_values.py +565 -0
- package/python/xlsx_resolver/merge_ui.html +345 -50
- package/python/xlsx_resolver/recalculate_workbook.ps1 +155 -0
- package/python/xlsx_resolver/resolve_xlsx_conflict.py +9 -0
- package/python/xlsx_resolver/xlsx_conflict.py +506 -113
- package/v-cli.plugin.json +5 -1
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
[CmdletBinding()]
|
|
2
|
+
param([string]$ParametersPath, [string]$CleanupParametersPath)
|
|
3
|
+
|
|
4
|
+
$ErrorActionPreference = 'Stop'
|
|
5
|
+
$excel = $null
|
|
6
|
+
$book = $null
|
|
7
|
+
$excelPid = $null
|
|
8
|
+
$ownedExcel = $false
|
|
9
|
+
$excelStartFileTimeUtc = $null
|
|
10
|
+
$statePath = $null
|
|
11
|
+
|
|
12
|
+
if (![string]::IsNullOrWhiteSpace($CleanupParametersPath)) {
|
|
13
|
+
try {
|
|
14
|
+
$owned = Get-Content -LiteralPath $CleanupParametersPath -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
15
|
+
$p = Get-Process -Id ([int]$owned.pid) -ErrorAction SilentlyContinue
|
|
16
|
+
if ($p -and $p.ProcessName -ieq 'EXCEL') {
|
|
17
|
+
$ft = $p.StartTime.ToUniversalTime().ToFileTimeUtc()
|
|
18
|
+
if ($ft -eq [int64]$owned.startFileTimeUtc) {
|
|
19
|
+
Stop-Process -Id $p.Id -Force -ErrorAction SilentlyContinue
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exit 0
|
|
23
|
+
} catch { exit 1 }
|
|
24
|
+
}
|
|
25
|
+
if ([string]::IsNullOrWhiteSpace($ParametersPath)) { throw 'ParametersPath is required' }
|
|
26
|
+
|
|
27
|
+
if (-not ('XlmergeExcelWindow' -as [type])) {
|
|
28
|
+
Add-Type @"
|
|
29
|
+
using System;
|
|
30
|
+
using System.Runtime.InteropServices;
|
|
31
|
+
public static class XlmergeExcelWindow {
|
|
32
|
+
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
|
|
33
|
+
}
|
|
34
|
+
"@
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function Write-State([string]$status, [string]$errorText = $null) {
|
|
38
|
+
if ([string]::IsNullOrWhiteSpace($statePath)) { return }
|
|
39
|
+
$state = [ordered]@{ status = $status }
|
|
40
|
+
if ($null -ne $excelPid) {
|
|
41
|
+
$state.pid = [int]$excelPid
|
|
42
|
+
if ($null -ne $excelStartFileTimeUtc) { $state.startFileTimeUtc = [int64]$excelStartFileTimeUtc }
|
|
43
|
+
}
|
|
44
|
+
if ($null -ne $errorText) { $state.error = $errorText }
|
|
45
|
+
$state | ConvertTo-Json -Compress | Set-Content -LiteralPath $statePath -Encoding UTF8
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
$parameters = Get-Content -LiteralPath $ParametersPath -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
50
|
+
$source = [IO.Path]::GetFullPath([string]$parameters.source)
|
|
51
|
+
$result = [IO.Path]::GetFullPath([string]$parameters.result)
|
|
52
|
+
$statePath = [IO.Path]::GetFullPath([string]$parameters.state)
|
|
53
|
+
$timeoutSeconds = [int]$parameters.timeoutSeconds
|
|
54
|
+
if ($timeoutSeconds -lt 1) { throw 'timeoutSeconds must be positive' }
|
|
55
|
+
if (!(Test-Path -LiteralPath $source -PathType Leaf)) { throw "Source workbook does not exist: $source" }
|
|
56
|
+
$parent = Split-Path -Parent $result
|
|
57
|
+
if (!(Test-Path -LiteralPath $parent)) { New-Item -ItemType Directory -Path $parent -Force | Out-Null }
|
|
58
|
+
|
|
59
|
+
# Excel constants are used as numbers so this script has no interop assembly
|
|
60
|
+
# dependency. AutomationSecurity=3 is msoAutomationSecurityForceDisable.
|
|
61
|
+
$existingExcelPids = @(Get-Process -Name EXCEL -ErrorAction SilentlyContinue | ForEach-Object { $_.Id })
|
|
62
|
+
$excel = New-Object -ComObject Excel.Application
|
|
63
|
+
$window = [IntPtr]$excel.Hwnd
|
|
64
|
+
[uint32]$windowPid = 0
|
|
65
|
+
[XlmergeExcelWindow]::GetWindowThreadProcessId($window, [ref]$windowPid) | Out-Null
|
|
66
|
+
if ($windowPid -eq 0) { throw 'Unable to identify the Excel process created for this calculation' }
|
|
67
|
+
$excelProcess = Get-Process -Id ([int]$windowPid) -ErrorAction Stop
|
|
68
|
+
if ($excelProcess.ProcessName -ine 'EXCEL') { throw 'Excel window belongs to an unexpected process' }
|
|
69
|
+
if ($existingExcelPids -contains $excelProcess.Id) { throw 'Excel COM attached to a pre-existing user Excel process' }
|
|
70
|
+
$excelPid = $excelProcess.Id
|
|
71
|
+
$excelStartFileTimeUtc = [int64]$excelProcess.StartTime.ToUniversalTime().ToFileTimeUtc()
|
|
72
|
+
$ownedExcel = $true
|
|
73
|
+
Write-State 'running'
|
|
74
|
+
|
|
75
|
+
$excel.Visible = $false
|
|
76
|
+
$excel.DisplayAlerts = $false
|
|
77
|
+
$excel.EnableEvents = $false
|
|
78
|
+
$excel.ScreenUpdating = $false
|
|
79
|
+
$excel.AskToUpdateLinks = $false
|
|
80
|
+
$excel.AutomationSecurity = 3
|
|
81
|
+
$excel.UserControl = $false
|
|
82
|
+
# Iterative calculation can make a circular workbook appear to have a
|
|
83
|
+
# stable value. It is never safe for a conflict snapshot.
|
|
84
|
+
if ($excel.Iteration) { throw 'Excel iterative calculation is enabled' }
|
|
85
|
+
|
|
86
|
+
# UpdateLinks=0 and ReadOnly=true are positional arguments in the Excel COM
|
|
87
|
+
# API. The temporary source is never saved in place.
|
|
88
|
+
$book = $excel.Workbooks.Open($source, 0, $true)
|
|
89
|
+
if ($excel.Iteration) { throw 'Workbook enables iterative calculation' }
|
|
90
|
+
$excel.CalculateFullRebuild()
|
|
91
|
+
$deadline = [DateTime]::UtcNow.AddSeconds($timeoutSeconds)
|
|
92
|
+
while ($excel.CalculationState -ne 0) { # xlDone
|
|
93
|
+
if ([DateTime]::UtcNow -gt $deadline) { throw 'Excel calculation did not reach xlDone before timeout' }
|
|
94
|
+
Start-Sleep -Milliseconds 100
|
|
95
|
+
}
|
|
96
|
+
if ([DateTime]::UtcNow -gt $deadline) { throw 'Excel calculation exceeded timeout' }
|
|
97
|
+
try {
|
|
98
|
+
$circular = $excel.CircularReference
|
|
99
|
+
if ($null -ne $circular) { throw 'Workbook contains a circular reference' }
|
|
100
|
+
} catch {
|
|
101
|
+
if ($_.Exception.Message -like '*circular reference*') { throw }
|
|
102
|
+
throw "Unable to verify circular references: $($_.Exception.Message)"
|
|
103
|
+
}
|
|
104
|
+
# Value2 is used only as a typed error probe. The actual typed value is
|
|
105
|
+
# read from Excel's serialized cache by openpyxl so dates retain the
|
|
106
|
+
# workbook epoch/number format and booleans never become numeric 1/0.
|
|
107
|
+
foreach ($sheet in $book.Worksheets) {
|
|
108
|
+
$used = $sheet.UsedRange
|
|
109
|
+
foreach ($cell in $used.Cells) {
|
|
110
|
+
if ($cell.HasFormula) {
|
|
111
|
+
try {
|
|
112
|
+
if ($excel.WorksheetFunction.IsError($cell.Value2)) {
|
|
113
|
+
throw "Excel returned an error value at $($sheet.Name)!$($cell.Address($false,$false))"
|
|
114
|
+
}
|
|
115
|
+
} catch {
|
|
116
|
+
if ($_.Exception.Message -like '*returned an error value*') { throw }
|
|
117
|
+
throw "Unable to verify formula result at $($sheet.Name)!$($cell.Address($false,$false)): $($_.Exception.Message)"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
$book.SaveCopyAs($result)
|
|
123
|
+
if (!(Test-Path -LiteralPath $result -PathType Leaf)) { throw 'Excel did not produce a result file' }
|
|
124
|
+
Write-State 'completed'
|
|
125
|
+
exit 0
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
Write-State 'failed' $_.Exception.Message
|
|
129
|
+
exit 1
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
if ($null -ne $book) {
|
|
133
|
+
try { $book.Close($false) } catch { }
|
|
134
|
+
try { [Runtime.InteropServices.Marshal]::ReleaseComObject($book) | Out-Null } catch { }
|
|
135
|
+
}
|
|
136
|
+
if ($null -ne $excel -and $ownedExcel) {
|
|
137
|
+
try { $excel.Quit() } catch { }
|
|
138
|
+
}
|
|
139
|
+
if ($null -ne $excel) {
|
|
140
|
+
try { [Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null } catch { }
|
|
141
|
+
}
|
|
142
|
+
[GC]::Collect()
|
|
143
|
+
[GC]::WaitForPendingFinalizers()
|
|
144
|
+
# Quit can leave an orphaned hidden Excel process while COM child ranges
|
|
145
|
+
# are being finalized. Ownership is exact: PID + creation timestamp.
|
|
146
|
+
if ($null -ne $excelProcess -and $null -ne $excelPid) {
|
|
147
|
+
try {
|
|
148
|
+
$left = Get-Process -Id ([int]$excelPid) -ErrorAction SilentlyContinue
|
|
149
|
+
if ($left -and $left.ProcessName -ieq 'EXCEL' -and
|
|
150
|
+
$left.StartTime.ToUniversalTime().ToFileTimeUtc() -eq $excelStartFileTimeUtc) {
|
|
151
|
+
Stop-Process -Id ([int]$excelPid) -Force -ErrorAction SilentlyContinue
|
|
152
|
+
}
|
|
153
|
+
} catch { }
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -30,6 +30,14 @@ SCRIPT_PATH = Path(__file__).resolve()
|
|
|
30
30
|
PACKAGE_ROOT = SCRIPT_DIR.parent
|
|
31
31
|
|
|
32
32
|
|
|
33
|
+
def _configure_stdio() -> None:
|
|
34
|
+
"""Keep structured CLI output UTF-8 even under legacy Windows code pages."""
|
|
35
|
+
for stream in (sys.stdout, sys.stderr):
|
|
36
|
+
reconfigure = getattr(stream, "reconfigure", None)
|
|
37
|
+
if callable(reconfigure):
|
|
38
|
+
reconfigure(encoding="utf-8", errors="replace")
|
|
39
|
+
|
|
40
|
+
|
|
33
41
|
def _print(data) -> None:
|
|
34
42
|
print(json.dumps(data, ensure_ascii=False, indent=2))
|
|
35
43
|
|
|
@@ -146,6 +154,7 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
146
154
|
|
|
147
155
|
|
|
148
156
|
def main(argv: list[str] | None = None) -> int:
|
|
157
|
+
_configure_stdio()
|
|
149
158
|
args = build_parser().parse_args(argv)
|
|
150
159
|
try:
|
|
151
160
|
# serve/apply 仅依赖 manifest(其 repoRoot 在 apply_manifest 内部解析),
|