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 5 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
11 changes: 5 additions & 6 deletions Excel_Adapter/CRUD/Create/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ public partial class ExcelAdapter

private bool Create(IXLWorkbook workbook, Table table, ExcelPushConfig config)
{

travispotterBH marked this conversation as resolved.
Show resolved Hide resolved
if (table?.Data == null)
{
BH.Engine.Base.Compute.RecordError("Creation of a table failed: input table is null or does not contain a table.");
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 +62,6 @@ private bool Create(IXLWorkbook workbook, Table table, ExcelPushConfig config)
return false;
}
}

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



}
5 changes: 3 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,7 @@
<Compile Include="CRUD\Update\UpdateWorkbookProperties.cs" />
<Compile Include="ExcelAdapter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Validation\WorksheetName.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -132,4 +133,4 @@

</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>
100 changes: 100 additions & 0 deletions Excel_Adapter/Validation/WorksheetName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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 = !IsValidName(name, workbook) ? ModifyWorksheetName() : name;

if (workSheetName != name)
{
BH.Engine.Base.Compute.RecordError("Name of worksheet has been adjusted to a name that which is compatible with Excel naming limitations.");
travispotterBH marked this conversation as resolved.
Show resolved Hide resolved
}

return workSheetName;
}

/***************************************************/

private static bool IsValidName(string workSheetName, IXLWorkbook workbook)
{
Dictionary<string, bool> nameChecks = new Dictionary<string, bool>();

bool isUnique = IsUnique(workSheetName, workbook);
bool isWithinCharacterLimit = IsWithinCharacterLimit(workSheetName);
bool isNotBlank = !string.IsNullOrWhiteSpace(workSheetName);
bool isNotReservedWord = IsNotReservedWord(workSheetName);
bool isValidCharacters = IsValidCharacters(workSheetName);
bool isNotBeginWithInvalidCharacter = IsNotBeginOrEndWithInvalidCharacter(workSheetName);

return isUnique && isWithinCharacterLimit && isNotBlank && isNotReservedWord && isValidCharacters && isNotBeginWithInvalidCharacter;
}

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

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

return !r.IsMatch(workSheetName);
}

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

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

private static bool IsUnique(string workSheetName, IXLWorkbook workbook)
{
return !workbook.Worksheets.Contains(workSheetName);
}

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

private static string ModifyWorksheetName()
{
string workSheetName = "BHoM_Export_" + DateTime.Now.ToString("ddMMyy_HHmmss");

return workSheetName;
}
}
}