public static SheetCtx New(Metadata meta, Dictionary <string, object> headerValues, AsyncExprCtx parent) { var sc = meta.ctxHdr; var values = new object[sc.values.Count]; sc.values.CopyTo(values, 0); var k2n = sc.name2ndx; values[k2n[HdrCellsRef.name]] = headerValues; { var prevRowVals = new OPs.ListOfConst(); for (int i = meta.ctxRow.values.Count; i > 0; i--) { prevRowVals.Add(null); } // imitate empty zero row prevRowVals[iRowVals] = new Dictionary <string, object>(); values[k2n[PrevValsRef.name]] = prevRowVals; } // template metadata info context var aec = new AsyncExprCtx(sc.parent, sc.parent.values, parent); // var ctx = new SheetCtx(meta, values, aec); return(ctx.Condition ? ctx : null); }
static async Task <object> LoadCodeLookupDictRows(AsyncExprCtx ae, IList args) { var conn = (IDbConn)await OPs.ConstValueOf(ae, args[0]); var table = Convert.ToString(await OPs.ConstValueOf(ae, args[1])).Replace(' ', '_').Replace(',', '_'); var query = $"SELECT {nameof(LookupEntry.Code)}, {nameof(LookupEntry.Description)}, {nameof(LookupEntry.ReplacedWith_Code)} FROM {table}"; var cmd = new SqlCommandData() { Kind = CommandKind.Query, SqlText = query, ConvertMultiResultsToLists = false }; var rows = (IIndexedDict[])await conn.ExecCmd(cmd, ae.Cancellation); var dict = new Dictionary <string, LookupEntry>(rows.Length); foreach (var r in rows) { var entry = new LookupEntry() { Code = r.ValuesList[0].ToString(), Description = Convert.ToString(r.ValuesList[1]), ReplacedWith_Code = Convert.ToString(r.ValuesList[2]), }; dict.Add(entry.Code, entry); } // incapsulate into tuple to mask IList interface return(Tuple.Create(dict)); }
public static async Task <object> _ReportRow(AsyncExprCtx ae, IList args) { // get/calculate values var lstVals = (IList)await OPs.VectorValueOf(ae, args[3]); int n = lstVals.Count; var vals = new object[n]; for (int i = n - 1; i >= 0; i--) { try { vals[i] = await OPs.ConstValueOf(ae, lstVals[i]); } catch (TaskCanceledException) { throw; } }
public SheetCtx TryGetSheetCtx(AsyncExprCtx parent, Dictionary <string, object> headerValues, int nHeaderRows) { if (nHeaderRows == iHdrMaxUsedRow) { return(SheetCtx.New(this, headerValues, parent)); } if (nHeaderRows > iHdrMaxUsedRow) { return(SheetCtx.Empty); } else { return(null); } }
public static async Task <object> WRptGetXmlWriter(AsyncExprCtx ae, IList args) { var outputFiles = (IWriteFiles)await OPs.ConstValueOf(ae, args[0]); var parts = new string[args.Count - 1]; for (int i = args.Count - 1; i >= 1; i--) { parts[i - 1] = Convert.ToString(await OPs.ConstValueOf(ae, args[i])); } var stream = await outputFiles.CreateFile(Path.Combine(parts), ae.Cancellation); var writer = new XmlTextWriter(stream, Encoding.UTF8); return(writer); }
public async Task <IReadOnlyDictionary <string, object> > NextRowData(Dictionary <string, object> rowValues, bool forRes = false) { var rc = forRes ? meta.ctxRes : meta.ctxRow; var values = new object[rc.values.Count]; rc.values.CopyTo(values, 0); var k2n = rc.name2ndx; values[k2n[CellValsRef.name]] = rowValues; var rowCtx = new AsyncExprCtx(rc, values, this); var prevVals = (OPs.ListOfConst) await this.GetValue(PrevValsRef.name); int nValsInCtx = values.Length; values[iKey] = await rowCtx.GetValue(iKey); values[iSubkey] = await rowCtx.GetValue(iSubkey); values[iRowVals] = rowValues; for (int i = iRowVals + 1; i < values.Length; i++) { values[i] = await rowCtx.GetValue(i); } for (int i = 0; i < values.Length; i++) { prevVals[i] = values[i]; } if (values[iKey] == null) { // skip row if error or empty string datakey return(null); } return(ValuesDictionary.New(values, k2n)); }
public static async Task <object> _ReportSheet(AsyncExprCtx ae, IList args) { using (var wr = (XmlWriter)await OPs.ConstValueOf(ae, args[0])) { var xmls = (IEnumerable <Xlsx.XmlItem>) await OPs.ConstValueOf(ae, args[1]); wr.WriteStartDocument(); object result = string.Empty; foreach (var xml in xmls) { xml.WriteStartElementAndAttrs(wr); var xmlSheetData = new Xlsx.XmlItem(); { // write xml before "sheetData" bool stop = false; xml.Traverse(x => { if (stop) { return; } if (x.Name != "sheetData") { if (x.Name == "dimension") { return; } x.WritePart(wr); return; } x.WriteStartElementAndAttrs(wr); xmlSheetData = x; stop = true; }); } if (xmlSheetData.IsEmpty) { continue; // no sheet data } // write sheet data (rows) for (int i = 2; i < args.Count; i++) { result = await OPs.ConstValueOf(ae, args[i]); //Console.WriteLine(res); // debug } { // write xml after "sheetData" bool skip = true; xml.Traverse(x => { if (!skip) { x.WritePart(wr); return; } else if (x.Name == "sheetData") { skip = false; wr.WriteEndElement(); } }); } wr.WriteEndElement(); } wr.WriteEndDocument(); return(result); } }
LoadSourceData(string fileName, AsyncExprCtx aecParent, Action <string> msgReceiver = null) { ITabbedFileReader rdr; var ext = Path.GetExtension(fileName).ToLowerInvariant(); if (!dictExt2Kind.TryGetValue(ext, out var kind)) { throw new Exception($"{nameof(LoadSourceData)}: Filename extension '{ext}' is not suitable for any source template // {fileName}"); } switch (kind) { //case "XLSX": // rdr = new Xlsx.Reader(fileName); // break; case "XLSX": case "XLS": rdr = new ExcelDataReader(fileName); break; case "TEXT": rdr = new TxtReader(new StreamReader(fileName), fileName); break; default: throw new Exception($"{nameof(LoadSourceData)}: No reader implemented for content kind '{kind}'"); } using (rdr) { var values = new object[ctxFile.values.Count]; ctxFile.values.CopyTo(values, 0); values[ctxFile.name2ndx[sFileDir]] = Path.GetDirectoryName(fileName); values[ctxFile.name2ndx[sFileName]] = Path.GetFileName(fileName); int MaxUsedHeaderRows = metas.Max(m => m.MaxUsedHeaderRows); foreach (var si in rdr.EnumerateTabs()) { var headingValues = new Dictionary <string, object>(); values[ctxFile.name2ndx[sSheetName]] = si.SheetName; var aecSheet = new AsyncExprCtx(ctxFile, values, aecParent); List <SheetCtx> infos = null; int prevRowNum = 0; foreach (var row in si.EnumerateRows()) { int iRowNum = row.num; if (infos == null) { if (iRowNum > MaxUsedHeaderRows) { break; } foreach (var c in row.cells) { headingValues.Add(c.name, c.value); } infos = metas .Select(m => m.TryGetSheetCtx(aecSheet, headingValues, iRowNum)) .ToList(); bool withNonEmpty = false; bool withEmpty = false; for (int i = infos.Count - 1; i >= 0; i--) { var sc = infos[i]; if (sc == SheetCtx.Empty) { withEmpty = true; infos.RemoveAt(i); } else if (sc == null) { withNonEmpty = true; infos.RemoveAt(i); } else { withNonEmpty = true; } } if (withEmpty && !withNonEmpty) { break; } if (infos.Count == 0) { infos = null; } if (infos == null) { // appropriate metadata not found prevRowNum = iRowNum; continue; } else { msgReceiver?.Invoke(string.Join(", ", infos.Select(i => i.SourceName))); } } var dictRowValues = new Dictionary <string, object>(row.cells.Length); // process missed empty rows if needed while (++prevRowNum < iRowNum) { foreach (var info in infos) { if (prevRowNum < info.FirstDataRowNum) { continue; } var data = info.NextRowData(dictRowValues).Result; if (data == null) { continue; } yield return(info.SourceKey, data); } } // fill in dictionary with row data foreach (var c in row.cells) { int i = 0; var s = c.name; for (; i < s.Length && char.IsLetter(s[i]); i++) { ; } var colName = s.Substring(0, i); dictRowValues.Add(colName, c.value); } // process row foreach (var info in infos) { if (iRowNum < info.FirstDataRowNum) { continue; } var data = info.NextRowData(dictRowValues).Result; if (data == null) { continue; } yield return(info.SourceKey, data); } } // process res, if needed if (infos != null) { var dictRowValues = new Dictionary <string, object>(0); foreach (var info in infos) { if (info.meta.ctxRes == null) { continue; } var data = info.NextRowData(dictRowValues, true).Result; if (data == null) { continue; } yield return(info.SourceKey, data); } } } } }
SheetCtx(Metadata meta, IList values, AsyncExprCtx parent) : base(meta.ctxHdr, values, parent) { this.meta = meta; SourceKey = Convert.ToString(GetValue(sSourceKey).Result); }