From 3fcb3af4e87b54e3101577e75ecdf7a3f0bced6a Mon Sep 17 00:00:00 2001 From: kimsama Date: Wed, 10 Aug 2016 16:22:45 +0900 Subject: [PATCH] Fixed issue #7. Modified to check header column when it imported. * Header column should not be null * Header column should not be any c# keywords. --- .../ExcelPlugin/Editor/ExcelMachineEditor.cs | 8 ++- .../ExcelPlugin/Editor/ExcelQuery.cs | 49 ++++++++++++++++--- Assets/QuickSheet/Util/Util.cs | 21 ++++++++ Assets/QuickSheet/Util/Util.cs.meta | 12 +++++ 4 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 Assets/QuickSheet/Util/Util.cs create mode 100644 Assets/QuickSheet/Util/Util.cs.meta diff --git a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs index 05336da..518e0ae 100644 --- a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs +++ b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs @@ -207,8 +207,14 @@ protected override void Import(bool reimport = false) ); return; } + string error = string.Empty; + var titles = new ExcelQuery(path, sheet).GetTitle(0, ref error); + if (titles == null || !string.IsNullOrEmpty(error)) + { + EditorUtility.DisplayDialog("Error", error, "OK"); + return; + } - var titles = new ExcelQuery(path, sheet).GetTitle(); List titleList = titles.ToList(); if (machine.HasHeadColumn() && reimport == false) diff --git a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs index d7e0b31..c12a8b4 100644 --- a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs +++ b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs @@ -45,11 +45,11 @@ public ExcelQuery(string path, string sheetName = "") workbook = new HSSFWorkbook(fileStream); else if (extension == "xlsx") { -#if UNITY_MAC - throw new Exception("xlsx is not supported on OSX."); -#else + #if UNITY_EDITOR_OSX + throw new Exception("xlsx is not supported on OSX."); + #else workbook = new XSSFWorkbook(fileStream); -#endif + #endif } else { @@ -200,9 +200,9 @@ public string[] GetSheetNames() } /// - /// Retrieves all first columns(aka. header) which are needed to determine type of each cell. + /// Retrieves all first columns(aka. header column) which are needed to determine each type of a cell. /// - public string[] GetTitle(int start = 0) + public string[] GetTitle(int start, ref string error) { List result = new List(); @@ -210,14 +210,47 @@ public string[] GetTitle(int start = 0) if (title != null) { for (int i = 0; i < title.LastCellNum; i++) - result.Add(title.GetCell(i).StringCellValue); + { + string value = title.GetCell(i).StringCellValue; + if (string.IsNullOrEmpty(value)) + { + error = string.Format(@"Empty column is found at {0}.", i); + return null; + } + else + { + if (IsValidHeader(value)) + result.Add(value); + else + { + error = string.Format(@"Error at column {0}, {1} is invalid name as header column.", i, value); + return null; + } + } + } return result.ToArray(); } - + + error = string.Format(@"Empty row at {0}", start); return null; } + /// + /// Check the given header column has valid name which should not be any c# keywords. + /// + private bool IsValidHeader(string s) + { + // no case sensitive! + string comp = s.ToLower(); + + string found = Array.Find(Util.Keywords, x => x == comp); + if (string.IsNullOrEmpty(found)) + return true; + + return false; + } + /// /// Convert type of cell value to its predefined type in the sheet's ScriptMachine setting file. /// diff --git a/Assets/QuickSheet/Util/Util.cs b/Assets/QuickSheet/Util/Util.cs new file mode 100644 index 0000000..f6a93b4 --- /dev/null +++ b/Assets/QuickSheet/Util/Util.cs @@ -0,0 +1,21 @@ +using UnityEngine; +using System.Collections; + +namespace UnityQuickSheet +{ + public class Util + { + //all c# keywords. + public static string[] Keywords = new string[] { + "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", + "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", + "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", + "if", "implicit", "in", "in", "int", "interface", "internal", "is", "lock", "long", + "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", + "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", + "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", + "ushort", "using", "virtual", "void", "volatile", "while", + }; + + } +} \ No newline at end of file diff --git a/Assets/QuickSheet/Util/Util.cs.meta b/Assets/QuickSheet/Util/Util.cs.meta new file mode 100644 index 0000000..b0cac3d --- /dev/null +++ b/Assets/QuickSheet/Util/Util.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 1bfc32c6f139a62409180c6920a40b67 +timeCreated: 1470812588 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: