Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Commit a062a86

Browse files
committed
Potential fix for Issue #12 - pending testing
1 parent 262b1e0 commit a062a86

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

src/Simplexcel.TestApp/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void Main(string[] args)
2828
sheet.Cells[0, 2].Bold = true;
2929
sheet.Cells[0, 2].TextColor = Color.Red;
3030

31-
Cell cell = "BIU Big Blue";
31+
Cell cell = "BIU & Big & Blue";
3232
cell.Bold = true;
3333
cell.Underline = true;
3434
cell.Italic = true;
@@ -55,6 +55,9 @@ static void Main(string[] args)
5555
cell2.FontSize = 18;
5656
sheet.Cells[0, 2] = cell2;
5757

58+
sheet.Cells[0, 6] = "👪";
59+
sheet.Cells[0, 7] = "👨‍👩‍👧‍👦";
60+
5861
wb.Add(sheet);
5962

6063
var sheet2 = new Worksheet("Sheet 2");

src/Simplexcel.Tests/BugTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Simplexcel.XlsxInternal;
2+
using System.Linq;
3+
using Xunit;
4+
5+
namespace Simplexcel.Tests
6+
{
7+
public class BugTests
8+
{
9+
/// <summary>
10+
/// https://github.com/mstum/Simplexcel/issues/12
11+
///
12+
/// SharedStrings _sanitizeRegex makes "&" in CellStrings impossible
13+
/// </summary>
14+
[Fact]
15+
public void Issue12_AmpersandInCellValues()
16+
{
17+
var sharedStrings = new SharedStrings();
18+
sharedStrings.GetStringIndex("Here & Now");
19+
sharedStrings.GetStringIndex("&");
20+
var xmlFile = sharedStrings.ToXmlFile();
21+
22+
var nodeValues = xmlFile.Content.Descendants(Namespaces.x + "t").Select(x => x.Value).ToList();
23+
24+
Assert.Contains("Here & Now", nodeValues);
25+
Assert.Contains("&", nodeValues);
26+
}
27+
}
28+
}

src/Simplexcel.Tests/Simplexcel.Tests.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp1.1</TargetFramework>
@@ -15,4 +15,8 @@
1515
<ProjectReference Include="..\Simplexcel\Simplexcel.csproj" />
1616
</ItemGroup>
1717

18+
<ItemGroup>
19+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
20+
</ItemGroup>
21+
1822
</Project>

src/Simplexcel/InternalsVisibleTo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
using System.Runtime.CompilerServices;
2+
[assembly: InternalsVisibleTo("Simplexcel.Tests")]

src/Simplexcel/XlsxInternal/SharedStrings.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace Simplexcel.XlsxInternal
1313
internal class SharedStrings
1414
{
1515
private readonly Dictionary<string, int> _sharedStrings = new Dictionary<string, int>(StringComparer.Ordinal);
16-
private static Regex _sanitizeRegex = new Regex("[\x00-\x08\x0B\x0C\x0E-\x1F\x26]", RegexOptions.Compiled);
1716

1817
/// <summary>
1918
/// The number of Unique Strings
@@ -63,9 +62,11 @@ internal XmlFile ToXmlFile()
6362

6463
foreach (var kvp in _sharedStrings.OrderBy(k => k.Value))
6564
{
66-
var str = _sanitizeRegex.Replace(kvp.Key, string.Empty);
67-
var se = new XElement(Namespaces.x + "si", new XElement(Namespaces.x + "t", str));
68-
sst.Root.Add(se);
65+
var tElem = new XElement(Namespaces.x + "t");
66+
tElem.Value = kvp.Key;
67+
68+
var siElem = new XElement(Namespaces.x + "si", tElem);
69+
sst.Root.Add(siElem);
6970
}
7071

7172
file.Content = sst;

0 commit comments

Comments
 (0)