【重构】调整excel格式后相应调整了LubanAssistant对sheet的解析
parent
13309b5da8
commit
cd227c3f4c
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Datas;
|
||||
using Luban.Job.Cfg.DataSources;
|
||||
using Luban.Job.Cfg.DataVisitors;
|
||||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Cfg.Utils;
|
||||
|
|
@ -226,7 +226,7 @@ namespace Luban.Job.Cfg.DataSources.Excel
|
|||
return false;
|
||||
}
|
||||
|
||||
foreach (var attr in metaStr.Substring(2).Split("&"))
|
||||
foreach (var attr in metaStr.Substring(2).Split('&'))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(attr))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Luban.Job.Cfg.Datas;
|
||||
using Luban.Job.Cfg.DataSources;
|
||||
using Luban.Job.Cfg.DataSources.Excel;
|
||||
using Luban.Job.Cfg.Defs;
|
||||
using Luban.Job.Cfg.Utils;
|
||||
|
|
@ -113,7 +114,7 @@ namespace LubanAssistant
|
|||
{
|
||||
var tableDataInfo = LastLoadTableData = await DataLoaderUtil.LoadTableDataAsync(RootDefineFile, InputDataDir, rawSheet.TableName);
|
||||
var title = ExcelUtil.ParseTitles(sheet);
|
||||
ExcelUtil.FillRecords(sheet, rawSheet.TitleRowCount, title, tableDataInfo);
|
||||
ExcelUtil.FillRecords(sheet, title, tableDataInfo);
|
||||
MessageBox.Show("加载成功");
|
||||
}
|
||||
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;
|
||||
|
||||
|
|
@ -182,7 +183,7 @@ namespace LubanAssistant
|
|||
|
||||
var tableDef = await DataLoaderUtil.LoadTableDefAsync(RootDefineFile, InputDataDir, tableName);
|
||||
var title = ExcelUtil.ParseTitles(sheet);
|
||||
await saveTask(sheet, rawSheet.TitleRowCount, LastLoadTableData, tableDef, title);
|
||||
await saveTask(sheet, LastLoadTableData, tableDef, title);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
@ -193,10 +194,10 @@ namespace LubanAssistant
|
|||
|
||||
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 firstDataRowNum = titleRowNum + 2;
|
||||
int firstDataRowNum = ExcelUtil.GetTitleRowCount(sheet) + 1;
|
||||
if (firstDataRowNum <= usedRowNum)
|
||||
{
|
||||
var newRecords = ExcelUtil.LoadRecordsInRange(defTable, sheet, title, (sheet.Range[$"A{firstDataRowNum}:A{usedRowNum}"]).EntireRow);
|
||||
|
|
@ -252,10 +253,10 @@ namespace LubanAssistant
|
|||
MessageBox.Show("没有选中的行");
|
||||
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;
|
||||
if (titleRowNum + 1 < usedRowNum)
|
||||
if (ExcelUtil.GetTitleRowCount(sheet) < usedRowNum)
|
||||
{
|
||||
var newRecords = ExcelUtil.LoadRecordsInRange(defTable, sheet, title, selectRange.EntireRow);
|
||||
await ExcelUtil.SaveRecordsAsync(InputDataDir, defTable, GetModifyRecords(LastLoadTableData, newRecords));
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using Luban.Job.Cfg.DataConverts;
|
||||
using Luban.Job.Cfg.DataExporters;
|
||||
using Luban.Job.Cfg.Datas;
|
||||
using Luban.Job.Cfg.DataSources;
|
||||
using Luban.Job.Cfg.DataSources.Excel;
|
||||
using Luban.Job.Cfg.DataVisitors;
|
||||
using Luban.Job.Cfg.Defs;
|
||||
|
|
@ -23,7 +24,7 @@ namespace LubanAssistant
|
|||
{
|
||||
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行不合法");
|
||||
}
|
||||
|
|
@ -47,12 +48,12 @@ namespace LubanAssistant
|
|||
}
|
||||
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)
|
||||
{
|
||||
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行不合法");
|
||||
}
|
||||
|
|
@ -64,29 +65,17 @@ namespace LubanAssistant
|
|||
|
||||
Title title = ParseTitles(sheet);
|
||||
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];
|
||||
|
||||
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);
|
||||
string metaStr = ((Range)sheet.Cells[1, 1]).Value?.ToString();
|
||||
return SheetLoadUtil.TryParseMeta(metaStr, out orientRow, out tableName);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
FromIndex = 0,
|
||||
|
|
@ -95,13 +84,31 @@ namespace LubanAssistant
|
|||
Root = true,
|
||||
Tags = new Dictionary<string, string>(),
|
||||
};
|
||||
ParseSubTitle(sheet, 2, titleRows + 1, rootTile);
|
||||
ParseSubTitle(sheet, 1, rootTile);
|
||||
rootTile.ToIndex = rootTile.SubTitleList.Max(t => t.ToIndex);
|
||||
rootTile.Init();
|
||||
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];
|
||||
for (int i = title.FromIndex; i <= title.ToIndex; i++)
|
||||
|
|
@ -133,21 +140,37 @@ namespace LubanAssistant
|
|||
}
|
||||
title.AddSubTitle(newSubTitle);
|
||||
}
|
||||
if (rowIndex < maxRowIndex)
|
||||
if (rowIndex < sheet.UsedRange.Rows.Count && IsSubFieldRow(sheet.Cells[rowIndex + 1, 1]))
|
||||
{
|
||||
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;
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -282,6 +282,9 @@
|
|||
<Compile Include="..\Luban.Job.Cfg\Source\DataCreators\YamlDataCreator.cs">
|
||||
<Link>Source\DataCreators\YamlDataCreator.cs</Link>
|
||||
</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">
|
||||
<Link>Source\DataSources\AbstractDataSource.cs</Link>
|
||||
</Compile>
|
||||
|
|
@ -324,6 +327,9 @@
|
|||
<Compile Include="..\Luban.Job.Cfg\Source\DataSources\Lua\LuaDataSource.cs">
|
||||
<Link>Source\DataSources\Lua\LuaDataSource.cs</Link>
|
||||
</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">
|
||||
<Link>Source\DataSources\Xml\XmlDataSource.cs</Link>
|
||||
</Compile>
|
||||
|
|
@ -402,9 +408,6 @@
|
|||
<Compile Include="..\Luban.Job.Cfg\Source\Datas\DVector4.cs">
|
||||
<Link>Source\Datas\DVector4.cs</Link>
|
||||
</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">
|
||||
<Link>Source\DataVisitors\IDataActionVisitor.cs</Link>
|
||||
</Compile>
|
||||
|
|
@ -643,7 +646,6 @@
|
|||
<Compile Include="Properties\AssemblyInfo.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Source\DataExporters\RawJsonExportor.cs" />
|
||||
<EmbeddedResource Include="AssistantTab.resx">
|
||||
<DependentUpon>AssistantTab.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
|
|
|
|||
Loading…
Reference in New Issue