-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcelColumnMetadataTests.cs
More file actions
106 lines (97 loc) · 3.46 KB
/
Copy pathExcelColumnMetadataTests.cs
File metadata and controls
106 lines (97 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Exercises ColumnInfo.Metadata sourced from a custom XML part shaped like
// <sheet name="..."><column index="N" {anyAttr}/></sheet>. The reader is namespace-agnostic
// and surfaces every attribute (other than index) verbatim.
[TestFixture]
public class ExcelColumnMetadataTests
{
[Test]
public Task ColumnMetadataFromCustomXml()
{
using var document = CreateDocument(
"""
<columnMetadata xmlns="urn:test:column-metadata">
<sheet name="Sheet1">
<column index="1" property="Id" nullable="false" />
<column index="2" property="Name" />
<column index="3" property="Notes" nullable="true" />
</sheet>
</columnMetadata>
""");
return Verify(document);
}
[Test]
public Task ColumnMetadataIgnoresNamespace()
{
// No xmlns at all — reader still picks it up by structure.
using var document = CreateDocument(
"""
<whatever>
<sheet name="Sheet1">
<column index="1" foo="bar" />
<column index="2" baz="qux" />
</sheet>
</whatever>
""");
return Verify(document);
}
[Test]
public Task SheetMetadataFromCustomXml()
{
// Sheet-level attributes (anything other than `name`) flow to SheetInfo.Metadata. The
// mechanism mirrors ColumnInfo.Metadata so producers like Excelsior can attach per-sheet
// annotations such as `bannerRows="1"` without coordinating schema changes here.
using var document = CreateDocument(
"""
<columnMetadata xmlns="urn:test:column-metadata">
<sheet name="Sheet1" bannerRows="1" origin="import">
<column index="1" property="Id" />
</sheet>
</columnMetadata>
""");
return Verify(document);
}
[Test]
public Task ColumnMetadataMissingPartIsBenign()
{
// No custom XML part at all — Metadata stays null on each column.
using var document = CreateDocument(customXml: null);
return Verify(document);
}
static SpreadsheetDocument CreateDocument(string? customXml)
{
var document = SpreadsheetDocument.Create(new MemoryStream(), SpreadsheetDocumentType.Workbook);
var wbPart = document.AddWorkbookPart();
wbPart.Workbook = new(new Sheets());
var wsPart = wbPart.AddNewPart<WorksheetPart>();
wsPart.Worksheet = new(
new SheetData(
new Row(
Header("Id"),
Header("Name"),
Header("Notes"))
{
RowIndex = 1u
}));
var sheets = wbPart.Workbook.GetFirstChild<Sheets>()!;
sheets.Append(new Sheet
{
Id = wbPart.GetIdOfPart(wsPart),
SheetId = 1,
Name = "Sheet1"
});
if (customXml != null)
{
var customPart = wbPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
using var stream = customPart.GetStream(FileMode.Create);
using var writer = new StreamWriter(stream);
writer.Write(customXml);
}
return document;
static Cell Header(string value) =>
new()
{
DataType = CellValues.InlineString,
InlineString = new(new Text(value))
};
}
}