Elin Decompiled Documentation EA 23.329 Nightly
Loading...
Searching...
No Matches
ExcelParser.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using NPOI.SS.UserModel;
6using UnityEngine;
7
8public class ExcelParser
9{
10 public static string path;
11
12 public static IRow row;
13
14 public static IRow rowDefault;
15
16 public static IRow rowHeader;
17
18 public static bool allowTrimming;
19
20 public static bool IsNull(ICell cell)
21 {
22 if (cell != null && cell.CellType != CellType.Blank)
23 {
24 return cell.CellType == CellType.Unknown;
25 }
26 return true;
27 }
28
29 public static int GetInt(int id)
30 {
31 string str = GetStr(id);
32 if (str.IsEmpty())
33 {
34 return 0;
35 }
36 if (!int.TryParse(str, out var result))
37 {
38 if (float.TryParse(str, out var result2))
39 {
40 return (int)result2;
41 }
42 ReportIllFormat<int>(id);
43 }
44 return result;
45 }
46
47 public static int GetInt(int col, IRow _row)
48 {
49 row = _row;
50 return GetInt(col);
51 }
52
53 public static bool GetBool(int id)
54 {
55 string str = GetStr(id);
56 bool result;
57 return str switch
58 {
59 null => false,
60 "1" => true,
61 "0" => false,
62 _ => bool.TryParse(str, out result) && result,
63 };
64 }
65
66 public static bool GetBool(int col, IRow _row)
67 {
68 row = _row;
69 return GetBool(col);
70 }
71
72 public static double GetDouble(int id)
73 {
74 string str = GetStr(id);
75 if (str.IsEmpty())
76 {
77 return 0.0;
78 }
79 if (!double.TryParse(str, out var result))
80 {
81 ReportIllFormat<double>(id);
82 }
83 return result;
84 }
85
86 public static float GetFloat(int id)
87 {
88 string str = GetStr(id);
89 if (str.IsEmpty())
90 {
91 return 0f;
92 }
93 if (!float.TryParse(str, out var result))
94 {
95 ReportIllFormat<float>(id);
96 }
97 return result;
98 }
99
100 public static float[] GetFloatArray(int id)
101 {
102 string str = GetStr(id);
103 if (str.IsEmpty())
104 {
105 return Array.Empty<float>();
106 }
107 string[] array = str.Split(',');
108 float[] array2 = new float[array.Length];
109 for (int i = 0; i < array.Length; i++)
110 {
111 if (!float.TryParse(array[i], out array2[i]))
112 {
113 ReportIllFormat<float>(id);
114 array2[i] = 0f;
115 }
116 }
117 return array2;
118 }
119
120 public static int[] GetIntArray(int id)
121 {
122 string str = GetStr(id);
123 if (str.IsEmpty())
124 {
125 return Array.Empty<int>();
126 }
127 string[] array = str.Split(',');
128 int[] array2 = new int[array.Length];
129 for (int i = 0; i < array.Length; i++)
130 {
131 if (!int.TryParse(array[i], out array2[i]))
132 {
133 if (float.TryParse(array[i], out var result))
134 {
135 array2[i] = (int)result;
136 continue;
137 }
138 ReportIllFormat<int>(id);
139 array2[i] = 0;
140 }
141 }
142 return array2;
143 }
144
145 public static string[] GetStringArray(int id)
146 {
147 string str = GetStr(id);
148 if (str != null)
149 {
150 if (!allowTrimming)
151 {
152 return str.Split(',');
153 }
154 return (from c in str.Split(',')
155 select c.Trim()).ToArray();
156 }
157 return Array.Empty<string>();
158 }
159
160 public static string GetString(int id)
161 {
162 return GetStr(id) ?? "";
163 }
164
165 public static string GetString(int col, IRow _row)
166 {
167 row = _row;
168 return GetStr(col);
169 }
170
171 public static string GetStr(int id, bool useDefault = false)
172 {
173 IRow row = (useDefault ? rowDefault : ExcelParser.row);
174 if (row == null)
175 {
176 if (!useDefault)
177 {
178 return GetStr(id, useDefault: true);
179 }
180 return null;
181 }
182 ICell cell = row.GetCell(id);
183 if (IsNull(cell))
184 {
185 if (!useDefault)
186 {
187 return GetStr(id, useDefault: true);
188 }
189 return null;
190 }
191 cell.SetCellType(CellType.String);
192 if (cell.StringCellValue == "")
193 {
194 if (!useDefault)
195 {
196 return GetStr(id, useDefault: true);
197 }
198 return null;
199 }
200 if (!allowTrimming)
201 {
202 return cell.StringCellValue;
203 }
204 return cell.StringCellValue.Trim();
205 }
206
207 public static string ToLetterId(int id)
208 {
209 string text = "";
210 while (id >= 0)
211 {
212 text = (char)(id % 26 + 65) + text;
213 id /= 26;
214 id--;
215 }
216 return text;
217 }
218
219 public static void ReportIllFormat<T>(int id)
220 {
221 StringBuilder stringBuilder = new StringBuilder();
222 string name = typeof(T).Name;
223 ICell cell = row?.Cells.TryGet(id, returnNull: true);
224 IRow obj = row;
225 string value = ((obj != null && obj.RowNum >= 3) ? $", default:'{rowDefault?.Cells.TryGet(id, returnNull: true)}'" : ", SourceData begins at the 4th row. 3rd row is the default value row.");
226 stringBuilder.AppendLine("#source ill-format file: " + path);
227 object[] array = new object[5];
228 IRow obj2 = row;
229 array[0] = ((obj2 != null) ? new int?(obj2.RowNum + 1) : null);
230 array[1] = id + 1;
231 array[2] = ToLetterId(id);
232 array[3] = name;
233 array[4] = cell;
234 stringBuilder.Append(string.Format("row#{0}, cell#{1}/#{2}, expected:'{3}', read:'{4}'", array));
235 stringBuilder.AppendLine(value);
236 Debug.LogWarning(stringBuilder);
237 }
238
239 public static string GetRowHeaderDiff(IReadOnlyDictionary<string, int> mapping, IReadOnlyDictionary<string, int> header)
240 {
241 StringBuilder stringBuilder = new StringBuilder();
242 List<KeyValuePair<string, int>> list = mapping.OrderBy((KeyValuePair<string, int> c) => c.Value).ToList();
243 int num = list.Max((KeyValuePair<string, int> c) => c.Key.Length);
244 foreach (KeyValuePair<string, int> item in list)
245 {
246 item.Deconstruct(out var key, out var value);
247 string text = key;
248 int index = value;
249 int value2;
250 bool num2 = header.TryGetValue(text, out value2);
251 string text2 = ((num2 && value2 != index) ? $"maybe {value2 + 1,2}/{ToLetterId(value2)} {text}" : "");
252 if (!num2)
253 {
254 text2 = "<missing>";
255 }
256 string text3 = header.FirstOrDefault((KeyValuePair<string, int> c) => c.Value == index).Key ?? "<missing>";
257 text3 = text3.PadRight(num + 3);
258 string text4 = text.PadRight(num);
259 stringBuilder.AppendLine($"{index + 1,2}/{ToLetterId(index),2}: {text4} -> {text3} {text2}");
260 }
261 return stringBuilder.ToString();
262 }
263
264 public static void ClearStaticRows()
265 {
266 row = null;
267 rowDefault = null;
268 rowHeader = null;
269 allowTrimming = false;
270 }
271}
$
Definition: ExcelParser.cs:239
static void ClearStaticRows()
Definition: ExcelParser.cs:264
static bool GetBool(int id)
Definition: ExcelParser.cs:53
static void ReportIllFormat< T >(int id)
Definition: ExcelParser.cs:219
static string GetRowHeaderDiff(IReadOnlyDictionary< string, int > mapping, IReadOnlyDictionary< string, int > header)
Definition: ExcelParser.cs:239
static string ToLetterId(int id)
Definition: ExcelParser.cs:207
static IRow rowDefault
Definition: ExcelParser.cs:14
static bool GetBool(int col, IRow _row)
Definition: ExcelParser.cs:66
static int GetInt(int id)
Definition: ExcelParser.cs:29
static IRow row
Definition: ExcelParser.cs:12
static bool IsNull(ICell cell)
Definition: ExcelParser.cs:20
static string GetString(int id)
Definition: ExcelParser.cs:160
static int[] GetIntArray(int id)
Definition: ExcelParser.cs:120
static int GetInt(int col, IRow _row)
Definition: ExcelParser.cs:47
static float GetFloat(int id)
Definition: ExcelParser.cs:86
static string path
Definition: ExcelParser.cs:10
static double GetDouble(int id)
Definition: ExcelParser.cs:72
static string GetStr(int id, bool useDefault=false)
Definition: ExcelParser.cs:171
static bool allowTrimming
Definition: ExcelParser.cs:18
static float[] GetFloatArray(int id)
Definition: ExcelParser.cs:100
static IRow rowHeader
Definition: ExcelParser.cs:16
static string GetString(int col, IRow _row)
Definition: ExcelParser.cs:165
static string[] GetStringArray(int id)
Definition: ExcelParser.cs:145