Skip to content

Commit

Permalink
cell-text length limit
Browse files Browse the repository at this point in the history
  • Loading branch information
mustaddon committed May 25, 2023
1 parent 78a58d4 commit b35819a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions ArrayToExcel.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DFA43145-49CC-4FD6-B5A8-C108DBCA5625}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
pack.ps1 = pack.ps1
README.md = README.md
EndProjectSection
EndProject
Expand Down
17 changes: 13 additions & 4 deletions ArrayToExcel/ArrayToExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ static string NormSheetName(string? value, uint sheetId, HashSet<string> existNa
if (string.IsNullOrWhiteSpace(value) || existNames.Contains(value))
return $"Sheet{sheetId}";

if (value.Length > 31)
return $"{value.Substring(0, 28)}...";
if (value.Length > _maxSheetName)
return value.Substring(0, _maxSheetName);

return value;
}
Expand Down Expand Up @@ -230,17 +230,22 @@ static CellValue GetCellValue(object? value)
if (type == typeof(float))
return new(((float)value).ToString(_cultureInfo));

return new(_invalidXmlChars.Replace(value.ToString()!, string.Empty));
return new(NormCellText(value.ToString()!));
}

static InlineString GetInlineString(string value)
{
return new InlineString(new Text(_invalidXmlChars.Replace(value, string.Empty))
return new InlineString(new Text(NormCellText(value))
{
Space = SpaceProcessingModeValues.Preserve
});
}

static string NormCellText(string value)
{
return _invalidXmlChars.Replace(value.Length > _maxCellText ? value.Substring(0, _maxCellText) : value, string.Empty);
}

static CellValues GetCellType(object? value)
{
var type = value?.GetType() ?? typeof(object);
Expand Down Expand Up @@ -291,5 +296,9 @@ static string GetColReference(int index)
static readonly Regex _invalidXmlChars = new(@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled);

static readonly Regex _invalidSheetNameChars = new(@"[:?*\\/\[\]\r\n]|[\uDC00-\uDFFF]|[\uD800-\uDBFF]|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled);

const int _maxSheetName = 31;

const int _maxCellText = 32767;
}
}
8 changes: 4 additions & 4 deletions ArrayToExcel/ArrayToExcel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<Nullable>enable</Nullable>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\ArrayToExcel.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>2.2.1</AssemblyVersion>
<FileVersion>2.2.1</FileVersion>
<Version>2.2.1</Version>
<AssemblyVersion>2.2.2</AssemblyVersion>
<FileVersion>2.2.2</FileVersion>
<Version>2.2.2</Version>
<Company></Company>
<Authors>Leonid Salavatov</Authors>
<Copyright>Leonid Salavatov 2022</Copyright>
Expand All @@ -28,7 +28,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.18.0" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions pack.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dotnet build -c Release
dotnet pack .\ArrayToExcel\ -c Release -o ..\_publish

0 comments on commit b35819a

Please sign in to comment.