From f8af30d4154692bac9b4206f59756d9be9de187e Mon Sep 17 00:00:00 2001 From: travispotterBH Date: Thu, 23 Jun 2022 18:57:23 -0400 Subject: [PATCH 1/8] Update Create.cs --- Excel_Adapter/CRUD/Create/Create.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Excel_Adapter/CRUD/Create/Create.cs b/Excel_Adapter/CRUD/Create/Create.cs index d125425..3667ec1 100644 --- a/Excel_Adapter/CRUD/Create/Create.cs +++ b/Excel_Adapter/CRUD/Create/Create.cs @@ -22,6 +22,7 @@ using BH.Engine.Excel; using BH.oM.Adapters.Excel; +using BH.oM.Base.Debugging; using BH.oM.Data.Collections; using ClosedXML.Excel; using System; @@ -42,6 +43,13 @@ private bool Create(IXLWorkbook workbook, Table table, ExcelPushConfig config) return false; } + if (table.Name.Length > 31) + { + BH.Engine.Base.Compute.RecordError("Creation of a table failed: input table name length is greater than Excel's limit of 31 characters."); + return false; + } + + try { IXLWorksheet worksheet = workbook.AddWorksheet(table.Name); From 45f6ce1f29ad26028bb03854f6488a93ff6e7cfb Mon Sep 17 00:00:00 2001 From: travispotterBH Date: Thu, 23 Jun 2022 19:05:50 -0400 Subject: [PATCH 2/8] Update Create.cs --- Excel_Adapter/CRUD/Create/Create.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Excel_Adapter/CRUD/Create/Create.cs b/Excel_Adapter/CRUD/Create/Create.cs index 3667ec1..2096341 100644 --- a/Excel_Adapter/CRUD/Create/Create.cs +++ b/Excel_Adapter/CRUD/Create/Create.cs @@ -22,7 +22,6 @@ using BH.Engine.Excel; using BH.oM.Adapters.Excel; -using BH.oM.Base.Debugging; using BH.oM.Data.Collections; using ClosedXML.Excel; using System; @@ -49,7 +48,6 @@ private bool Create(IXLWorkbook workbook, Table table, ExcelPushConfig config) return false; } - try { IXLWorksheet worksheet = workbook.AddWorksheet(table.Name); From a47c1f13e59b4312327e4969479edc85ebec4e44 Mon Sep 17 00:00:00 2001 From: travispotterBH Date: Tue, 28 Jun 2022 10:04:29 -0400 Subject: [PATCH 3/8] WorkSheet Name Validation updated excel toolkit ability to validate a worksheet name based on rules: https://www.keynotesupport.com/excel-basics/worksheet-names-characters-allowed-prohibited.shtml --- Excel_Adapter/CRUD/Create/Create.cs | 15 +-- Excel_Adapter/Excel_Adapter.csproj | 5 +- Excel_Adapter/Validation/WorksheetName.cs | 131 ++++++++++++++++++++++ 3 files changed, 138 insertions(+), 13 deletions(-) create mode 100644 Excel_Adapter/Validation/WorksheetName.cs diff --git a/Excel_Adapter/CRUD/Create/Create.cs b/Excel_Adapter/CRUD/Create/Create.cs index 2096341..7e4b29b 100644 --- a/Excel_Adapter/CRUD/Create/Create.cs +++ b/Excel_Adapter/CRUD/Create/Create.cs @@ -36,21 +36,18 @@ public partial class ExcelAdapter private bool Create(IXLWorkbook workbook, Table table, ExcelPushConfig config) { + 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; } - if (table.Name.Length > 31) - { - BH.Engine.Base.Compute.RecordError("Creation of a table failed: input table name length is greater than Excel's limit of 31 characters."); - 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)) @@ -65,10 +62,6 @@ private bool Create(IXLWorkbook workbook, Table table, ExcelPushConfig config) return false; } } - /***************************************************/ } -} - - - +} \ No newline at end of file diff --git a/Excel_Adapter/Excel_Adapter.csproj b/Excel_Adapter/Excel_Adapter.csproj index bfd8941..544b97d 100644 --- a/Excel_Adapter/Excel_Adapter.csproj +++ b/Excel_Adapter/Excel_Adapter.csproj @@ -1,4 +1,4 @@ - + @@ -107,6 +107,7 @@ + @@ -132,4 +133,4 @@ - + \ No newline at end of file diff --git a/Excel_Adapter/Validation/WorksheetName.cs b/Excel_Adapter/Validation/WorksheetName.cs new file mode 100644 index 0000000..643573d --- /dev/null +++ b/Excel_Adapter/Validation/WorksheetName.cs @@ -0,0 +1,131 @@ +/* + * 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 . + */ + +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) + { + Dictionary nameChecks = new Dictionary(); + + string workSheetName = !IsValidName(name, workbook, out nameChecks) ? ModifyWorksheetName(name, nameChecks, workbook) : 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."); + } + + return workSheetName; + } + + private static bool IsValidName(string workSheetName, IXLWorkbook workbook, out Dictionary nameChecks) + { + nameChecks = new Dictionary(); + + 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); + + if (!isUnique) + { + nameChecks.Add("isUnique", isUnique); + } + + if (!isWithinCharacterLimit) + { + nameChecks.Add("isWithinCharacterLimit", isWithinCharacterLimit); + } + + if (!isNotBlank) + { + nameChecks.Add("isNotBlank", isNotBlank); + } + + if (!isNotReservedWord) + { + nameChecks.Add("isNotReservedWord", isNotReservedWord); + } + + if (!isValidCharacters) + { + nameChecks.Add("isValidCharacters", isValidCharacters); + } + + if (!isNotBeginWithInvalidCharacter) + { + nameChecks.Add("isNotBeginWithInvalidCharacter", isNotBeginWithInvalidCharacter); + } + + return nameChecks.Count == 0; + } + + 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 reservedWords = new List { "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; + } + } +} \ No newline at end of file From 7507fdf3858cd9d258b54d90d39c01a5ae9810d0 Mon Sep 17 00:00:00 2001 From: travispotterBH Date: Tue, 28 Jun 2022 10:08:33 -0400 Subject: [PATCH 4/8] Cleaning up --- Excel_Adapter/Validation/WorksheetName.cs | 43 ++++------------------- 1 file changed, 6 insertions(+), 37 deletions(-) diff --git a/Excel_Adapter/Validation/WorksheetName.cs b/Excel_Adapter/Validation/WorksheetName.cs index 643573d..0874d9e 100644 --- a/Excel_Adapter/Validation/WorksheetName.cs +++ b/Excel_Adapter/Validation/WorksheetName.cs @@ -33,12 +33,9 @@ public partial class Validation /**** Public Methods ****/ /***************************************************/ - public static string WorksheetName(string name, IXLWorkbook workbook) { - Dictionary nameChecks = new Dictionary(); - - string workSheetName = !IsValidName(name, workbook, out nameChecks) ? ModifyWorksheetName(name, nameChecks, workbook) : name; + string workSheetName = !IsValidName(name, workbook) ? ModifyWorksheetName() : name; if (workSheetName != name) { @@ -48,9 +45,11 @@ public static string WorksheetName(string name, IXLWorkbook workbook) return workSheetName; } - private static bool IsValidName(string workSheetName, IXLWorkbook workbook, out Dictionary nameChecks) + /***************************************************/ + + private static bool IsValidName(string workSheetName, IXLWorkbook workbook) { - nameChecks = new Dictionary(); + Dictionary nameChecks = new Dictionary(); bool isUnique = IsUnique(workSheetName, workbook); bool isWithinCharacterLimit = IsWithinCharacterLimit(workSheetName); @@ -59,37 +58,7 @@ private static bool IsValidName(string workSheetName, IXLWorkbook workbook, out bool isValidCharacters = IsValidCharacters(workSheetName); bool isNotBeginWithInvalidCharacter = IsNotBeginOrEndWithInvalidCharacter(workSheetName); - if (!isUnique) - { - nameChecks.Add("isUnique", isUnique); - } - - if (!isWithinCharacterLimit) - { - nameChecks.Add("isWithinCharacterLimit", isWithinCharacterLimit); - } - - if (!isNotBlank) - { - nameChecks.Add("isNotBlank", isNotBlank); - } - - if (!isNotReservedWord) - { - nameChecks.Add("isNotReservedWord", isNotReservedWord); - } - - if (!isValidCharacters) - { - nameChecks.Add("isValidCharacters", isValidCharacters); - } - - if (!isNotBeginWithInvalidCharacter) - { - nameChecks.Add("isNotBeginWithInvalidCharacter", isNotBeginWithInvalidCharacter); - } - - return nameChecks.Count == 0; + return isUnique && isWithinCharacterLimit && isNotBlank && isNotReservedWord && isValidCharacters && isNotBeginWithInvalidCharacter; } private static bool IsNotBeginOrEndWithInvalidCharacter(string workSheetName) From 95a43e722bbddda12073c101d814188042390993 Mon Sep 17 00:00:00 2001 From: travispotterBH Date: Tue, 28 Jun 2022 10:43:16 -0400 Subject: [PATCH 5/8] Update WorksheetName.cs --- Excel_Adapter/Validation/WorksheetName.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Excel_Adapter/Validation/WorksheetName.cs b/Excel_Adapter/Validation/WorksheetName.cs index 0874d9e..abf2ba8 100644 --- a/Excel_Adapter/Validation/WorksheetName.cs +++ b/Excel_Adapter/Validation/WorksheetName.cs @@ -97,4 +97,4 @@ private static string ModifyWorksheetName() return workSheetName; } } -} \ No newline at end of file +} From 1cd510b30b334d583c6b3f1c7ea9d5519c89f049 Mon Sep 17 00:00:00 2001 From: travispotterBH Date: Tue, 28 Jun 2022 15:06:39 -0400 Subject: [PATCH 6/8] increased robustness of name validation --- Excel_Adapter/CRUD/Create/Create.cs | 1 - Excel_Adapter/Validation/WorksheetName.cs | 149 +++++++++++++++++++--- 2 files changed, 129 insertions(+), 21 deletions(-) diff --git a/Excel_Adapter/CRUD/Create/Create.cs b/Excel_Adapter/CRUD/Create/Create.cs index 7e4b29b..91a267c 100644 --- a/Excel_Adapter/CRUD/Create/Create.cs +++ b/Excel_Adapter/CRUD/Create/Create.cs @@ -36,7 +36,6 @@ public partial class ExcelAdapter private bool Create(IXLWorkbook workbook, Table table, ExcelPushConfig config) { - if (table?.Data == null) { BH.Engine.Base.Compute.RecordError("Creation of a table failed: input table is null or does not contain a table."); diff --git a/Excel_Adapter/Validation/WorksheetName.cs b/Excel_Adapter/Validation/WorksheetName.cs index abf2ba8..af8c28c 100644 --- a/Excel_Adapter/Validation/WorksheetName.cs +++ b/Excel_Adapter/Validation/WorksheetName.cs @@ -21,6 +21,7 @@ */ using ClosedXML.Excel; +using DocumentFormat.OpenXml.Spreadsheet; using System; using System.Collections.Generic; using System.Text.RegularExpressions; @@ -35,66 +36,174 @@ public partial class Validation public static string WorksheetName(string name, IXLWorkbook workbook) { - string workSheetName = !IsValidName(name, workbook) ? ModifyWorksheetName() : name; + string worksheetName = name; - if (workSheetName != name) + List validationIssues = new List(); + + bool isValidName = IsValidName(worksheetName, workbook, out validationIssues); + while (!isValidName) + { + worksheetName = ModifyWorksheetName(worksheetName, validationIssues, workbook); + isValidName = IsValidName(worksheetName, workbook, out validationIssues); + } + + + 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."); } - return workSheetName; + return worksheetName; } + /***************************************************/ - private static bool IsValidName(string workSheetName, IXLWorkbook workbook) + private static string ModifyWorksheetName(string worksheetName, List validationIssues, IXLWorkbook workbook) + { + switch (validationIssues[0]) + { + case WorksheetValidation.isReservedWord: + case WorksheetValidation.isBlank: + return SetGeneralName(); + + case WorksheetValidation.isNotUnique: + return SetUniqueName(workbook, worksheetName); + + case WorksheetValidation.isBeginOrEndWithInvalidCharacter: + return RemoveInvalidBeginningOrEnding(worksheetName); + + case WorksheetValidation.isNotValidCharacters: + return RemoveInvalidCharacters(worksheetName); + + case WorksheetValidation.isNotWithinCharacterLimit: + return TrimName(worksheetName); + + default: + break; + } + + return worksheetName; + } + + private static string TrimName(string worksheetName) + { + if (worksheetName.ToString().Contains(" ")) + { + worksheetName = worksheetName.Replace(" ", ""); + return worksheetName; + } + + return worksheetName.Substring(0, 31); + + } + + private static string RemoveInvalidCharacters(string worksheetName) + { + List InvalidChars = new List() { '[', '/', '?', ']', '*', '\\', ':' }; + + InvalidChars.ForEach(c => worksheetName = worksheetName.Replace(c.ToString(), String.Empty)); + return worksheetName; + } + + private static string RemoveInvalidBeginningOrEnding(string worksheetName) { - Dictionary nameChecks = new Dictionary(); + if (worksheetName.Substring(0, 1) == "\'") + { + worksheetName = worksheetName.Replace("\'", ""); + } - 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); + if (worksheetName.Substring(worksheetName.Length - 1) == "\'") + { + worksheetName = worksheetName.Replace("\'", ""); + } + + return worksheetName; + } - return isUnique && isWithinCharacterLimit && isNotBlank && isNotReservedWord && isValidCharacters && isNotBeginWithInvalidCharacter; + private static string SetUniqueName(IXLWorkbook workbook, string worksheetName) + { + return DateTime.Now.ToString("ddMMyy HHmmssf") + "_" + worksheetName; } - private static bool IsNotBeginOrEndWithInvalidCharacter(string workSheetName) + private static bool IsValidName(string workSheetName, IXLWorkbook workbook, out List validationIssues) + { + validationIssues = new List(); + + if (!CheckIsUnique(workSheetName, workbook)) + validationIssues.Add(WorksheetValidation.isNotUnique); + + if (!CheckIsNullOrWhitespace(workSheetName)) + validationIssues.Add(WorksheetValidation.isBlank); + + if (!CheckIsNotReservedWord(workSheetName)) + validationIssues.Add(WorksheetValidation.isReservedWord); + + if (!CheckIsValidCharacters(workSheetName)) + validationIssues.Add(WorksheetValidation.isNotValidCharacters); + + if (!CheckIsNotBeginOrEndWithInvalidCharacter(workSheetName)) + validationIssues.Add(WorksheetValidation.isBeginOrEndWithInvalidCharacter); + + if (!CheckIsWithinCharacterLimit(workSheetName)) + validationIssues.Add(WorksheetValidation.isNotWithinCharacterLimit); + + if (validationIssues.Count > 0) + { + return false; + } + + return true; + } + + private static bool CheckIsNullOrWhitespace(string workSheetName) + { + return !string.IsNullOrWhiteSpace(workSheetName); + } + + private static bool CheckIsNotBeginOrEndWithInvalidCharacter(string workSheetName) { return !workSheetName.StartsWith("\'") && !workSheetName.EndsWith("\'"); } - private static bool IsValidCharacters(string workSheetName) + private static bool CheckIsValidCharacters(string workSheetName) { Regex r = new Regex(@"[\[/\?\]\*\\\:]"); return !r.IsMatch(workSheetName); } - private static bool IsNotReservedWord(string workSheetName) + private static bool CheckIsNotReservedWord(string workSheetName) { List reservedWords = new List { "history" }; return !reservedWords.Contains(workSheetName.ToLower()); } - private static bool IsUnique(string workSheetName, IXLWorkbook workbook) + private static bool CheckIsUnique(string workSheetName, IXLWorkbook workbook) { return !workbook.Worksheets.Contains(workSheetName); } - private static bool IsWithinCharacterLimit(string workSheetName) + private static bool CheckIsWithinCharacterLimit(string workSheetName) { return workSheetName.Length <= 31; } - private static string ModifyWorksheetName() + private static string SetGeneralName() { - string workSheetName = "BHoM_Export_" + DateTime.Now.ToString("ddMMyy_HHmmss"); + return "BHoM_Export_" + DateTime.Now.ToString("ddMMyy_HHmmss"); + } - return workSheetName; + private enum WorksheetValidation + { + isNotUnique, + isNotWithinCharacterLimit, + isBlank, + isReservedWord, + isNotValidCharacters, + isBeginOrEndWithInvalidCharacter } } + } From 8ef28092835b04aae1236c625cbedbe6cc950b70 Mon Sep 17 00:00:00 2001 From: travispotterBH Date: Wed, 29 Jun 2022 09:17:43 -0400 Subject: [PATCH 7/8] updates per PR changes requested --- Excel_Adapter/Excel_Adapter.csproj | 1 + .../Validation/Enums/WorksheetValidation.cs | 35 ++++ Excel_Adapter/Validation/WorksheetName.cs | 190 ++++++++---------- 3 files changed, 125 insertions(+), 101 deletions(-) create mode 100644 Excel_Adapter/Validation/Enums/WorksheetValidation.cs diff --git a/Excel_Adapter/Excel_Adapter.csproj b/Excel_Adapter/Excel_Adapter.csproj index 544b97d..66ec5c7 100644 --- a/Excel_Adapter/Excel_Adapter.csproj +++ b/Excel_Adapter/Excel_Adapter.csproj @@ -108,6 +108,7 @@ + diff --git a/Excel_Adapter/Validation/Enums/WorksheetValidation.cs b/Excel_Adapter/Validation/Enums/WorksheetValidation.cs new file mode 100644 index 0000000..b232fee --- /dev/null +++ b/Excel_Adapter/Validation/Enums/WorksheetValidation.cs @@ -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 . + */ + +namespace BH.Adapter.Excel +{ + public enum WorksheetValidation + { + NotUnique, + NotWithinCharacterLimit, + Blank, + ReservedWord, + NotValidCharacters, + BeginOrEndWithInvalidCharacter, + Valid + } +} diff --git a/Excel_Adapter/Validation/WorksheetName.cs b/Excel_Adapter/Validation/WorksheetName.cs index af8c28c..2b21f1e 100644 --- a/Excel_Adapter/Validation/WorksheetName.cs +++ b/Excel_Adapter/Validation/WorksheetName.cs @@ -21,7 +21,6 @@ */ using ClosedXML.Excel; -using DocumentFormat.OpenXml.Spreadsheet; using System; using System.Collections.Generic; using System.Text.RegularExpressions; @@ -38,172 +37,161 @@ public static string WorksheetName(string name, IXLWorkbook workbook) { string worksheetName = name; - List validationIssues = new List(); - - bool isValidName = IsValidName(worksheetName, workbook, out validationIssues); - while (!isValidName) + WorksheetValidation issue = IsValidName(worksheetName, workbook); + while (issue != WorksheetValidation.Valid) { - worksheetName = ModifyWorksheetName(worksheetName, validationIssues, workbook); - isValidName = IsValidName(worksheetName, workbook, out validationIssues); + worksheetName = ModifyWorksheetName(worksheetName, issue); + issue = IsValidName(worksheetName, workbook); } 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."); + 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 string ModifyWorksheetName(string worksheetName, List validationIssues, IXLWorkbook workbook) + private static WorksheetValidation IsValidName(string workSheetName, IXLWorkbook workbook) { - switch (validationIssues[0]) - { - case WorksheetValidation.isReservedWord: - case WorksheetValidation.isBlank: - return SetGeneralName(); + WorksheetValidation issue = WorksheetValidation.Valid; - case WorksheetValidation.isNotUnique: - return SetUniqueName(workbook, worksheetName); + if (!CheckIsUnique(workSheetName, workbook)) + return WorksheetValidation.NotUnique; - case WorksheetValidation.isBeginOrEndWithInvalidCharacter: - return RemoveInvalidBeginningOrEnding(worksheetName); + if (string.IsNullOrWhiteSpace(workSheetName)) + return WorksheetValidation.Blank; - case WorksheetValidation.isNotValidCharacters: - return RemoveInvalidCharacters(worksheetName); + if (!CheckIsNotReservedWord(workSheetName)) + return WorksheetValidation.ReservedWord; - case WorksheetValidation.isNotWithinCharacterLimit: - return TrimName(worksheetName); + if (!CheckIsValidCharacters(workSheetName)) + return WorksheetValidation.NotValidCharacters; - default: - break; - } + if (!CheckIsNotBeginOrEndWithInvalidCharacter(workSheetName)) + return WorksheetValidation.BeginOrEndWithInvalidCharacter; - return worksheetName; - } + if (!CheckIsWithinCharacterLimit(workSheetName)) + return WorksheetValidation.NotWithinCharacterLimit; - private static string TrimName(string worksheetName) - { - if (worksheetName.ToString().Contains(" ")) - { - worksheetName = worksheetName.Replace(" ", ""); - return worksheetName; - } + return issue; + } - return worksheetName.Substring(0, 31); + /************ Checks *********************/ + private static bool CheckIsUnique(string workSheetName, IXLWorkbook workbook) + { + return !workbook.Worksheets.Contains(workSheetName); } - private static string RemoveInvalidCharacters(string worksheetName) + private static bool CheckIsNotReservedWord(string workSheetName) { - List InvalidChars = new List() { '[', '/', '?', ']', '*', '\\', ':' }; + List reservedWords = new List { "history" }; - InvalidChars.ForEach(c => worksheetName = worksheetName.Replace(c.ToString(), String.Empty)); - return worksheetName; + return !reservedWords.Contains(workSheetName.ToLower()); } - private static string RemoveInvalidBeginningOrEnding(string worksheetName) + private static bool CheckIsValidCharacters(string workSheetName) { - if (worksheetName.Substring(0, 1) == "\'") - { - worksheetName = worksheetName.Replace("\'", ""); - } - - if (worksheetName.Substring(worksheetName.Length - 1) == "\'") - { - worksheetName = worksheetName.Replace("\'", ""); - } + Regex r = new Regex(@"[\[/\?\]\*\\\:]"); - return worksheetName; + return !r.IsMatch(workSheetName); } - private static string SetUniqueName(IXLWorkbook workbook, string worksheetName) + private static bool CheckIsNotBeginOrEndWithInvalidCharacter(string workSheetName) { - return DateTime.Now.ToString("ddMMyy HHmmssf") + "_" + worksheetName; + return !workSheetName.StartsWith("\'") && !workSheetName.EndsWith("\'"); } - private static bool IsValidName(string workSheetName, IXLWorkbook workbook, out List validationIssues) + private static bool CheckIsWithinCharacterLimit(string workSheetName) { - validationIssues = new List(); + return workSheetName.Length <= 31; + } - if (!CheckIsUnique(workSheetName, workbook)) - validationIssues.Add(WorksheetValidation.isNotUnique); + /************ Modifications *********************/ + private static string ModifyWorksheetName(string worksheetName, WorksheetValidation issue) + { + switch (issue) + { + case WorksheetValidation.ReservedWord: + return SetUniqueNameWithReservedName(worksheetName); - if (!CheckIsNullOrWhitespace(workSheetName)) - validationIssues.Add(WorksheetValidation.isBlank); + case WorksheetValidation.Blank: + return SetGeneralName(); - if (!CheckIsNotReservedWord(workSheetName)) - validationIssues.Add(WorksheetValidation.isReservedWord); + case WorksheetValidation.NotUnique: + return SetUniqueName(worksheetName); - if (!CheckIsValidCharacters(workSheetName)) - validationIssues.Add(WorksheetValidation.isNotValidCharacters); + case WorksheetValidation.BeginOrEndWithInvalidCharacter: + return RemoveInvalidBeginningOrEnding(worksheetName); - if (!CheckIsNotBeginOrEndWithInvalidCharacter(workSheetName)) - validationIssues.Add(WorksheetValidation.isBeginOrEndWithInvalidCharacter); + case WorksheetValidation.NotValidCharacters: + return RemoveInvalidCharacters(worksheetName); - if (!CheckIsWithinCharacterLimit(workSheetName)) - validationIssues.Add(WorksheetValidation.isNotWithinCharacterLimit); + case WorksheetValidation.NotWithinCharacterLimit: + return TrimName(worksheetName); - if (validationIssues.Count > 0) - { - return false; + default: + case WorksheetValidation.Valid: + break; } - return true; + return worksheetName; } - private static bool CheckIsNullOrWhitespace(string workSheetName) + private static string SetUniqueNameWithReservedName(string worksheetName) { - return !string.IsNullOrWhiteSpace(workSheetName); + return "BHoM_" + worksheetName; } - private static bool CheckIsNotBeginOrEndWithInvalidCharacter(string workSheetName) + private static string SetGeneralName() { - return !workSheetName.StartsWith("\'") && !workSheetName.EndsWith("\'"); + return "BHoM_Export_" + DateTime.Now.ToString("ddMMyy_HHmmss"); } - private static bool CheckIsValidCharacters(string workSheetName) + private static string SetUniqueName(string worksheetName) { - Regex r = new Regex(@"[\[/\?\]\*\\\:]"); - - return !r.IsMatch(workSheetName); + return DateTime.Now.ToString("ddMMyy HHmmssf") + "_" + worksheetName; } - private static bool CheckIsNotReservedWord(string workSheetName) + private static string RemoveInvalidBeginningOrEnding(string worksheetName) { - List reservedWords = new List { "history" }; + if (worksheetName.StartsWith("\'")) + { + worksheetName = worksheetName.Substring(1, worksheetName.Length - 1); + } - return !reservedWords.Contains(workSheetName.ToLower()); - } + if (worksheetName.EndsWith("\'")) + { + worksheetName = worksheetName.Substring(0, worksheetName.Length - 2); + } - private static bool CheckIsUnique(string workSheetName, IXLWorkbook workbook) - { - return !workbook.Worksheets.Contains(workSheetName); + return worksheetName; } - private static bool CheckIsWithinCharacterLimit(string workSheetName) + private static string RemoveInvalidCharacters(string worksheetName) { - return workSheetName.Length <= 31; - } + List invalidChars = new List() { '[', '/', '?', ']', '*', '\\', ':' }; - private static string SetGeneralName() - { - return "BHoM_Export_" + DateTime.Now.ToString("ddMMyy_HHmmss"); + invalidChars.ForEach(c => worksheetName = worksheetName.Replace(c.ToString(), String.Empty)); + return worksheetName; } - private enum WorksheetValidation + private static string TrimName(string worksheetName) { - isNotUnique, - isNotWithinCharacterLimit, - isBlank, - isReservedWord, - isNotValidCharacters, - isBeginOrEndWithInvalidCharacter + if (worksheetName.Contains(" ") || worksheetName.Contains("-") || worksheetName.Contains("_") || worksheetName.Contains(",")) + { + List toBeRemovedChars = new List() { ' ', '-', '_', ',' }; + toBeRemovedChars.ForEach(c => worksheetName = worksheetName.Replace(c.ToString(), String.Empty)); + return worksheetName; + } + + return worksheetName.Substring(0, 31); } } - } From 4a3ebb8e6e0489b840deab824e89e0a4eb18c335 Mon Sep 17 00:00:00 2001 From: travispotterBH Date: Wed, 29 Jun 2022 10:16:56 -0400 Subject: [PATCH 8/8] updates per pr comments --- .../Validation/Enums/WorksheetValidation.cs | 20 +++++++++---------- Excel_Adapter/Validation/WorksheetName.cs | 7 ++----- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Excel_Adapter/Validation/Enums/WorksheetValidation.cs b/Excel_Adapter/Validation/Enums/WorksheetValidation.cs index b232fee..bb5d922 100644 --- a/Excel_Adapter/Validation/Enums/WorksheetValidation.cs +++ b/Excel_Adapter/Validation/Enums/WorksheetValidation.cs @@ -22,14 +22,14 @@ namespace BH.Adapter.Excel { - public enum WorksheetValidation - { - NotUnique, - NotWithinCharacterLimit, - Blank, - ReservedWord, - NotValidCharacters, - BeginOrEndWithInvalidCharacter, - Valid - } + public enum WorksheetValidation + { + BeginOrEndWithInvalidCharacter, + Blank, + NotUnique, + NotValidCharacters, + NotWithinCharacterLimit, + ReservedWord, + Valid + } } diff --git a/Excel_Adapter/Validation/WorksheetName.cs b/Excel_Adapter/Validation/WorksheetName.cs index 2b21f1e..4a7a2ca 100644 --- a/Excel_Adapter/Validation/WorksheetName.cs +++ b/Excel_Adapter/Validation/WorksheetName.cs @@ -44,7 +44,6 @@ public static string WorksheetName(string name, IXLWorkbook workbook) 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}."); @@ -59,8 +58,6 @@ public static string WorksheetName(string name, IXLWorkbook workbook) private static WorksheetValidation IsValidName(string workSheetName, IXLWorkbook workbook) { - WorksheetValidation issue = WorksheetValidation.Valid; - if (!CheckIsUnique(workSheetName, workbook)) return WorksheetValidation.NotUnique; @@ -79,7 +76,7 @@ private static WorksheetValidation IsValidName(string workSheetName, IXLWorkbook if (!CheckIsWithinCharacterLimit(workSheetName)) return WorksheetValidation.NotWithinCharacterLimit; - return issue; + return WorksheetValidation.Valid; } /************ Checks *********************/ @@ -163,7 +160,7 @@ private static string RemoveInvalidBeginningOrEnding(string worksheetName) { if (worksheetName.StartsWith("\'")) { - worksheetName = worksheetName.Substring(1, worksheetName.Length - 1); + worksheetName = worksheetName.Substring(1); } if (worksheetName.EndsWith("\'"))