Skip to content

Commit

Permalink
Fixed #20 issue. Now, it does not need to ',' at the end of cell for …
Browse files Browse the repository at this point in the history
…array type (if there is anyone, it is removed when it is imported). Same as both of excel and google spreadsheet.
  • Loading branch information
kimsama committed Aug 22, 2016
1 parent e98147a commit 9a0a174
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
13 changes: 11 additions & 2 deletions Assets/QuickSheet/Editor/Util/Util.cs
Original file line number Diff line number Diff line change
@@ -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[] {
Expand Down
47 changes: 23 additions & 24 deletions Assets/QuickSheet/ExcelPlugin/Editor/ExcelQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,36 +132,35 @@ public List<T> Deserialize<T>(int start = 1)

if (property.PropertyType.IsArray)
{
// collect string values.
CsvParser parser = new CsvParser(value as string);
List<string> tokenizedValues = new List<string>();
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down

0 comments on commit 9a0a174

Please sign in to comment.