Skip to content

Commit

Permalink
* [Google] Changed to check the validation of the given column headers.
Browse files Browse the repository at this point in the history
* [Excel] Fixed a bug which is resetting existing column header's array type when it is just doing update.
  • Loading branch information
kimsama committed Aug 11, 2016
1 parent 3fcb3af commit 2ebab18
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
18 changes: 17 additions & 1 deletion Assets/QuickSheet/Editor/BaseMachineEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
///////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace UnityQuickSheet
{
Expand All @@ -28,6 +29,21 @@ protected virtual void Import(bool reimport = false)
Debug.LogWarning("!!! It should be implemented in the derived class !!!");
}

/// <summary>
/// Check the given header column has valid name which should not be any c# keywords.
/// </summary>
protected 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>
/// Generate script files with the given templates.
/// Total four files are generated, two for runtime and others for editor.
Expand Down
25 changes: 20 additions & 5 deletions Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,31 +207,46 @@ protected override void Import(bool reimport = false)
);
return;
}

int startRowIndex = 0;
string error = string.Empty;
var titles = new ExcelQuery(path, sheet).GetTitle(0, ref error);
var titles = new ExcelQuery(path, sheet).GetTitle(startRowIndex, ref error);
if (titles == null || !string.IsNullOrEmpty(error))
{
EditorUtility.DisplayDialog("Error", error, "OK");
return;
}
else
{
// check the column header is valid
foreach(string column in titles)
{
if (!IsValidHeader(column))
{
error = string.Format(@"Invalid column header name {0}. Any c# keyword should not be used for column header. Note it is not case sensitive.", column);
EditorUtility.DisplayDialog("Error", error, "OK");
return;
}
}
}

List<string> titleList = titles.ToList();

if (machine.HasHeadColumn() && reimport == false)
{
var headerDic = machine.HeaderColumnList.ToDictionary(header => header.name);

// collect non changed header columns
// collect non-changed column headers
var exist = from t in titleList
where headerDic.ContainsKey(t) == true
select new HeaderColumn { name = t, type = headerDic[t].type, OrderNO = headerDic[t].OrderNO };
select new HeaderColumn { name = t, type = headerDic[t].type, isArray = headerDic[t].isArray, OrderNO = headerDic[t].OrderNO };

// collect newly added or changed header columns
// collect newly added or changed column headers
var changed = from t in titleList
where headerDic.ContainsKey(t) == false
select new HeaderColumn { name = t, type = CellType.Undefined, OrderNO = titleList.IndexOf(t) };

// merge two
// merge two list via LINQ
var merged = exist.Union(changed).OrderBy(x => x.OrderNO);

machine.HeaderColumnList.Clear();
Expand Down
24 changes: 2 additions & 22 deletions Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,8 @@ public string[] GetTitle(int start, ref string error)
}
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;
}
// column header is not an empty string, we check its validation later.
result.Add(value);
}
}

Expand All @@ -236,21 +231,6 @@ public string[] GetTitle(int start, ref string error)
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
8 changes: 8 additions & 0 deletions Assets/QuickSheet/GDataPlugin/Editor/GoogleMachineEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ protected override void Import(bool reimport = false)
if (int.Parse(m.Value) > 1)
return;

// check the column header is valid
if (!IsValidHeader(cell.Value))
{
string error = string.Format(@"Invalid column header name {0}. Any c# keyword should not be used for column header. Note it is not case sensitive.", cell.Value);
EditorUtility.DisplayDialog("Error", error, "OK");
return;
}

HeaderColumn column = new HeaderColumn();
column.name = cell.Value;
if (headerDic != null && headerDic.ContainsKey(cell.Value))
Expand Down

0 comments on commit 2ebab18

Please sign in to comment.