Skip to content

Commit fc0ed0c

Browse files
committed
split out Workbook
1 parent 7ce03c7 commit fc0ed0c

7 files changed

Lines changed: 149 additions & 55 deletions

src/DeterministicIoPackaging/DeterministicPackage.cs

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,11 @@
33

44
namespace DeterministicIoPackaging;
55

6-
public static class DeterministicPackage
6+
public static partial class DeterministicPackage
77
{
88
public static DateTime StableDate { get; } = new(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc);
99
public static DateTimeOffset StableDateOffset { get; } = new(StableDate);
1010

11-
public static MemoryStream Convert(Stream source)
12-
{
13-
var target = new MemoryStream();
14-
Convert(source, target);
15-
target.Position = 0;
16-
return target;
17-
}
18-
19-
public static async Task<MemoryStream> ConvertAsync(Stream source)
20-
{
21-
var target = new MemoryStream();
22-
await ConvertAsync(source, target);
23-
target.Position = 0;
24-
return target;
25-
}
26-
27-
public static void Convert(Stream source, Stream target)
28-
{
29-
using var sourceArchive = ReadArchive(source);
30-
using var targetArchive = CreateArchive(target);
31-
foreach (var sourceEntry in sourceArchive.Entries)
32-
{
33-
DuplicateEntry(sourceEntry, targetArchive);
34-
}
35-
}
36-
37-
public static async Task ConvertAsync(Stream source, Stream target, Cancel token = default)
38-
{
39-
using var sourceArchive = ReadArchive(source);
40-
using var targetArchive = CreateArchive(target);
41-
foreach (var sourceEntry in sourceArchive.Entries)
42-
{
43-
await DuplicateEntryAsync(sourceEntry, targetArchive, token);
44-
}
45-
}
46-
4711
static Archive CreateArchive(Stream target) => new(target, ZipArchiveMode.Create, leaveOpen: true);
4812

4913
static Archive ReadArchive(Stream source)
@@ -81,9 +45,9 @@ static void DuplicateEntry(Entry sourceEntry, Archive targetArchive)
8145
return;
8246
}
8347

84-
if (IsWorkbookXml(sourceEntry))
48+
if (sourceEntry.IsWorkbookXml())
8549
{
86-
var xml = PatchWorkbook(sourceStream);
50+
var xml = Workbook.Patch(sourceStream);
8751
SaveXml(xml, targetStream);
8852
return;
8953
}
@@ -115,9 +79,9 @@ static async Task DuplicateEntryAsync(Entry sourceEntry, Archive targetArchive,
11579
return;
11680
}
11781

118-
if (IsWorkbookXml(sourceEntry))
82+
if (sourceEntry.IsWorkbookXml())
11983
{
120-
var xml = PatchWorkbook(sourceStream);
84+
var xml = Workbook.Patch(sourceStream);
12185
await SaveXml(xml, targetStream, cancel);
12286
return;
12387
}
@@ -167,20 +131,16 @@ static bool IsPsmdcpElement(XElement rel)
167131
return target.Value.EndsWith(".psmdcp");
168132
}
169133
}
170-
171-
static XNamespace mc = "http://schemas.openxmlformats.org/markup-compatibility/2006";
172-
static XNamespace x15ac = "http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac";
173-
174-
static XDocument PatchWorkbook(Stream sourceStream)
134+
static XDocument PatchSheet(Stream sourceStream)
175135
{
176136
var xml = XDocument.Load(sourceStream);
137+
return PatchSheet(xml);
138+
}
177139

178-
var absPath = xml
179-
.Descendants(mc + "AlternateContent")
180-
.FirstOrDefault(_ => _.Descendants(x15ac + "absPath").Any());
181-
182-
absPath?.Remove();
183-
140+
internal static XDocument PatchSheet(XDocument xml)
141+
{
142+
XNamespace xr = "http://schemas.microsoft.com/office/spreadsheetml/2014/revision";
143+
xml.Root!.Attribute(xr + "uid")?.Remove();
184144
return xml;
185145
}
186146

@@ -201,6 +161,4 @@ static bool IsRelationships(Entry _) =>
201161
static bool IsWorkbookRelationships(Entry _) =>
202162
_.FullName is "xl/_rels/workbook.xml.rels";
203163

204-
static bool IsWorkbookXml(Entry _) =>
205-
_.FullName == "xl/workbook.xml";
206164
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace DeterministicIoPackaging;
2+
3+
public static partial class DeterministicPackage
4+
{
5+
public static MemoryStream Convert(Stream source)
6+
{
7+
var target = new MemoryStream();
8+
Convert(source, target);
9+
target.Position = 0;
10+
return target;
11+
}
12+
13+
public static async Task<MemoryStream> ConvertAsync(Stream source)
14+
{
15+
var target = new MemoryStream();
16+
await ConvertAsync(source, target);
17+
target.Position = 0;
18+
return target;
19+
}
20+
21+
public static void Convert(Stream source, Stream target)
22+
{
23+
using var sourceArchive = ReadArchive(source);
24+
using var targetArchive = CreateArchive(target);
25+
foreach (var sourceEntry in sourceArchive.Entries)
26+
{
27+
DuplicateEntry(sourceEntry, targetArchive);
28+
}
29+
}
30+
31+
public static async Task ConvertAsync(Stream source, Stream target, Cancel token = default)
32+
{
33+
using var sourceArchive = ReadArchive(source);
34+
using var targetArchive = CreateArchive(target);
35+
foreach (var sourceEntry in sourceArchive.Entries)
36+
{
37+
await DuplicateEntryAsync(sourceEntry, targetArchive, token);
38+
}
39+
}
40+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Entry = System.IO.Compression.ZipArchiveEntry;
2+
3+
static class Workbook
4+
{
5+
static XNamespace mc = "http://schemas.openxmlformats.org/markup-compatibility/2006";
6+
static XNamespace x15ac = "http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac";
7+
8+
public static XDocument Patch(Stream sourceStream)
9+
{
10+
var xml = XDocument.Load(sourceStream);
11+
12+
var absPath = xml
13+
.Descendants(mc + "AlternateContent")
14+
.FirstOrDefault(_ => _.Descendants(x15ac + "absPath").Any());
15+
16+
absPath?.Remove();
17+
18+
return xml;
19+
}
20+
21+
public static bool IsWorkbookXml(this Entry entry) =>
22+
entry.FullName == "xl/workbook.xml";
23+
}

src/Tests/PatchRelationshipsTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ public Task RunWithoutIdPatch()
3333

3434
return Verify(xml);
3535
}
36-
}
36+
}
37+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" mc:Ignorable="x14ac xr xr2 xr3">
2+
<dimension ref="A5" />
3+
<sheetViews>
4+
<sheetView tabSelected="1" workbookViewId="0" topLeftCell="A1" />
5+
</sheetViews>
6+
<sheetFormatPr defaultRowHeight="12.75" />
7+
<sheetData>
8+
<row r="5" spans="1:1" ht="23.25" customHeight="1">
9+
<c r="A5" s="3" t="s">
10+
<v>30</v>
11+
</c>
12+
</row>
13+
</sheetData>
14+
<pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.5" footer="0.5" />
15+
</worksheet>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" mc:Ignorable="x14ac xr xr2 xr3">
2+
<dimension ref="A5" />
3+
<sheetViews>
4+
<sheetView tabSelected="1" workbookViewId="0" topLeftCell="A1" />
5+
</sheetViews>
6+
<sheetFormatPr defaultRowHeight="12.75" />
7+
<sheetData>
8+
<row r="5" spans="1:1" ht="23.25" customHeight="1">
9+
<c r="A5" s="3" t="s">
10+
<v>30</v>
11+
</c>
12+
</row>
13+
</sheetData>
14+
<pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.5" footer="0.5" />
15+
</worksheet>

src/Tests/PatchSheetTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[TestFixture]
2+
public class PatchSheetTests
3+
{
4+
[Test]
5+
public Task Run()
6+
{
7+
var xml = XDocument.Parse(
8+
"""
9+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
10+
<worksheet
11+
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
12+
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
13+
xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
14+
xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"
15+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
16+
xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"
17+
xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main"
18+
xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision"
19+
xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2"
20+
xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3"
21+
mc:Ignorable="x14ac xr xr2 xr3"
22+
xr:uid="{F81C51CE-C559-4BE6-9436-ED68E7BEB9A9}">
23+
<dimension ref="A5"/>
24+
<sheetViews>
25+
<sheetView tabSelected="1" workbookViewId="0" topLeftCell="A1"/>
26+
</sheetViews>
27+
<sheetFormatPr defaultRowHeight="12.75"/>
28+
<sheetData>
29+
<row r="5" spans="1:1" ht="23.25" customHeight="1">
30+
<c r="A5" s="3" t="s">
31+
<v>30</v>
32+
</c>
33+
</row>
34+
</sheetData>
35+
<pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.5" footer="0.5"/>
36+
</worksheet>
37+
""");
38+
DeterministicPackage.PatchSheet(xml);
39+
40+
return Verify(xml);
41+
}
42+
}

0 commit comments

Comments
 (0)