protected object ConvertFrom(ICell cell, Type t, ref int cellColumnIndex) { object value = null; List <UnityQuickSheet.CellType> cellTypes = new List <UnityQuickSheet.CellType>(); cellTypes = MachineQueryCellTypeBinder.bind(this); List <bool> isArr = new List <bool>(); isArr = MachineQueryCellTypeBinder.bind2(this); if (cellTypes == null || cellTypes.Count == 0) { Debug.Log("cellTypes can recognize but use normal Convert, may have bugs. try to run machine->update"); value = normalConvertFrom(cell, t); } else { if (cell != null) { Debug.Log("ColumnIndex:" + cell.ColumnIndex); var myCell = cellTypes[cell.ColumnIndex]; var myiArr = isArr[cell.ColumnIndex]; cellColumnIndex = cell.ColumnIndex; value = UnityConvertFrom(cell, t, myCell, myiArr); } else { cellColumnIndex = cellColumnIndex + 1; var myCell = cellTypes[cellColumnIndex]; var myiArr = isArr[cellColumnIndex]; Debug.Log("cell is null,cellType:" + myCell + "," + myiArr + ",ColumnIndex should be " + cellColumnIndex); value = UnityConvertFrom(null, t, myCell, myiArr); } } return(value); }
public void giveCellType() { MachineQueryCellTypeBinder.readMachine(this); }
/// <summary> /// Import the specified excel file and prepare to set type of each cell. /// </summary> protected override void Import(bool reimport = false) { ExcelMachine machine = target as ExcelMachine; string path = machine.excelFilePath; string sheet = machine.WorkSheetName; if (string.IsNullOrEmpty(path)) { string msg = "You should specify spreadsheet file first!"; EditorUtility.DisplayDialog("Error", msg, "OK"); return; } if (!File.Exists(path)) { string msg = string.Format("File at {0} does not exist.", path); EditorUtility.DisplayDialog("Error", msg, "OK"); return; } int startRowIndex = 1; string error = string.Empty; 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.HasColumnHeader() && reimport == false) { var headerDic = machine.ColumnHeaderList.ToDictionary(header => header.name); // collect non-changed column headers var exist = titleList.Select(t => GetColumnHeaderString(t)) .Where(e => headerDic.ContainsKey(e) == true) .Select(t => new ColumnHeader { name = t, type = headerDic[t].type, isArray = headerDic[t].isArray, OrderNO = headerDic[t].OrderNO }); // collect newly added or changed column headers var changed = titleList.Select(t => GetColumnHeaderString(t)) .Where(e => headerDic.ContainsKey(e) == false) .Select(t => ParseColumnHeader(t, titleList.IndexOf(t))); // merge two list via LINQ var merged = exist.Union(changed).OrderBy(x => x.OrderNO); machine.ColumnHeaderList.Clear(); machine.ColumnHeaderList = merged.ToList(); Debug.Log("readMachine!"); MachineQueryCellTypeBinder.readMachine(machine); for (var i = 0; i < machine.ColumnHeaderList.Count; i++) { Debug.Log(machine.ColumnHeaderList[i].type.ToString() + (machine.ColumnHeaderList[i].isArray?",isArr":"")); } } else { machine.ColumnHeaderList.Clear(); if (titleList.Count > 0) { int order = 0; machine.ColumnHeaderList = titleList.Select(e => ParseColumnHeader(e, order++)).ToList(); } else { string msg = string.Format("An empty workhheet: [{0}] ", sheet); Debug.LogWarning(msg); } MachineQueryCellTypeBinder.readMachine(machine); } EditorUtility.SetDirty(machine); AssetDatabase.SaveAssets(); }