protected void ImportAll() { ExcelMachine machine = target as ExcelMachine; foreach (var fileInfo in new DirectoryInfo(machine.fileFolder).GetFiles()) { var path = fileInfo.FullName; 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; } string error = string.Empty; var titles = new ExcelQuery(path, 0).GetTitle(2, 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 (titleList.Count == 0) { string msg = string.Format("An empty workhheet: [{0}] ", 0); Debug.LogWarning(msg); } } EditorUtility.SetDirty(machine); AssetDatabase.SaveAssets(); }
/// <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 = 0; 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(); } 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); } } EditorUtility.SetDirty(machine); AssetDatabase.SaveAssets(); }
/// <summary> /// Import the specified excel file and prepare to set type of each cell. /// </summary> protected override void Import(bool reimport = false) { base.Import(reimport); ExcelMachine machine = target as ExcelMachine; string path = machine.excelFilePath; string sheet = machine.WorkSheetName; if (string.IsNullOrEmpty(path)) { EditorUtility.DisplayDialog( "Error", "You should specify spreadsheet file first!", "OK" ); return; } if (!File.Exists(path)) { EditorUtility.DisplayDialog( "Error", "File at " + path + " does not exist.", "OK" ); return; } var titles = new ExcelQuery(path, sheet).GetTitle(); List <string> titleList = titles.ToList(); if (machine.HasHeadColumn() && reimport == false) { var headerDic = machine.HeaderColumnList.ToDictionary(header => header.name); // collect non changed header columns var exist = from t in titleList where headerDic.ContainsKey(t) == true select new HeaderColumn { name = t, type = headerDic[t].type, OrderNO = headerDic[t].OrderNO }; // collect newly added or changed header columns 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 var merged = exist.Union(changed).OrderBy(x => x.OrderNO); machine.HeaderColumnList.Clear(); machine.HeaderColumnList = merged.ToList(); } else { machine.HeaderColumnList.Clear(); if (titles != null) { int i = 0; foreach (string s in titles) { machine.HeaderColumnList.Add(new HeaderColumn { name = s, type = CellType.Undefined, OrderNO = i }); i++; } } else { Debug.LogWarning("The WorkSheet [" + sheet + "] may be empty."); } } EditorUtility.SetDirty(machine); AssetDatabase.SaveAssets(); }
/// <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)) { EditorUtility.DisplayDialog( "Error", "You should specify spreadsheet file first!", "OK" ); return; } if (!File.Exists(path)) { EditorUtility.DisplayDialog( "Error", "File at " + path + " does not exist.", "OK" ); return; } int startRowIndex = 0; 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 = from t in titleList where headerDic.ContainsKey(t) == true select 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 = from t in titleList where headerDic.ContainsKey(t) == false select new ColumnHeader { name = t, type = CellType.Undefined, OrderNO = titleList.IndexOf(t) }; // merge two list via LINQ var merged = exist.Union(changed).OrderBy(x => x.OrderNO); machine.ColumnHeaderList.Clear(); machine.ColumnHeaderList = merged.ToList(); } else { machine.ColumnHeaderList.Clear(); if (titles != null) { int i = 0; foreach (string s in titles) { machine.ColumnHeaderList.Add(new ColumnHeader { name = s, type = CellType.Undefined, OrderNO = i }); i++; } } else { Debug.LogWarning("The WorkSheet [" + sheet + "] may be empty."); } } EditorUtility.SetDirty(machine); AssetDatabase.SaveAssets(); }