From 9a0a1741ebf55a7593d60af175f0789ae23eeaad Mon Sep 17 00:00:00 2001 From: kimsama Date: Mon, 22 Aug 2016 12:55:12 +0900 Subject: [PATCH] Fixed #20 issue. Now, it does not need to ',' at the end of cell for array type (if there is anyone, it is removed when it is imported). Same as both of excel and google spreadsheet. --- Assets/QuickSheet/Editor/Util/Util.cs | 13 ++++- .../ExcelPlugin/Editor/ExcelQuery.cs | 47 +++++++++---------- .../Editor/GDataDB/GDataDB/Impl/Serializer.cs | 13 ++++- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/Assets/QuickSheet/Editor/Util/Util.cs b/Assets/QuickSheet/Editor/Util/Util.cs index cb17dd8..cfac57d 100644 --- a/Assets/QuickSheet/Editor/Util/Util.cs +++ b/Assets/QuickSheet/Editor/Util/Util.cs @@ -1,8 +1,17 @@ -using System.Collections; +/////////////////////////////////////////////////////////////////////////////// +/// +/// Util.cs +/// +/// (c)2016 Kim, Hyoun Woo +/// +/////////////////////////////////////////////////////////////////////////////// +using System; +using System.Collections; +using System.Linq; namespace UnityQuickSheet { - public class Util + public static class Util { //all c# keywords. public static string[] Keywords = new string[] { diff --git a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs index 014cf02..4519ff8 100644 --- a/Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs +++ b/Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs @@ -132,36 +132,35 @@ public List Deserialize(int start = 1) if (property.PropertyType.IsArray) { - // collect string values. - CsvParser parser = new CsvParser(value as string); - List tokenizedValues = new List(); - foreach (string s in parser) - tokenizedValues.Add(s); - - // convert string to corresponded type of the array element. - object[] convertedValues = null; - if (property.PropertyType.GetElementType() == typeof(string)) - convertedValues = tokenizedValues.ToArray(); - else - convertedValues = tokenizedValues.ToArray().Select(s => Convert.ChangeType(s, property.PropertyType.GetElementType())).ToArray(); - - // set converted string value to the array. - Array array = (Array)property.GetValue(item, null); - if (array != null) - { - // initialize the array of the data class - array = Array.CreateInstance(property.PropertyType.GetElementType(), convertedValues.Length); - for (int j = 0; j < convertedValues.Length; j++) - array.SetValue(convertedValues[j], j); + //NOTE: enum array type is not supported. (Does it really needed?) + + const char DELIMETER = ','; + string str = value as string; + + // remove whitespace between each of element + str = new string(str.ToCharArray() + .Where(ch => !Char.IsWhiteSpace(ch)) + .ToArray()); + + // remove ',', if it is found at the end. + char[] charToTrim = { ',', ' ' }; + str = str.TrimEnd(charToTrim); - // should do deep copy - property.SetValue(item, array, null); + // split by ',' + object[] temp = str.Split(DELIMETER); + + Array array = (Array)Activator.CreateInstance(property.PropertyType, temp.Length); + + for (int j = 0; j < array.Length; j++) + { + array.SetValue(Convert.ChangeType(temp[j], property.PropertyType.GetElementType()), j); } + + property.SetValue(item, array, null); } else property.SetValue(item, value, null); - //Debug.Log("cell value: " + value.ToString()); } catch (Exception e) { diff --git a/Assets/QuickSheet/GDataPlugin/Editor/GDataDB/GDataDB/Impl/Serializer.cs b/Assets/QuickSheet/GDataPlugin/Editor/GDataDB/GDataDB/Impl/Serializer.cs index a1948e2..1c41fab 100644 --- a/Assets/QuickSheet/GDataPlugin/Editor/GDataDB/GDataDB/Impl/Serializer.cs +++ b/Assets/QuickSheet/GDataPlugin/Editor/GDataDB/GDataDB/Impl/Serializer.cs @@ -86,7 +86,18 @@ public T Deserialize(ListEntry e) { } else { - object[] temp = c.Value.Split(DELIMETER); + // remove whitespace between each of element + string str = new string(c.Value.ToCharArray() + .Where(ch => !Char.IsWhiteSpace(ch)) + .ToArray()); + + // remove ',', if it is found at the end. + char[] charToTrim = { ',', ' ' }; + str = str.TrimEnd(charToTrim); + + // split by ',' + object[] temp = str.Split(DELIMETER); + Array array = (Array)Activator.CreateInstance(property.PropertyType, temp.Length); for (int i = 0; i < array.Length; i++)