public static string DealWithFormulaSheetGo(ISheet sheet) { CodeTemplate.curlang = CodeTemplate.Langue.Go; StringBuilder sb = new StringBuilder(); Dictionary <CellCoord, List <CellCoord> > abouts = new Dictionary <CellCoord, List <CellCoord> >(); string sheetName = sheet.SheetName.Substring(3); string SheetName = sheetName.Substring(0, 1).ToUpper() + sheetName.Substring(1); sb.AppendLine("package config"); sb.AppendLine("type " + SheetName + "FormulaSheet struct {"); sb.AppendLine("formulaSheet"); sb.AppendLine("}"); sb.AppendLine("var " + sheetName + "FormaulaTemplate *formulaSheetTemplate"); sb.AppendLine("func loadFormula" + SheetName + "() {"); sb.AppendLine(sheetName + "FormaulaTemplate = new(formulaSheetTemplate)"); sb.AppendLine(sheetName + "FormaulaTemplate.datas = make(map[int32]float64)"); sb.AppendLine(sheetName + "FormaulaTemplate.relation = make(map[int32][]int32)"); sb.AppendLine(sheetName + "FormaulaTemplate.funcs = make(map[int32]func(*formulaSheet) float64)"); // 数据内容 for (int rownum = 0; rownum <= sheet.LastRowNum; rownum++) { IRow row = sheet.GetRow(rownum); if (row == null) { continue; } for (int i = 0; i < row.Cells.Count; i++) { ICell cell = row.Cells[i]; int colnum = cell.ColumnIndex; // in、out声明忽略 if (colnum == 0 || colnum == 1 || colnum == 3 || colnum == 4) { continue; } if (cell.CellType == CellType.Boolean || cell.CellType == CellType.Numeric) { sb.AppendLine(sheetName + "FormaulaTemplate.datas[" + ((rownum + 1) * 1000 + colnum + 1) + "] = " + (cell.CellType == CellType.Boolean ? (cell.BooleanCellValue ? 1 : 0).ToString() : cell.NumericCellValue.ToString())); } else if (cell.CellType == CellType.Formula) { List <CellCoord> about; sb.AppendLine(sheetName + "FormaulaTemplate.funcs[" + ((rownum + 1) * 1000 + colnum + 1) + "] = func(ins *formulaSheet) float64 {"); sb.AppendLine("return " + Formula2Code.Translate(sheet, cell.CellFormula, cell.ToString(), out about)); sb.AppendLine("}"); CellCoord cur = new CellCoord(rownum + 1, colnum + 1); foreach (CellCoord cc in about) { if (!abouts.ContainsKey(cc)) { abouts.Add(cc, new List <CellCoord>()); } if (!abouts[cc].Contains(cur)) { abouts[cc].Add(cur); } } } } } // 数据影响关联递归统计 bool change; do { change = false; foreach (var item in abouts) { for (int i = 0; i < item.Value.Count; i++) { if (abouts.ContainsKey(item.Value[i])) { foreach (var c in abouts[item.Value[i]]) { if (!item.Value.Contains(c)) { item.Value.Add(c); change = true; } } } } } } while (change); // 数据影响关联 foreach (var item in abouts) { sb.AppendLine(sheetName + "FormaulaTemplate.relation[" + (item.Key.row * 1000 + item.Key.col) + "] = []int32{" + string.Join(",", item.Value.Select(c => { return(c.row * 1000 + c.col); })) + "}"); } sb.AppendLine("}"); // 创建 sb.AppendLine("func New" + SheetName + "Formula() *" + SheetName + "FormulaSheet {"); sb.AppendLine("formula:= new(" + SheetName + "FormulaSheet)"); sb.AppendLine("formula.template = " + sheetName + "FormaulaTemplate"); sb.AppendLine("formula.datas = make(map[int32]float64)"); sb.AppendLine("return formula"); sb.AppendLine("}"); // 声明 AppendGoDeclara(sheet, 0, true, sb); AppendGoDeclara(sheet, 3, false, sb); // 枚举器 foreach (var item in FormulaEnumerator.GetList(sheet)) { // 写结构 sb.AppendLine("type " + item.fullName + " struct {"); sb.AppendLine("sheet *" + SheetName + "FormulaSheet"); sb.AppendLine("line int32"); for (int i = 0; i < item.propertys.Count; i++) { sb.AppendLine(item.propertys[i] + " float64 // " + item.notes[i]); } sb.AppendLine("}"); // MoveNext sb.AppendLine("func (ins *" + item.fullName + ") MoveNext() bool {"); sb.AppendLine("if ins.line <= 0 {"); sb.AppendLine("ins.line = " + (item.start + 1) * 1000); sb.AppendLine("} else {"); sb.AppendLine("ins.line = ins.line + " + item.div * 1000); sb.AppendLine("}"); sb.AppendLine("if ins.line >= " + (item.end + 1) * 1000 + " {"); sb.AppendLine("return false"); sb.AppendLine("}"); sb.AppendLine(""); sb.AppendLine("if ins.sheet.get(ins.line+" + (6 + 1000 * (item.key - 1)) + ") == 0 {"); sb.AppendLine("return ins.MoveNext()"); sb.AppendLine("}"); sb.AppendLine(""); for (int i = 0; i < item.propertys.Count; i++) { sb.AppendLine("ins." + item.propertys[i] + " = ins.sheet.get(ins.line+" + (6 + 1000 * i) + ")"); } sb.AppendLine("return true"); sb.AppendLine("}"); sb.AppendLine(""); // GetEnumerator sb.AppendLine("func (ins *" + SheetName + "FormulaSheet) Get" + item.name + "Enumerator() *" + item.fullName + " {"); sb.AppendLine("enumerator := &" + item.fullName + "{}"); sb.AppendLine("enumerator.sheet = ins"); sb.AppendLine("return enumerator"); sb.AppendLine("}"); } // 结果 formulaContents.Add(SheetName, sb.ToString()); return(string.Empty); }
/// <summary> /// 处理lua表格的formula /// </summary> public static string DealWithFormulaSheetLua(ISheet sheet) { CodeTemplate.curlang = CodeTemplate.Langue.Lua; StringBuilder sb = new StringBuilder(); Dictionary <CellCoord, List <CellCoord> > abouts = new Dictionary <CellCoord, List <CellCoord> >(); // 开头 sb.AppendLine(CodeTemplate.Get("template_title") .Replace("[SHEET_NAME]", sheet.SheetName.Substring(3)) .Replace("[USEFUL_ROW_COUNT]", (sheet.LastRowNum + 1).ToString()) ); // 声明 sb.AppendLine("\n-- declares"); sb.AppendLine(CodeTemplate.Get("template_input").Replace("[CONTENT]", GetDeclaras(sheet, 0))); sb.AppendLine(CodeTemplate.Get("template_output").Replace("[CONTENT]", GetDeclaras(sheet, 3))); // 抓取所有数据 sb.AppendLine("\n-- all datas"); for (int rownum = 0; rownum <= sheet.LastRowNum; rownum++) { IRow row = sheet.GetRow(rownum); if (row == null) { continue; } for (int i = 0; i < row.Cells.Count; i++) { ICell cell = row.Cells[i]; int colnum = cell.ColumnIndex; // in、out声明忽略 if (colnum == 0 || colnum == 1 || colnum == 3 || colnum == 4) { continue; } if (cell.CellType == CellType.Boolean || cell.CellType == CellType.Numeric) { sb.AppendLine(CodeTemplate.Get("template_value") .Replace("[ROW]", (rownum + 1).ToString()) .Replace("[COL]", (colnum + 1).ToString()) .Replace("[VALUE]", cell.CellType == CellType.Boolean ? cell.BooleanCellValue.ToString().ToLower() : cell.NumericCellValue.ToString()) ); } else if (cell.CellType == CellType.Formula) { List <CellCoord> about; sb.AppendLine(CodeTemplate.Get("template_func") .Replace("[ROW]", (rownum + 1).ToString()) .Replace("[COL]", (colnum + 1).ToString()) .Replace("[CONTENT]", Formula2Code.Translate(sheet, cell.CellFormula, cell.ToString(), out about)) ); CellCoord cur = new CellCoord(rownum + 1, colnum + 1); foreach (CellCoord cc in about) { if (!abouts.ContainsKey(cc)) { abouts.Add(cc, new List <CellCoord>()); } if (!abouts[cc].Contains(cur)) { abouts[cc].Add(cur); } } } } } // 数据影响关联递归统计 bool change; do { change = false; foreach (var item in abouts) { for (int i = 0; i < item.Value.Count; i++) { if (abouts.ContainsKey(item.Value[i])) { foreach (var c in abouts[item.Value[i]]) { if (!item.Value.Contains(c)) { item.Value.Add(c); change = true; } } } } } } while (change); // 数据影响关联 sb.AppendLine("\n-- cell data relation"); foreach (var item in abouts) { sb.AppendLine(CodeTemplate.Get("template_about") .Replace("[ROW]", item.Key.row.ToString()) .Replace("[COL]", item.Key.col.ToString()) .Replace("[CONTENT]", string.Join(",", item.Value.Select(CellCoord.ToString))) ); } // 枚举器 sb.AppendLine("\n-- enumerator"); sb.AppendLine("sheet.enumerator = {}"); foreach (var item in FormulaEnumerator.GetList(sheet)) { sb.AppendLine("sheet.enumerator." + item.name + " = {}"); sb.AppendLine("sheet.enumerator." + item.name + ".start = " + (item.start + 1)); sb.AppendLine("sheet.enumerator." + item.name + ".over = " + (item.end + 1)); sb.AppendLine("sheet.enumerator." + item.name + ".div = " + item.div); sb.AppendLine("sheet.enumerator." + item.name + ".key = " + (item.key - 1)); sb.AppendLine("sheet.enumerator." + item.name + ".propertys = {"); for (int i = 0; i < item.propertys.Count; i++) { sb.AppendLine("\"" + item.propertys[i] + "\", --" + item.notes[i]); } sb.AppendLine("}"); } // 结果 formulaContents.Add(sheet.SheetName.Substring(3), sb.ToString()); return(string.Empty); }
public static string DealWithFormulaSheetTS(ISheet sheet) { CodeTemplate.curlang = CodeTemplate.Langue.TS; StringBuilder sb = new StringBuilder(); Dictionary <CellCoord, List <CellCoord> > abouts = new Dictionary <CellCoord, List <CellCoord> >(); string sheetName = sheet.SheetName.Substring(3); string SheetName = sheetName.Substring(0, 1).ToUpper() + sheetName.Substring(1); string className = SheetName + "FormulaSheet"; // module定义,让所有表都在一个模块里面 sb.Append("/// <reference path=\"ConfigBase.ts\" />\r\n"); sb.Append("namespace SuperConfig {\r\n"); sb.Append("\texport function " + " New" + className + "():" + className + "{\r\n"); sb.Append("\t\tvar formula = new " + className + "();\r\n"); sb.Append("\t\tformula.Init();\r\n"); sb.Append("\t\treturn formula;\r\n"); sb.Append("\t}\r\n"); //------ // 开始生成这个配置表的算法类 sb.Append("\texport class " + className + " extends FormulaSheet { //定义数据表类开始\r\n"); sb.Append("\t\tpublic Init(){\r\n"); // 数据内容 for (int rownum = 0; rownum <= sheet.LastRowNum; rownum++) { IRow row = sheet.GetRow(rownum); if (row == null) { continue; } for (int i = 0; i < row.Cells.Count; i++) { ICell cell = row.Cells[i]; int colnum = cell.ColumnIndex; // in、out声明忽略 if (colnum == 0 || colnum == 1 || colnum == 3 || colnum == 4) { continue; } if (cell.CellType == CellType.Boolean || cell.CellType == CellType.Numeric) { sb.Append("\t\t\tthis.datas.set(" + ((rownum + 1) * 1000 + colnum + 1) + "," + (cell.CellType == CellType.Boolean ? (cell.BooleanCellValue ? 1 : 0).ToString() : cell.NumericCellValue.ToString()) + ");\r\n"); } else if (cell.CellType == CellType.Formula) { List <CellCoord> about; sb.Append("\t\t\tthis.funcs.set(" + ((rownum + 1) * 1000 + colnum + 1) + " , (ins:any) => {\r\n"); string content = Formula2Code.Translate(sheet, cell.CellFormula, cell.ToString(), out about); if (content.IndexOf("vlookup") >= 0)//前面需要导入引用的类 { string[] strArr = content.Split('_'); string refSheetName = strArr[1]; string refName = refSheetName.Substring(0, 1).ToUpper() + refSheetName.Substring(1); sb.Append("\t\t\t\t var tab = Get" + refName + "Table()\r\n"); sb.Append("\t\t\t\t if(tab){\r\n"); content = content.Insert(1, "tab."); sb.Append(string.Format("\t\t\t\t\treturn {0};\r\n", content)); sb.Append("\t\t\t\t }\r\n"); //content = content.Insert(1, "Get" + refName + "Table()."); } else { sb.Append("\t\t\t\treturn " + content + ";\r\n"); } // if (CodeTemplate.curlang == CodeTemplate.Langue.CS) // { // content = FixFloat(content); // } sb.Append("\t\t\t});\r\n"); CellCoord cur = new CellCoord(rownum + 1, colnum + 1); foreach (CellCoord cc in about) { if (!abouts.ContainsKey(cc)) { abouts.Add(cc, new List <CellCoord>()); } if (!abouts[cc].Contains(cur)) { abouts[cc].Add(cur); } } } } } // 数据影响关联递归统计 bool change; do { change = false; foreach (var item in abouts) { for (int i = 0; i < item.Value.Count; i++) { if (abouts.ContainsKey(item.Value[i])) { foreach (var c in abouts[item.Value[i]]) { if (!item.Value.Contains(c)) { item.Value.Add(c); change = true; } } } } } } while (change); // 数据影响关联 foreach (var item in abouts) { sb.Append("\t\t\tthis.relation.set(" + (item.Key.row * 1000 + item.Key.col) + ", [" + string.Join(",", item.Value.Select(c => { return(c.row * 1000 + c.col); })) + "]);\r\n"); } sb.Append("\t\t} // 初始化数据结束\r\n\r"); // 声明 AppendTSDeclara(sheet, 0, true, sb); AppendTSDeclara(sheet, 3, false, sb); // 枚举器 // sb.Append("\r// 枚举器\r"); // foreach (var item in FormulaEnumerator.GetList(sheet)) // { // // 写结构 // sb.Append("public struct " + item.fullName + " {\r\n"); // // 属性 // sb.Append("\tpublic " + className + " sheet;\r\n"); // sb.Append("\t int line;\r\n"); // for (int i = 0; i < item.propertys.Count; i++) // sb.Append("\tfloat " + item.propertys[i] + "; // " + item.notes[i] + "\r\n"); // // 枚举方法 // sb.Append("public bool MoveNext() {\r\n"); // // MoveNext // sb.Append("\tif (line <= 0) {\r\n"); // sb.Append("\t\tline = " + (item.start + 1) * 1000 + ";\r\n"); // sb.Append("\t} else {\r\n"); // sb.Append("\t\tline = line + " + item.div * 1000 + ";\r\n"); // sb.Append("}\r\n"); // sb.Append("\tif (line >= " + (item.end + 1) * 1000 + ") {\r\n"); // sb.Append("\t\treturn false;\r\n"); // sb.Append("}\r\n"); // sb.Append("\tif (sheet.get(line+" + (6 + 1000 * (item.key - 1)) + ") == 0 ) {\r\n"); // sb.Append("\t\treturn MoveNext();\r\n"); // sb.Append("}\r\n"); // for (int i = 0; i < item.propertys.Count; i++) // sb.Append("" + item.propertys[i] + " = sheet.get(line+" + (6 + 1000 * i) + ");\r\n"); // sb.Append("\treturn true;\r\n"); // sb.Append("} // 枚举方法next结束\r\n"); // sb.Append("} // 枚举struct定义结束\r\n"); // // GetEnumerator // sb.Append("public " + item.fullName + " Get" + item.name + "Enumerator(){\r\n"); // sb.Append("\tvar enumerator = new " + item.fullName + "();\r\n"); // sb.Append("\t\tenumerator.sheet = this;\r\n"); // sb.Append("\t\treturn enumerator;\r\n"); // sb.Append("}\r\n"); // } sb.Append("\t}\r\n"); // module结束符 sb.Append("}\r\n"); // 结果 formulaContents.Add(SheetName, sb.ToString()); return(string.Empty); }
public static string DealWithFormulaSheetCS(ISheet sheet) { CodeTemplate.curlang = CodeTemplate.Langue.CS; StringBuilder sb = new StringBuilder(); Dictionary <CellCoord, List <CellCoord> > abouts = new Dictionary <CellCoord, List <CellCoord> >(); string sheetName = sheet.SheetName.Substring(3); string SheetName = sheetName.Substring(0, 1).ToUpper() + sheetName.Substring(1); string className = SheetName + "FormulaSheet"; sb.Append("using System;\r\n"); sb.Append("using UnityEngine;\r\n"); sb.Append("using System.Collections;\r\n"); sb.Append("using System.Collections.Generic;\r\n"); sb.Append("\r\n"); // 扩展Config类统一获取某个表的实例对象 sb.Append("public partial class Config {\r\n"); sb.Append("\tpublic static " + className + " New" + className + "(){\r\n"); sb.Append("\t\tvar formula = new " + className + "();\r\n"); sb.Append("\t\tformula.Init();\r\n"); sb.Append("\t\treturn formula;\r\n"); sb.Append("\t}\r\n"); sb.Append("}\r\n"); //------ // 开始生成这个配置表的算法类 sb.Append("public class " + className + " : FormulaSheet { //定义数据表类开始\r\n"); sb.Append("public void Init(){\r\n"); // 数据内容 for (int rownum = 0; rownum <= sheet.LastRowNum; rownum++) { IRow row = sheet.GetRow(rownum); if (row == null) { continue; } for (int i = 0; i < row.Cells.Count; i++) { ICell cell = row.Cells[i]; int colnum = cell.ColumnIndex; // in、out声明忽略 if (colnum == 0 || colnum == 1 || colnum == 3 || colnum == 4) { continue; } if (cell.CellType == CellType.Boolean || cell.CellType == CellType.Numeric) { sb.Append("this.datas[" + ((rownum + 1) * 1000 + colnum + 1) + "] = " + (cell.CellType == CellType.Boolean ? (cell.BooleanCellValue ? 1 : 0).ToString() : cell.NumericCellValue.ToString()) + "f;\r\n"); } else if (cell.CellType == CellType.Formula) { List <CellCoord> about; sb.Append("this.funcs[" + ((rownum + 1) * 1000 + colnum + 1) + "] = ins => {\r\n"); string content = Formula2Code.Translate(sheet, cell.CellFormula, cell.ToString(), out about); if (CodeTemplate.curlang == CodeTemplate.Langue.CS) { content = FixFloat(content); } sb.Append("\treturn (float)" + content + ";\r\n"); sb.Append("};\r\n"); CellCoord cur = new CellCoord(rownum + 1, colnum + 1); foreach (CellCoord cc in about) { if (!abouts.ContainsKey(cc)) { abouts.Add(cc, new List <CellCoord>()); } if (!abouts[cc].Contains(cur)) { abouts[cc].Add(cur); } } } } } // 数据影响关联递归统计 bool change; do { change = false; foreach (var item in abouts) { for (int i = 0; i < item.Value.Count; i++) { if (abouts.ContainsKey(item.Value[i])) { foreach (var c in abouts[item.Value[i]]) { if (!item.Value.Contains(c)) { item.Value.Add(c); change = true; } } } } } } while (change); // 数据影响关联 foreach (var item in abouts) { sb.Append("this.relation[" + (item.Key.row * 1000 + item.Key.col) + "] = new int[]{" + string.Join(",", item.Value.Select(c => { return(c.row * 1000 + c.col); })) + "};\r\n"); } sb.Append("} // 初始化数据结束\r\n"); // 声明 AppendCSDeclara(sheet, 0, true, sb); AppendCSDeclara(sheet, 3, false, sb); // 枚举器 foreach (var item in FormulaEnumerator.GetList(sheet)) { // 写结构 sb.Append("public struct " + item.fullName + " {\r\n"); // 属性 sb.Append("\tpublic " + className + " sheet;\r\n"); sb.Append("\t int line;\r\n"); for (int i = 0; i < item.propertys.Count; i++) { sb.Append("\tfloat " + item.propertys[i] + "; // " + item.notes[i] + "\r\n"); } // 枚举方法 sb.Append("public bool MoveNext() {\r\n"); // MoveNext sb.Append("\tif (line <= 0) {\r\n"); sb.Append("\t\tline = " + (item.start + 1) * 1000 + ";\r\n"); sb.Append("\t} else {\r\n"); sb.Append("\t\tline = line + " + item.div * 1000 + ";\r\n"); sb.Append("}\r\n"); sb.Append("\tif (line >= " + (item.end + 1) * 1000 + ") {\r\n"); sb.Append("\t\treturn false;\r\n"); sb.Append("}\r\n"); sb.Append("\tif (sheet.get(line+" + (6 + 1000 * (item.key - 1)) + ") == 0 ) {\r\n"); sb.Append("\t\treturn MoveNext();\r\n"); sb.Append("}\r\n"); for (int i = 0; i < item.propertys.Count; i++) { sb.Append("" + item.propertys[i] + " = sheet.get(line+" + (6 + 1000 * i) + ");\r\n"); } sb.Append("\treturn true;\r\n"); sb.Append("} // 枚举方法next结束\r\n"); sb.Append("} // 枚举struct定义结束\r\n"); // GetEnumerator sb.Append("public " + item.fullName + " Get" + item.name + "Enumerator(){\r\n"); sb.Append("\tvar enumerator = new " + item.fullName + "();\r\n"); sb.Append("\t\tenumerator.sheet = this;\r\n"); sb.Append("\t\treturn enumerator;\r\n"); sb.Append("}\r\n"); } sb.Append("}\r\n"); // 结果 formulaContents.Add(SheetName, sb.ToString()); return(string.Empty); }