/// <summary> /// Generate AssetPostprocessor editor script file. /// </summary> protected override void CreateAssetCreationScript(BaseMachine m, ScriptPrescription sp) { ExcelMachine machine = target as ExcelMachine; sp.className = machine.WorkSheetName; sp.dataClassName = machine.WorkSheetName + "Data"; sp.worksheetClassName = machine.WorkSheetName; // where the imported excel file is. sp.importedFilePath = machine.excelFilePath; // path where the .asset file will be created. string path = Path.GetDirectoryName(machine.excelFilePath); path += "/" + machine.WorkSheetName + ".asset"; sp.assetFilepath = path.Replace('\\', '/'); sp.assetPostprocessorClass = machine.WorkSheetName + "AssetPostprocessor"; sp.template = GetTemplate("PostProcessor"); var query = new ExcelQuery(machine.excelFilePath, machine.WorkSheetName); sp.enumDefines = query?.GetEnumDefine(); // write a script to the given folder. using (var writer = new StreamWriter(TargetPathForAssetPostProcessorFile(machine.WorkSheetName))) { writer.Write(new ScriptGenerator(sp).ToString()); writer.Close(); } }
void CreateDataClassScript() { ExcelMachine machine = target as ExcelMachine; #if UNITY_EDITOR_WIN var files = new DirectoryInfo(machine.fileFolder).GetFiles().Where(x => x.Extension == ".xlsx"); #else // for UNITY_EDITOR_OSX var files = new DirectoryInfo(machine.fileFolder).GetFiles().Where(x => x.Extension == ".xls"); #endif foreach (var fileInfo in files) { var className = Path.GetFileNameWithoutExtension(fileInfo.Name); // check the directory path exists var targetPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(target)); string fullPath = Path.Combine(targetPath + "/Runtime", className + "ExcelData." + "cs"); string folderPath = Path.GetDirectoryName(fullPath); if (!Directory.Exists(folderPath)) { EditorUtility.DisplayDialog( "Warning", "The folder for runtime script files does not exist. Check the path " + folderPath + " exists.", "OK" ); return; } string error = string.Empty; var titles = new ExcelQuery(fileInfo.FullName, 0).GetTitle(2, ref error); var types = new ExcelQuery(fileInfo.FullName, 0).GetTitle(1, ref error); var comment = new ExcelQuery(fileInfo.FullName, 0).GetTitle(0, ref error); List <MemberFieldData> fieldList = new List <MemberFieldData>(); for (int i = 0; i < titles.Length; i++) { MemberFieldData member = new MemberFieldData(); member.Name = titles[i]; member.type = MemberFieldData.GetType(types[i]); fieldList.Add(member); } var sp = new ScriptPrescription { className = className + "ExcelData", template = GetTemplate("DataClass"), memberFields = fieldList.ToArray() }; // write a script to the given folder. using (var writer = new StreamWriter(fullPath)) { writer.Write(new ScriptGenerator(sp).ToString()); writer.Close(); } } }
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(); }
public static void SetupSelectExcels() { if (Selection.assetGUIDs.Length == 0) { EditorUtility.DisplayDialog("Error", "Select excel files to setup!", "OK"); return; } var selectObjs = new List <Object>(); foreach (var guid in Selection.assetGUIDs) { var path = AssetDatabase.GUIDToAssetPath(guid); if (!IsExcel(path)) { continue; } ExcelSettings.Instance._waitImportPath.Add(path); var excelQuery = new ExcelQuery(path); var sheets = excelQuery.GetSheetNames(); for (int i = 0; i < sheets.Length; i++) { var machine = ExcelMachine.CreateScriptMachineAsset(); machine.excelFilePath = path; machine.SheetNames = sheets; machine.WorkSheetName = sheets[i]; machine.CurrentSheetIndex = i; machine.SpreadSheetName = Path.GetFileName(path); ReimportMachine(machine, true); BaseMachineEditor.CreateGenerateDirectory(machine); GenerateMachine(machine, false); RenameMachineAsset(machine); selectObjs.Add(machine); Debug.LogFormat("Setup finished! file:{0}, Sheet:{1}", machine.SpreadSheetName, sheets[i]); } } Selection.objects = selectObjs.ToArray(); AssetDatabase.Refresh(); }
/// <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(); }
public void copyExcelQuery(ExcelQuery e) { filepath = e.filepath; initExcelQuery(filepath, e.sheet.SheetName); }
public ExcelQuery(ExcelQuery e) { copyExcelQuery(e); }
/// <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(); }