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

Commit

Permalink
Potential fix for Issue #12 - pending testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mstum committed May 17, 2017
1 parent 262b1e0 commit a062a86
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/Simplexcel.TestApp/Program.cs
Expand Up @@ -28,7 +28,7 @@ static void Main(string[] args)
sheet.Cells[0, 2].Bold = true;
sheet.Cells[0, 2].TextColor = Color.Red;

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

sheet.Cells[0, 6] = "👪";
sheet.Cells[0, 7] = "👨‍👩‍👧‍👦";

wb.Add(sheet);

var sheet2 = new Worksheet("Sheet 2");
Expand Down
28 changes: 28 additions & 0 deletions src/Simplexcel.Tests/BugTests.cs
@@ -0,0 +1,28 @@
using Simplexcel.XlsxInternal;
using System.Linq;
using Xunit;

namespace Simplexcel.Tests
{
public class BugTests
{
/// <summary>
/// https://github.com/mstum/Simplexcel/issues/12
///
/// SharedStrings _sanitizeRegex makes "&" in CellStrings impossible
/// </summary>
[Fact]
public void Issue12_AmpersandInCellValues()
{
var sharedStrings = new SharedStrings();
sharedStrings.GetStringIndex("Here & Now");
sharedStrings.GetStringIndex("&");
var xmlFile = sharedStrings.ToXmlFile();

var nodeValues = xmlFile.Content.Descendants(Namespaces.x + "t").Select(x => x.Value).ToList();

Assert.Contains("Here & Now", nodeValues);
Assert.Contains("&", nodeValues);
}
}
}
6 changes: 5 additions & 1 deletion src/Simplexcel.Tests/Simplexcel.Tests.csproj
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
Expand All @@ -15,4 +15,8 @@
<ProjectReference Include="..\Simplexcel\Simplexcel.csproj" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions src/Simplexcel/InternalsVisibleTo.cs
@@ -0,0 +1,2 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Simplexcel.Tests")]
9 changes: 5 additions & 4 deletions src/Simplexcel/XlsxInternal/SharedStrings.cs
Expand Up @@ -13,7 +13,6 @@ namespace Simplexcel.XlsxInternal
internal class SharedStrings
{
private readonly Dictionary<string, int> _sharedStrings = new Dictionary<string, int>(StringComparer.Ordinal);
private static Regex _sanitizeRegex = new Regex("[\x00-\x08\x0B\x0C\x0E-\x1F\x26]", RegexOptions.Compiled);

/// <summary>
/// The number of Unique Strings
Expand Down Expand Up @@ -63,9 +62,11 @@ internal XmlFile ToXmlFile()

foreach (var kvp in _sharedStrings.OrderBy(k => k.Value))
{
var str = _sanitizeRegex.Replace(kvp.Key, string.Empty);
var se = new XElement(Namespaces.x + "si", new XElement(Namespaces.x + "t", str));
sst.Root.Add(se);
var tElem = new XElement(Namespaces.x + "t");
tElem.Value = kvp.Key;

var siElem = new XElement(Namespaces.x + "si", tElem);
sst.Root.Add(siElem);
}

file.Content = sst;
Expand Down

0 comments on commit a062a86

Please sign in to comment.