【重构】调整excel格式后相应调整了LubanAssistant对sheet的解析

main
walon 2021-10-27 14:44:33 +08:00
parent 13309b5da8
commit cd227c3f4c
5 changed files with 66 additions and 39 deletions

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.DataSources;
using Luban.Job.Cfg.DataVisitors; using Luban.Job.Cfg.DataVisitors;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils; using Luban.Job.Cfg.Utils;

View File

@ -226,7 +226,7 @@ namespace Luban.Job.Cfg.DataSources.Excel
return false; return false;
} }
foreach (var attr in metaStr.Substring(2).Split("&")) foreach (var attr in metaStr.Substring(2).Split('&'))
{ {
if (string.IsNullOrWhiteSpace(attr)) if (string.IsNullOrWhiteSpace(attr))
{ {

View File

@ -1,4 +1,5 @@
using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.DataSources;
using Luban.Job.Cfg.DataSources.Excel; using Luban.Job.Cfg.DataSources.Excel;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.Utils; using Luban.Job.Cfg.Utils;
@ -113,7 +114,7 @@ namespace LubanAssistant
{ {
var tableDataInfo = LastLoadTableData = await DataLoaderUtil.LoadTableDataAsync(RootDefineFile, InputDataDir, rawSheet.TableName); var tableDataInfo = LastLoadTableData = await DataLoaderUtil.LoadTableDataAsync(RootDefineFile, InputDataDir, rawSheet.TableName);
var title = ExcelUtil.ParseTitles(sheet); var title = ExcelUtil.ParseTitles(sheet);
ExcelUtil.FillRecords(sheet, rawSheet.TitleRowCount, title, tableDataInfo); ExcelUtil.FillRecords(sheet, title, tableDataInfo);
MessageBox.Show("加载成功"); MessageBox.Show("加载成功");
} }
catch (Exception e) catch (Exception e)
@ -159,7 +160,7 @@ namespace LubanAssistant
} }
} }
private void SaveRecords(Func<Worksheet, int, TableDataInfo, DefTable, Title, Task> saveTask) private void SaveRecords(Func<Worksheet, TableDataInfo, DefTable, Title, Task> saveTask)
{ {
Worksheet sheet = Globals.LubanAssistant.Application.ActiveSheet; Worksheet sheet = Globals.LubanAssistant.Application.ActiveSheet;
@ -182,7 +183,7 @@ namespace LubanAssistant
var tableDef = await DataLoaderUtil.LoadTableDefAsync(RootDefineFile, InputDataDir, tableName); var tableDef = await DataLoaderUtil.LoadTableDefAsync(RootDefineFile, InputDataDir, tableName);
var title = ExcelUtil.ParseTitles(sheet); var title = ExcelUtil.ParseTitles(sheet);
await saveTask(sheet, rawSheet.TitleRowCount, LastLoadTableData, tableDef, title); await saveTask(sheet, LastLoadTableData, tableDef, title);
} }
catch (Exception e) catch (Exception e)
{ {
@ -193,10 +194,10 @@ namespace LubanAssistant
private void BtnSaveAllClick(object sender, RibbonControlEventArgs e) private void BtnSaveAllClick(object sender, RibbonControlEventArgs e)
{ {
SaveRecords(async (Worksheet sheet, int titleRowNum, TableDataInfo tableDataInfo, DefTable defTable, Title title) => SaveRecords(async (Worksheet sheet, TableDataInfo tableDataInfo, DefTable defTable, Title title) =>
{ {
int usedRowNum = sheet.UsedRange.Rows.Count; int usedRowNum = sheet.UsedRange.Rows.Count;
int firstDataRowNum = titleRowNum + 2; int firstDataRowNum = ExcelUtil.GetTitleRowCount(sheet) + 1;
if (firstDataRowNum <= usedRowNum) if (firstDataRowNum <= usedRowNum)
{ {
var newRecords = ExcelUtil.LoadRecordsInRange(defTable, sheet, title, (sheet.Range[$"A{firstDataRowNum}:A{usedRowNum}"]).EntireRow); var newRecords = ExcelUtil.LoadRecordsInRange(defTable, sheet, title, (sheet.Range[$"A{firstDataRowNum}:A{usedRowNum}"]).EntireRow);
@ -252,10 +253,10 @@ namespace LubanAssistant
MessageBox.Show("没有选中的行"); MessageBox.Show("没有选中的行");
return; return;
} }
SaveRecords(async (Worksheet sheet, int titleRowNum, TableDataInfo tableDataInfo, DefTable defTable, Title title) => SaveRecords(async (Worksheet sheet, TableDataInfo tableDataInfo, DefTable defTable, Title title) =>
{ {
int usedRowNum = sheet.UsedRange.Rows.Count; int usedRowNum = sheet.UsedRange.Rows.Count;
if (titleRowNum + 1 < usedRowNum) if (ExcelUtil.GetTitleRowCount(sheet) < usedRowNum)
{ {
var newRecords = ExcelUtil.LoadRecordsInRange(defTable, sheet, title, selectRange.EntireRow); var newRecords = ExcelUtil.LoadRecordsInRange(defTable, sheet, title, selectRange.EntireRow);
await ExcelUtil.SaveRecordsAsync(InputDataDir, defTable, GetModifyRecords(LastLoadTableData, newRecords)); await ExcelUtil.SaveRecordsAsync(InputDataDir, defTable, GetModifyRecords(LastLoadTableData, newRecords));

View File

@ -2,6 +2,7 @@
using Luban.Job.Cfg.DataConverts; using Luban.Job.Cfg.DataConverts;
using Luban.Job.Cfg.DataExporters; using Luban.Job.Cfg.DataExporters;
using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.DataSources;
using Luban.Job.Cfg.DataSources.Excel; using Luban.Job.Cfg.DataSources.Excel;
using Luban.Job.Cfg.DataVisitors; using Luban.Job.Cfg.DataVisitors;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
@ -23,7 +24,7 @@ namespace LubanAssistant
{ {
public static RawSheet ParseRawSheet(Worksheet sheet, Range toSaveRecordRows) public static RawSheet ParseRawSheet(Worksheet sheet, Range toSaveRecordRows)
{ {
if (!ParseMetaAttrs(sheet, out var orientRow, out var titleRows, out var tableName)) if (!ParseMetaAttrs(sheet, out var orientRow, out var tableName))
{ {
throw new Exception($"meta行不合法"); throw new Exception($"meta行不合法");
} }
@ -47,12 +48,12 @@ namespace LubanAssistant
} }
cells.Add(rowCell); cells.Add(rowCell);
} }
return new RawSheet() { Title = title, TitleRowCount = titleRows, TableName = tableName, Cells = cells }; return new RawSheet() { Title = title, TableName = tableName, Cells = cells };
} }
public static RawSheet ParseRawSheetTitleOnly(Worksheet sheet) public static RawSheet ParseRawSheetTitleOnly(Worksheet sheet)
{ {
if (!ParseMetaAttrs(sheet, out var orientRow, out var titleRows, out var tableName)) if (!ParseMetaAttrs(sheet, out var orientRow, out var tableName))
{ {
throw new Exception($"meta行不合法"); throw new Exception($"meta行不合法");
} }
@ -64,29 +65,17 @@ namespace LubanAssistant
Title title = ParseTitles(sheet); Title title = ParseTitles(sheet);
var cells = new List<List<Cell>>(); var cells = new List<List<Cell>>();
return new RawSheet() { Title = title, TitleRowCount = titleRows, TableName = tableName, Cells = cells }; return new RawSheet() { Title = title, TableName = tableName, Cells = cells };
} }
public static bool ParseMetaAttrs(Worksheet sheet, out bool orientRow, out int titleRows, out string tableName) public static bool ParseMetaAttrs(Worksheet sheet, out bool orientRow, out string tableName)
{ {
Range metaRow = sheet.Rows[1]; string metaStr = ((Range)sheet.Cells[1, 1]).Value?.ToString();
return SheetLoadUtil.TryParseMeta(metaStr, out orientRow, out tableName);
var cells = new List<string>();
for (int i = 1, n = sheet.UsedRange.Columns.Count; i <= n; i++)
{
cells.Add(((Range)metaRow.Cells[1, i]).Value?.ToString());
}
return SheetLoadUtil.TryParseMeta(cells, out orientRow, out titleRows, out tableName);
} }
public static Title ParseTitles(Worksheet sheet) public static Title ParseTitles(Worksheet sheet)
{ {
int titleRows = 1;
Range c1 = sheet.Cells[2, 1];
if (c1.MergeCells)
{
titleRows = c1.MergeArea.Count;
}
var rootTile = new Title() var rootTile = new Title()
{ {
FromIndex = 0, FromIndex = 0,
@ -95,13 +84,31 @@ namespace LubanAssistant
Root = true, Root = true,
Tags = new Dictionary<string, string>(), Tags = new Dictionary<string, string>(),
}; };
ParseSubTitle(sheet, 2, titleRows + 1, rootTile); ParseSubTitle(sheet, 1, rootTile);
rootTile.ToIndex = rootTile.SubTitleList.Max(t => t.ToIndex); rootTile.ToIndex = rootTile.SubTitleList.Max(t => t.ToIndex);
rootTile.Init(); rootTile.Init();
return rootTile; return rootTile;
} }
private static void ParseSubTitle(Worksheet sheet, int rowIndex, int maxRowIndex, Title title) private static bool IsSubFieldRow(Range cell)
{
var s = cell.Value?.ToString()?.Trim();
return s == "##field";
}
private static bool IsTypeRow(Range cell)
{
var s = cell.Value?.ToString()?.Trim();
return s == "##type";
}
private static bool IsHeaderRow(Range cell)
{
var s = cell.Value?.ToString()?.Trim();
return !string.IsNullOrEmpty(s) && s.StartsWith("##");
}
private static void ParseSubTitle(Worksheet sheet, int rowIndex, Title title)
{ {
Range row = sheet.Rows[rowIndex]; Range row = sheet.Rows[rowIndex];
for (int i = title.FromIndex; i <= title.ToIndex; i++) for (int i = title.FromIndex; i <= title.ToIndex; i++)
@ -133,21 +140,37 @@ namespace LubanAssistant
} }
title.AddSubTitle(newSubTitle); title.AddSubTitle(newSubTitle);
} }
if (rowIndex < maxRowIndex) if (rowIndex < sheet.UsedRange.Rows.Count && IsSubFieldRow(sheet.Cells[rowIndex + 1, 1]))
{ {
foreach (var subTitle in title.SubTitleList) foreach (var subTitle in title.SubTitleList)
{ {
ParseSubTitle(sheet, rowIndex + 1, maxRowIndex, subTitle); ParseSubTitle(sheet, rowIndex + 1, subTitle);
} }
} }
} }
public static void FillRecords(Worksheet sheet, int titleRowNum, Title title, TableDataInfo tableDataInfo) public static int GetTitleRowCount(Worksheet sheet)
{
int firstDataRowIndex = 1;
while (true)
{
string tagCellStr = ((Range)sheet.Cells[firstDataRowIndex, 1]).Value?.ToString();
if (string.IsNullOrEmpty(tagCellStr) || !tagCellStr.StartsWith("##"))
{
break;
}
++firstDataRowIndex;
}
return firstDataRowIndex - 1;
}
public static void FillRecords(Worksheet sheet, Title title, TableDataInfo tableDataInfo)
{ {
int usedRowNum = sheet.UsedRange.Rows.Count; int usedRowNum = sheet.UsedRange.Rows.Count;
if (usedRowNum > titleRowNum + 1) int firstDataRowIndex = GetTitleRowCount(sheet) + 1;
if (usedRowNum >= firstDataRowIndex)
{ {
Range allDataRange = sheet.Range[sheet.Cells[titleRowNum + 2, 1], sheet.Cells[usedRowNum, sheet.UsedRange.Columns.Count]]; Range allDataRange = sheet.Range[sheet.Cells[firstDataRowIndex, 1], sheet.Cells[usedRowNum, sheet.UsedRange.Columns.Count]];
allDataRange.ClearContents(); allDataRange.ClearContents();
} }
@ -190,7 +213,7 @@ namespace LubanAssistant
} }
} }
Range recordFillRange = sheet.Range[sheet.Cells[titleRowNum + 2, 1], sheet.Cells[titleRowNum + 1 + dataRangeArray.Count, title.ToIndex + 1]]; Range recordFillRange = sheet.Range[sheet.Cells[firstDataRowIndex, 1], sheet.Cells[firstDataRowIndex + dataRangeArray.Count - 1, title.ToIndex + 1]];
recordFillRange.Value = resultDataRangeArray; recordFillRange.Value = resultDataRangeArray;
} }

View File

@ -282,6 +282,9 @@
<Compile Include="..\Luban.Job.Cfg\Source\DataCreators\YamlDataCreator.cs"> <Compile Include="..\Luban.Job.Cfg\Source\DataCreators\YamlDataCreator.cs">
<Link>Source\DataCreators\YamlDataCreator.cs</Link> <Link>Source\DataCreators\YamlDataCreator.cs</Link>
</Compile> </Compile>
<Compile Include="..\Luban.Job.Cfg\Source\DataExporters\RawJsonExportor.cs">
<Link>Source\DataExporters\RawJsonExportor.cs</Link>
</Compile>
<Compile Include="..\Luban.Job.Cfg\Source\DataSources\AbstractDataSource.cs"> <Compile Include="..\Luban.Job.Cfg\Source\DataSources\AbstractDataSource.cs">
<Link>Source\DataSources\AbstractDataSource.cs</Link> <Link>Source\DataSources\AbstractDataSource.cs</Link>
</Compile> </Compile>
@ -324,6 +327,9 @@
<Compile Include="..\Luban.Job.Cfg\Source\DataSources\Lua\LuaDataSource.cs"> <Compile Include="..\Luban.Job.Cfg\Source\DataSources\Lua\LuaDataSource.cs">
<Link>Source\DataSources\Lua\LuaDataSource.cs</Link> <Link>Source\DataSources\Lua\LuaDataSource.cs</Link>
</Compile> </Compile>
<Compile Include="..\Luban.Job.Cfg\Source\DataSources\Record.cs">
<Link>Source\DataSources\Record.cs</Link>
</Compile>
<Compile Include="..\Luban.Job.Cfg\Source\DataSources\Xml\XmlDataSource.cs"> <Compile Include="..\Luban.Job.Cfg\Source\DataSources\Xml\XmlDataSource.cs">
<Link>Source\DataSources\Xml\XmlDataSource.cs</Link> <Link>Source\DataSources\Xml\XmlDataSource.cs</Link>
</Compile> </Compile>
@ -402,9 +408,6 @@
<Compile Include="..\Luban.Job.Cfg\Source\Datas\DVector4.cs"> <Compile Include="..\Luban.Job.Cfg\Source\Datas\DVector4.cs">
<Link>Source\Datas\DVector4.cs</Link> <Link>Source\Datas\DVector4.cs</Link>
</Compile> </Compile>
<Compile Include="..\Luban.Job.Cfg\Source\Datas\Record.cs">
<Link>Source\Datas\Record.cs</Link>
</Compile>
<Compile Include="..\Luban.Job.Cfg\Source\DataVisitors\IDataActionVisitor.cs"> <Compile Include="..\Luban.Job.Cfg\Source\DataVisitors\IDataActionVisitor.cs">
<Link>Source\DataVisitors\IDataActionVisitor.cs</Link> <Link>Source\DataVisitors\IDataActionVisitor.cs</Link>
</Compile> </Compile>
@ -643,7 +646,6 @@
<Compile Include="Properties\AssemblyInfo.cs"> <Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Source\DataExporters\RawJsonExportor.cs" />
<EmbeddedResource Include="AssistantTab.resx"> <EmbeddedResource Include="AssistantTab.resx">
<DependentUpon>AssistantTab.cs</DependentUpon> <DependentUpon>AssistantTab.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>