@kevlns/xlmerge 2.0.1 → 2.0.3
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 +169 -141
- package/LICENSE +21 -21
- package/README.md +111 -193
- package/bin/xlmerge.js +152 -152
- package/docs/documentation.md +24 -0
- package/package.json +7 -5
- package/python/xlsx_merge_engine/__init__.py +2 -2
- package/python/xlsx_merge_engine/xlsx_git_merge_bridge.py +1614 -1611
- package/python/xlsx_resolver/__init__.py +2 -2
- package/python/xlsx_resolver/__main__.py +4 -4
- package/python/xlsx_resolver/formula_values.py +660 -565
- package/python/xlsx_resolver/loading_ui.html +44 -0
- package/python/xlsx_resolver/merge_server.py +179 -126
- package/python/xlsx_resolver/merge_ui.html +1634 -1522
- package/python/xlsx_resolver/prepare_progress.py +34 -0
- package/python/xlsx_resolver/recalculate_workbook.ps1 +141 -155
- package/python/xlsx_resolver/resolve_xlsx_conflict.py +236 -222
- package/python/xlsx_resolver/xlsx_conflict.py +2317 -1994
- package/v-cli.plugin.json +248 -194
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Per-preparation progress and cooperative cancellation; no global job state."""
|
|
2
|
+
from contextvars import ContextVar
|
|
3
|
+
from contextlib import contextmanager
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class PreparationCancelled(RuntimeError):
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
_current = ContextVar('xlmerge_preparation', default=None)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@contextmanager
|
|
14
|
+
def preparation_context(report, cancelled):
|
|
15
|
+
token = _current.set((report, cancelled))
|
|
16
|
+
try:
|
|
17
|
+
yield
|
|
18
|
+
finally:
|
|
19
|
+
_current.reset(token)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def checkpoint(**progress):
|
|
23
|
+
context = _current.get()
|
|
24
|
+
if context is None:
|
|
25
|
+
return
|
|
26
|
+
report, cancelled = context
|
|
27
|
+
if cancelled.is_set():
|
|
28
|
+
raise PreparationCancelled('解析已中断,工作树未修改。')
|
|
29
|
+
if progress:
|
|
30
|
+
report(progress)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def cancellable():
|
|
34
|
+
return _current.get() is not None
|
|
@@ -1,155 +1,141 @@
|
|
|
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
|
-
#
|
|
105
|
-
#
|
|
106
|
-
#
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if ($null -ne $
|
|
133
|
-
try {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
}
|
|
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
|
+
# Save the completed calculation once. Python verifies every original
|
|
105
|
+
# formula's serialized cache and cell type, including errors and missing
|
|
106
|
+
# results. Traversing UsedRange through COM duplicates that validation and
|
|
107
|
+
# also visits potentially millions of formatted, non-formula cells.
|
|
108
|
+
$book.SaveCopyAs($result)
|
|
109
|
+
if (!(Test-Path -LiteralPath $result -PathType Leaf)) { throw 'Excel did not produce a result file' }
|
|
110
|
+
Write-State 'completed'
|
|
111
|
+
exit 0
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
Write-State 'failed' $_.Exception.Message
|
|
115
|
+
exit 1
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
if ($null -ne $book) {
|
|
119
|
+
try { $book.Close($false) } catch { }
|
|
120
|
+
try { [Runtime.InteropServices.Marshal]::ReleaseComObject($book) | Out-Null } catch { }
|
|
121
|
+
}
|
|
122
|
+
if ($null -ne $excel -and $ownedExcel) {
|
|
123
|
+
try { $excel.Quit() } catch { }
|
|
124
|
+
}
|
|
125
|
+
if ($null -ne $excel) {
|
|
126
|
+
try { [Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null } catch { }
|
|
127
|
+
}
|
|
128
|
+
[GC]::Collect()
|
|
129
|
+
[GC]::WaitForPendingFinalizers()
|
|
130
|
+
# Quit can leave an orphaned hidden Excel process while COM child ranges
|
|
131
|
+
# are being finalized. Ownership is exact: PID + creation timestamp.
|
|
132
|
+
if ($null -ne $excelProcess -and $null -ne $excelPid) {
|
|
133
|
+
try {
|
|
134
|
+
$left = Get-Process -Id ([int]$excelPid) -ErrorAction SilentlyContinue
|
|
135
|
+
if ($left -and $left.ProcessName -ieq 'EXCEL' -and
|
|
136
|
+
$left.StartTime.ToUniversalTime().ToFileTimeUtc() -eq $excelStartFileTimeUtc) {
|
|
137
|
+
Stop-Process -Id ([int]$excelPid) -Force -ErrorAction SilentlyContinue
|
|
138
|
+
}
|
|
139
|
+
} catch { }
|
|
140
|
+
}
|
|
141
|
+
}
|