Skip to content

Commit

Permalink
Fixed issue #7. Modified to check header column when it imported.
Browse files Browse the repository at this point in the history
* Header column should not be null
* Header column should not be any c# keywords.
  • Loading branch information
kimsama committed Aug 10, 2016
1 parent ce900f4 commit 3fcb3af
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
8 changes: 7 additions & 1 deletion Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> titleList = titles.ToList();

if (machine.HasHeadColumn() && reimport == false)
Expand Down
49 changes: 41 additions & 8 deletions Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -200,24 +200,57 @@ public string[] GetSheetNames()
}

/// <summary>
/// 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.
/// </summary>
public string[] GetTitle(int start = 0)
public string[] GetTitle(int start, ref string error)
{
List<string> result = new List<string>();

IRow title = sheet.GetRow(start);
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;
}

/// <summary>
/// Check the given header column has valid name which should not be any c# keywords.
/// </summary>
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;
}

/// <summary>
/// Convert type of cell value to its predefined type in the sheet's ScriptMachine setting file.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions Assets/QuickSheet/Util/Util.cs
Original file line number Diff line number Diff line change
@@ -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",
};

}
}
12 changes: 12 additions & 0 deletions Assets/QuickSheet/Util/Util.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3fcb3af

Please sign in to comment.