static public void ConvertXls2Json() { #if !UNITY_WEBPLAYER Object[] obj = Selection.objects; List <string> filePathList = new List <string>(); for (int i = 0; i < obj.Length; i++) { string path = AssetDatabase.GetAssetPath(obj[i]); path = Path.Combine(Directory.GetCurrentDirectory(), path); for (int j = 0; j < _readExtention.Length; j++) { if (path.Contains(_readExtention[j])) { if (!filePathList.Contains(path)) { filePathList.Add(path); } } } } for (int i = 0; i < filePathList.Count; i++) { AddExcelFile(filePathList [i]); } EditorWindow.GetWindow <ExcelToJsonEditor>("Excels2JsonEditor"); #else CustomEditorTools.DisplayNoticeDialog("Error", "The editor does not work on the Web Player environment. We recommend a PC environment."); return; #endif }
static public void OpenXls2Json() { #if !UNITY_WEBPLAYER EditorWindow.GetWindow <ExcelToJsonEditor>("Excels2JsonEditor"); #else CustomEditorTools.DisplayNoticeDialog("Error", "The editor does not work on the Web Player environment. We recommend a PC environment."); return; #endif }
static void AddExcelFile(string filePath) { if (IsContainExcelFile(filePath)) { CustomEditorTools.DisplayNoticeDialog("Warning", "Already Add Excel File"); return; } Book book = new Book(); book.filePath = filePath; book.SetBook(); if (book.isData) { _currentBookInfo.Add(book); } }
void DisplaySheetInfo(Sheet sheet) { string outputPath = PlayerPrefs.GetString("ExcelToJson_OutputPath"); EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(30)); GUILayout.Space(10f); CustomEditorTools.DrawLabel(sheet.sheetName, 100f, "OL Title"); GUILayout.Space(5f); if (CustomEditorTools.DrawMiniButton("Convert")) { ExcelToJsonConvert.ConvertToJson(sheet, outputPath); CustomEditorTools.DisplayNoticeDialog("Notice", "Completed Convert Excel To Json"); return; } GUILayout.Space(5f); EditorGUILayout.LabelField(sheet.outputName + ".json"); EditorGUILayout.EndHorizontal(); }
IWorkbook GetWorkBook() { #if !UNITY_WEBPLAYER FileInfo fileInfo = new FileInfo(filePath); if (!fileInfo.Exists) { return(null); } try { FileStream stream = fileInfo.OpenRead(); if (stream == null) { stream.Close(); return(null); } fileName = GetFileName(filePath); IWorkbook book; if (fileName.Contains("xlsx")) { book = new XSSFWorkbook(stream); } else { book = new HSSFWorkbook(stream); } return(book); } catch (IOException e) { CustomEditorTools.DisplayNoticeDialog("Error", "Close Excel File And Try Again\n" + e.Message); } #endif return(null); }
void DisplayOutputJson() { EditorGUILayout.Separator(); CustomEditorTools.DrawHeader("Output Excel To Json"); CustomEditorTools.BeginContents(); EditorGUILayout.BeginHorizontal(); string outputPath = PlayerPrefs.GetString("ExcelToJson_OutputPath"); if (string.IsNullOrEmpty(outputPath)) { outputPath = Application.dataPath + "/" + _jsonFolder; PlayerPrefs.SetString("ExcelToJson_OutputPath", outputPath); } DirectoryInfo dirInfo = new DirectoryInfo(outputPath); if (!dirInfo.Exists) { Directory.CreateDirectory(outputPath); } EditorGUILayout.TextField(outputPath); if (CustomEditorTools.DrawMiniButton("Choose")) { string changePath = EditorUtility.OpenFolderPanel("Output Json Folder", outputPath, ""); if (!string.IsNullOrEmpty(changePath)) { outputPath = changePath; PlayerPrefs.SetString("ExcelToJson_OutputPath", outputPath); } } EditorGUILayout.EndHorizontal(); CustomEditorTools.EndContens(); }
void DisplayExcelFile() { string outputPath = PlayerPrefs.GetString("ExcelToJson_OutputPath"); CustomEditorTools.DrawHeader("Excel File Info"); CustomEditorTools.BeginContents(); EditorGUILayout.BeginHorizontal(); if (CustomEditorTools.DrawMiniButton("Clear Excel Data")) { _currentBookInfo.Clear(); } if (CustomEditorTools.DrawMiniButton("Remove All Json")) { DirectoryInfo outputDir = new DirectoryInfo(outputPath); if (outputDir.Exists) { FileInfo[] files = outputDir.GetFiles(); for (int i = files.Length - 1; i >= 0; i--) { FileUtil.DeleteFileOrDirectory(outputPath + "/" + files[i].Name); } } AssetDatabase.Refresh(); } if (CustomEditorTools.DrawMiniButton("All Convert")) { if (_currentBookInfo.Count <= 0) { CustomEditorTools.DisplayNoticeDialog("Warring", "Excel Data Empty, Select Excel Data"); return; } for (int i = 0; i < _currentBookInfo.Count; i++) { for (int j = 0; j < _currentBookInfo [i].sheet.Length; j++) { ExcelToJsonConvert.ConvertToJson(_currentBookInfo [i].sheet[j], outputPath); } } CustomEditorTools.DisplayNoticeDialog("Notice", "Completed Convert Excel To Json"); } EditorGUILayout.EndHorizontal(); CustomEditorTools.EndContens(); CustomEditorTools.BeginContents(); _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, GUILayout.MinHeight(200f)); for (int i = _currentBookInfo.Count - 1; i >= 0; i--) { CustomEditorTools.BeginContents(); EditorGUILayout.BeginHorizontal(); bool foldOut = GUILayout.Toggle(_currentBookInfo[i].foldOut, _currentBookInfo[i].fileName, "Foldout", GUILayout.MaxWidth(200f), GUILayout.MinWidth(200f)); if (foldOut != _currentBookInfo[i].foldOut) { _currentBookInfo[i].foldOut = foldOut; } EditorGUILayout.EndHorizontal(); if (_currentBookInfo[i].foldOut) { if (_currentBookInfo[i].sheet == null) { continue; } for (int j = 0; j < _currentBookInfo[i].sheet.Length; j++) { DisplaySheetInfo(_currentBookInfo [i].sheet [j]); } } CustomEditorTools.EndContens(); } EditorGUILayout.EndScrollView(); CustomEditorTools.EndContens(); }
void DisplayLoadExcelFolder() { EditorGUILayout.Separator(); CustomEditorTools.DrawHeader("Load Excel Folder"); CustomEditorTools.BeginContents(); EditorGUILayout.BeginHorizontal(); string loadPath = PlayerPrefs.GetString("ExcelLoadPath"); if (string.IsNullOrEmpty(loadPath)) { loadPath = Application.dataPath + "/" + _excelFolder; PlayerPrefs.SetString("ExcelLoadPath", loadPath); } DirectoryInfo dirInfo = new DirectoryInfo(loadPath); if (!dirInfo.Exists) { Directory.CreateDirectory(loadPath); } EditorGUILayout.TextField(loadPath); if (CustomEditorTools.DrawMiniButton("Choose")) { string changePath = EditorUtility.OpenFolderPanel("Output Json Folder", loadPath, ""); if (!string.IsNullOrEmpty(changePath)) { loadPath = changePath; PlayerPrefs.SetString("ExcelLoadPath", loadPath); } } if (CustomEditorTools.DrawMiniButton("Load")) { DirectoryInfo loadDir = new DirectoryInfo(loadPath); if (!loadDir.Exists) { CustomEditorTools.DisplayNoticeDialog("Warning", "Not Exist Excel Directory"); return; } _currentBookInfo.Clear(); FileInfo[] file = loadDir.GetFiles(); for (int i = 0; i < file.Length; i++) { string[] extention = file[i].Name.Split('.'); int lastIndex = extention.Length - 1; bool isRead = false; for (int j = 0; j < _readExtention.Length; j++) { if (extention[lastIndex].Equals(_readExtention[j])) { isRead = true; break; } } if (isRead) { AddExcelFile(loadPath + "/" + file [i].Name); } } if (_currentBookInfo.Count <= 0) { CustomEditorTools.DisplayNoticeDialog("Warning", "Not Exist Excel File"); return; } } EditorGUILayout.EndHorizontal(); CustomEditorTools.EndContens(); }
bool LoadFieldInfoByExcel(JSON_TYPE lastType, int index = 0) { if (_sheet == null) { return(false); } List <string> tableField = new List <string>(); for (int row = index; row <= _sheet.LastRowNum; row++) { JSON_TYPE type = GetJsonType(row, lastType); if (type != JSON_TYPE.ERROR) { lastType = type; } IRow rowInfo = _sheet.GetRow(row); if (rowInfo == null) { return(false); } if (type == JSON_TYPE.JSON_ARRAY_START || type == JSON_TYPE.JSON_ARRAY_END || type == JSON_TYPE.JSON_TABLE_END || type == JSON_TYPE.JSON_START || type == JSON_TYPE.JSON_END) { string value = ""; ICell cellField = rowInfo.GetCell(1); if (cellField != null) { value = cellField.StringCellValue; } else { if (type == JSON_TYPE.JSON_ARRAY_START || type == JSON_TYPE.JSON_TABLE_START) { CustomEditorTools.DisplayNoticeDialog("Warning Sheet : " + sheetName, "table and field names array is required. If the field name is not difficult to use and accessible to JSON."); } } AddFieldInfo(type, "", value); } if (type == JSON_TYPE.JSON_VALUE) { ICell cellField = rowInfo.GetCell(1); if (cellField == null) { return(false); } string field = cellField.StringCellValue; ICell cellValue = rowInfo.GetCell(2); if (cellValue == null) { return(false); } if (cellValue.CellType == CellType.Numeric) { AddFieldInfo(JSON_TYPE.JSON_VALUE, field, cellValue.NumericCellValue.ToString()); } else { AddFieldInfo(JSON_TYPE.JSON_VALUE, field, cellValue.StringCellValue); } } if (type == JSON_TYPE.JSON_TABLE_START) { ICell cell = rowInfo.GetCell(1); if (cell == null) { return(false); } AddFieldInfo(type, "", cell.StringCellValue); } if (type == JSON_TYPE.JSON_TABLE_FIELD) { for (int col = 0; col < rowInfo.LastCellNum; col++) { ICell cell = rowInfo.GetCell(col); if (cell == null) { return(false); } tableField.Add(cell.StringCellValue); } } if (type == JSON_TYPE.JSON_TABLE_VALUE) { AddFieldInfo(JSON_TYPE.JSON_START, "", ""); for (int col = 0; col < rowInfo.LastCellNum; col++) { ICell cell = rowInfo.GetCell(col); if (cell == null) { return(false); } if (cell.CellType == CellType.Numeric) { AddFieldInfo(JSON_TYPE.JSON_VALUE, tableField[col], cell.NumericCellValue.ToString()); } else { AddFieldInfo(JSON_TYPE.JSON_VALUE, tableField[col], cell.StringCellValue); } } AddFieldInfo(JSON_TYPE.JSON_END, "", ""); } } return(true); }