fix:完善excel,现在的不需要手动创建文件夹,而且就算误删内容也可以重新生成

pull/1/head
zc 2024-10-30 21:32:36 +08:00
parent 33e8e73d2c
commit e54d4b6cdb
3 changed files with 90 additions and 21 deletions

View File

@ -0,0 +1,7 @@
using UnityEngine;
using System.Collections.Generic;
public static class #CLASSNAME#
{
#CONSTCONTENT#
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2d7d2225392a4369834946f18709f9db
timeCreated: 1730292580

View File

@ -19,24 +19,28 @@ namespace ZEditor
{
private static string folderPath = $"{Application.streamingAssetsPath}/Excel/";
private static string genPath = $"{Application.streamingAssetsPath}/ExcelData/";
private static string genDataPath = $"{Application.streamingAssetsPath}/ExcelData/";
private static string genCSPath = $"{Application.dataPath}/DemoGame/GameScript/Hotfix/Generate/Excel2CS/";
private static string templatePath = $"{Application.dataPath}/DemoGame/GameRes/ExcelTemplate.txt";
[MenuItem("Tool/加载表格数据")]
static void Load()
{
string path = $"{Application.streamingAssetsPath}/ExcelData/Test.json";
var allText = File.ReadAllText(path);
var testData = JsonConvert.DeserializeObject<TestDatas>(allText);
Debug.Log($"{testData.TestList[0].Id} , {testData.TestList[0].Subject} , {testData.TestList[0].JX} ");
}
private static string templatePath = $"{Application.dataPath}/DemoGame/GameRes/ExcelTemplate.txt";
private static string constTemplatePath = $"{Application.dataPath}/DemoGame/GameRes/ExcelConstTemplate.txt";
// [MenuItem("Tool/加载表格数据")]
// static void Load()
// {
// string path = $"{Application.streamingAssetsPath}/ExcelData/Test.json";
// var allText = File.ReadAllText(path);
// var testData = JsonConvert.DeserializeObject<TestDatas>(allText);
// Debug.Log($"{testData.TestList[0].Id} , {testData.TestList[0].Subject} , {testData.TestList[0].JX} ");
// }
[MenuItem("Tool/表格数据生成")]
static void Gen()
{
DeleteAndCreateFolder();
DirectoryInfo info = new DirectoryInfo(folderPath);
var fileInfos = info.GetFiles();
foreach (var fileInfo in fileInfos)
@ -48,6 +52,71 @@ namespace ZEditor
LoadExcelAndCreateCS(fileInfo.FullName, fileName);
LoadExcelAndCreateData(fileInfo.FullName, fileName);
}
LoadExcelAndGenData(fileInfos);
}
static void DeleteAndCreateFolder()
{
DirectoryInfo info = new DirectoryInfo(genCSPath);
var fileInfos = info.GetFiles();
foreach (var fileInfo in fileInfos)
{
fileInfo.Delete();
}
info = new DirectoryInfo(genDataPath);
fileInfos = info.GetFiles();
foreach (var fileInfo in fileInfos)
{
fileInfo.Delete();
}
if (!Directory.Exists(genDataPath))
Directory.CreateDirectory(genDataPath);
if (!Directory.Exists(genCSPath))
Directory.CreateDirectory(genCSPath);
}
static void LoadExcelAndGenData(FileInfo[] fileInfos)
{
StringBuilder sb = new StringBuilder();
foreach (var fileInfo in fileInfos)
{
if (fileInfo.Name.Contains(".meta"))
continue;
string fileName = fileInfo.Name.Replace(".xlsx", "");
//查找整个文档
Workbook book = new Workbook(fileInfo.FullName);
//获取一个工作表
Worksheet sheet = book.Worksheets[0];
for (var i = 2; i < sheet.Cells.Rows.Count - 2; i++)
{
var cellsColumn = sheet.Cells[i + 3, 2].Value;
if (cellsColumn == null)
{
continue;
}
string str = cellsColumn.ToString();
var id = int.Parse(str);
sb.AppendLine($"public const int {fileName}_{str} = {id};");
}
}
string text = File.ReadAllText(constTemplatePath);
text = text.Replace("#CONSTCONTENT#", sb.ToString());
text = text.Replace("#CLASSNAME#", "ExcelConstData");
var csPath = $"{genCSPath}/ExcelConstData.cs";
if (!File.Exists(csPath))
{
File.Create(csPath).Dispose();
}
File.WriteAllText(csPath, text);
}
/// <summary>
@ -117,8 +186,6 @@ namespace ZEditor
text = text.Replace("#LISTNAME#", $"{fileName}List");
Debug.Log(text);
string filePath = genCSPath + "/" + newName + ".cs";
if (!File.Exists(filePath))
{
@ -135,14 +202,7 @@ namespace ZEditor
//获取一个工作表
Worksheet sheet = book.Worksheets[0];
Debug.Log($"{sheet.Cells.Rows.Count}行 , {sheet.Cells.Columns.Count}列");
// List<string> zhuShi = new List<string>();
// List<string> name = new List<string>();
// List<string> type = new List<string>();
DataTable table = new DataTable();
// table.Columns
for (var i = 0; i < sheet.Cells.Rows.Count + 2; i++)
{
@ -154,7 +214,6 @@ namespace ZEditor
string name = sheet.Cells[3, j + 2].Value.ToString();
var typeName = sheet.Cells[4, j + 2].Value.ToString();
Type type = GetType(typeName);
// Debug.Log($"{name} {typeName} {type}");
table.Columns.Add(name, type);
}
else if (i > 4)
@ -256,7 +315,7 @@ namespace ZEditor
var json = JsonConvert.SerializeObject(table);
string newStr = "{" + $"\"{fileName}List\":" + json + "}";
Debug.Log(newStr);
string filePath = genPath + "/" + fileName + ".json";
string filePath = genDataPath + "/" + fileName + ".json";
if (!File.Exists(filePath))
{
File.Create(filePath).Dispose();