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); }
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); }