Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excel Worksheet Name Limitations #345

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Excel_Adapter/CRUD/Create/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ private bool Create(IXLWorkbook workbook, Table table, ExcelPushConfig config)
return false;
}

string workSheetName = Validation.WorksheetName(table.Name, workbook);

try
{
IXLWorksheet worksheet = workbook.AddWorksheet(table.Name);
IXLWorksheet worksheet = workbook.AddWorksheet(workSheetName);

string startingCell = config?.StartingCell == null ? "A1" : config.StartingCell.ToExcel();
if (string.IsNullOrWhiteSpace(startingCell))
Expand All @@ -59,10 +61,6 @@ private bool Create(IXLWorkbook workbook, Table table, ExcelPushConfig config)
return false;
}
}

/***************************************************/
}
}



}
6 changes: 4 additions & 2 deletions Excel_Adapter/Excel_Adapter.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -107,6 +107,8 @@
<Compile Include="CRUD\Update\UpdateWorkbookProperties.cs" />
<Compile Include="ExcelAdapter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Validation\WorksheetName.cs" />
<Compile Include="Validation\Enums\WorksheetValidation.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -132,4 +134,4 @@

</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>
35 changes: 35 additions & 0 deletions Excel_Adapter/Validation/Enums/WorksheetValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2022, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

namespace BH.Adapter.Excel
{
public enum WorksheetValidation
{
BeginOrEndWithInvalidCharacter,
Blank,
NotUnique,
NotValidCharacters,
NotWithinCharacterLimit,
ReservedWord,
Valid
}
}
194 changes: 194 additions & 0 deletions Excel_Adapter/Validation/WorksheetName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2022, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace BH.Adapter.Excel
{
public partial class Validation
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

public static string WorksheetName(string name, IXLWorkbook workbook)
{
string worksheetName = name;

WorksheetValidation issue = IsValidName(worksheetName, workbook);
while (issue != WorksheetValidation.Valid)
{
worksheetName = ModifyWorksheetName(worksheetName, issue);
issue = IsValidName(worksheetName, workbook);
}

if (worksheetName != name)
{
BH.Engine.Base.Compute.RecordNote($"The selected name for worksheet {name} was not valid to Excel requirements. This has been renamed to {worksheetName}.");
}

return worksheetName;
}

/*************************************************/
/**** Private Methods ***/
/*************************************************/

private static WorksheetValidation IsValidName(string workSheetName, IXLWorkbook workbook)
{
if (!CheckIsUnique(workSheetName, workbook))
return WorksheetValidation.NotUnique;

if (string.IsNullOrWhiteSpace(workSheetName))
return WorksheetValidation.Blank;

if (!CheckIsNotReservedWord(workSheetName))
return WorksheetValidation.ReservedWord;

if (!CheckIsValidCharacters(workSheetName))
return WorksheetValidation.NotValidCharacters;

if (!CheckIsNotBeginOrEndWithInvalidCharacter(workSheetName))
return WorksheetValidation.BeginOrEndWithInvalidCharacter;

if (!CheckIsWithinCharacterLimit(workSheetName))
return WorksheetValidation.NotWithinCharacterLimit;

return WorksheetValidation.Valid;
}

/************ Checks *********************/

travispotterBH marked this conversation as resolved.
Show resolved Hide resolved
private static bool CheckIsUnique(string workSheetName, IXLWorkbook workbook)
{
return !workbook.Worksheets.Contains(workSheetName);
}

private static bool CheckIsNotReservedWord(string workSheetName)
{
List<string> reservedWords = new List<string> { "history" };

return !reservedWords.Contains(workSheetName.ToLower());
}

private static bool CheckIsValidCharacters(string workSheetName)
{
Regex r = new Regex(@"[\[/\?\]\*\\\:]");

return !r.IsMatch(workSheetName);
}

private static bool CheckIsNotBeginOrEndWithInvalidCharacter(string workSheetName)
{
return !workSheetName.StartsWith("\'") && !workSheetName.EndsWith("\'");
}

private static bool CheckIsWithinCharacterLimit(string workSheetName)
{
return workSheetName.Length <= 31;
}

/************ Modifications *********************/
private static string ModifyWorksheetName(string worksheetName, WorksheetValidation issue)
{
switch (issue)
{
case WorksheetValidation.ReservedWord:
return SetUniqueNameWithReservedName(worksheetName);

case WorksheetValidation.Blank:
return SetGeneralName();

case WorksheetValidation.NotUnique:
return SetUniqueName(worksheetName);

case WorksheetValidation.BeginOrEndWithInvalidCharacter:
return RemoveInvalidBeginningOrEnding(worksheetName);

case WorksheetValidation.NotValidCharacters:
return RemoveInvalidCharacters(worksheetName);

case WorksheetValidation.NotWithinCharacterLimit:
return TrimName(worksheetName);

default:
case WorksheetValidation.Valid:
break;
}

return worksheetName;
}

private static string SetUniqueNameWithReservedName(string worksheetName)
{
return "BHoM_" + worksheetName;
}

private static string SetGeneralName()
{
return "BHoM_Export_" + DateTime.Now.ToString("ddMMyy_HHmmss");
}

private static string SetUniqueName(string worksheetName)
{
return DateTime.Now.ToString("ddMMyy HHmmssf") + "_" + worksheetName;
}

private static string RemoveInvalidBeginningOrEnding(string worksheetName)
{
if (worksheetName.StartsWith("\'"))
{
worksheetName = worksheetName.Substring(1);
}

if (worksheetName.EndsWith("\'"))
{
worksheetName = worksheetName.Substring(0, worksheetName.Length - 2);
}

return worksheetName;
}

private static string RemoveInvalidCharacters(string worksheetName)
{
List<char> invalidChars = new List<char>() { '[', '/', '?', ']', '*', '\\', ':' };

invalidChars.ForEach(c => worksheetName = worksheetName.Replace(c.ToString(), String.Empty));
return worksheetName;
}

private static string TrimName(string worksheetName)
{
if (worksheetName.Contains(" ") || worksheetName.Contains("-") || worksheetName.Contains("_") || worksheetName.Contains(","))
{
List<char> toBeRemovedChars = new List<char>() { ' ', '-', '_', ',' };
toBeRemovedChars.ForEach(c => worksheetName = worksheetName.Replace(c.ToString(), String.Empty));
return worksheetName;
}

return worksheetName.Substring(0, 31);
}
}
}