Example #1
0
 private void EnsureSheetHaveColumns(DataSheet sheet, IEnumerable<string> columnNames)
 {
     var newNames = columnNames.Except(sheet.Rows[0].Keys);
     foreach (var name in newNames)
     {
         foreach (var row in sheet.Rows) row.Add(name, string.Empty);
     }
 }
Example #2
0
 private void CheckSheetConsistence(DataSheet sheet)
 {
     var columnNames = new HashSet<string>(sheet.Rows[0].Keys);
     foreach (var row in sheet.Rows)
     {
         if (!columnNames.SetEquals(row.Keys))
         {
             throw new Exception(
                 string.Format("{0} columns mismatch: ({1}),({2})",
                 sheet.Name, JsonOperation.Serialize(columnNames), JsonOperation.Serialize(row)));
         }
     }
 }
Example #3
0
        public DataSheet Read(string name)
        {
            var sheet = mWorkbook.GetSheet(name);

            var dsheet = new DataSheet() { Name = sheet.SheetName };
            var titles = (from cell in sheet.GetRow(mTitleRow).Cells
                          where !string.IsNullOrEmpty(cell.StringCellValue)
                          select new KeyValuePair<int, string>(cell.ColumnIndex, cell.StringCellValue)).ToArray();
            for (int i = mContentRow, last = sheet.LastRowNum; i <= last; ++i)
            {
                var row = sheet.GetRow(i);
                if (row != null) dsheet.Rows.Add(ReadRow(row, titles));
            }
            return dsheet;
        }
Example #4
0
        private void MergeSheet(DataSheet destSheet, DataSheet srcSheet, string primaryKey)
        {
            CheckSheetConsistence(destSheet);
            CheckSheetConsistence(srcSheet);

            EnsureSheetHaveColumns(destSheet, srcSheet.Rows[0].Keys);

            var id2DestRow = new Dictionary<object, Dictionary<string, object>>();
            foreach (var destRow in destSheet.Rows) id2DestRow[destRow[primaryKey]] = destRow;

            foreach (var srcRow in srcSheet.Rows)
            {
                var id = srcRow[primaryKey];
                if (!id2DestRow.ContainsKey(id))
                {
                    throw new Exception(string.Format("Can't find primary key in dest sheet: {0}", id));
                }

                Dictionary<string, object> destRow = id2DestRow[srcRow[primaryKey]];
                foreach (var kv in srcRow)
                {
                    destRow[kv.Key] = kv.Value;
                }
            }
        }
Example #5
0
        public override void Execute()
        {
            Log.Info("Start to replace string literals ...");

            Log.Info("Read filter from: {0}", FilterPath);

            string className = Path.GetFileNameWithoutExtension(ResultPath);
            var filter = new HashSet<string>(File.ReadAllLines(FilterPath, Encoding.UTF8));
            var str2Field = new Dictionary<string, string>(1024);

            foreach (var path in GetCSharpFiles(CSharpSrcDirectories))
            {
                Log.Info("\tProcessing file: {0}", path);

                var encoding = Utils.DetectFileEncoding(path);
                var oldText = File.ReadAllText(path, encoding);
                var newText = NRefactoryTool.CodeTransform.ReplaceStringLiteralsWithVariables(oldText, Macros,
                (line, column, lineText, s) =>
                {
                    if (!filter.Contains(string.Format("{0}({1},{2}):{3}", path, line, column, NRefactoryTool.Utils.EscapeString(lineText)))) return null;

                    string field;
                    if (str2Field.TryGetValue(s, out field)) return field;

                    field = string.Format("{0}.kString_{1}", className, str2Field.Count);
                    str2Field.Add(s, field);
                    return field;
                });

                if (oldText != newText)
                {
                    Log.Info("\tPerform replacement!");
                    File.WriteAllText(path, newText, encoding);
                }
            }

            Log.Info("Write sheet to: {0}", ResultPath);

            var sheet = new DataSheet
            {
                Name = className,
                Rows = (from kv in str2Field
                        let fieldName = kv.Value.Split('.').Last()
                        orderby fieldName
                        select new Dictionary<string, object>(){
                              {kNameKey, fieldName },
                              {kDefaultLangKey, kv.Key}
                          }).ToList(),
            };
            File.WriteAllText(ResultPath, JsonOperation.SerializeSheets(new[] { sheet }), Encoding.UTF8);

            Log.Info("Done.");
        }
Example #6
0
        private ISheet CreateSheet(DataSheet dsheet)
        {
            var sheet = mWorkbook.CreateSheet(dsheet.Name);
            for (var i = 0; i < mContentRow; ++i) sheet.CreateRow(i);

            var columnNames = dsheet.Rows[0].Keys.OrderBy(k => k).ToArray();
            var titleRow = sheet.GetRow(mTitleRow);
            for (var i = 0; i < columnNames.Length; ++i)
            {
                titleRow.CreateCell(i).SetCellValue(columnNames[i]);
            }

            return sheet;
        }
Example #7
0
        public void Write(DataSheet dsheet)
        {
            if (dsheet.Rows.Count == 0) return;

            var sheet = mWorkbook.GetSheet(dsheet.Name);
            if (sheet == null) sheet = CreateSheet(dsheet);

            var titleRow = sheet.GetRow(mTitleRow);
            foreach (var newTitle in dsheet.Rows[0].Keys.Except(titleRow.Cells.Select(c => c.StringCellValue)))
            {
                titleRow.CreateCell(titleRow.LastCellNum).SetCellValue(newTitle);
            }

            var titles = (from cell in titleRow.Cells
                          where !string.IsNullOrEmpty(cell.StringCellValue)
                          select new KeyValuePair<int, string>(cell.ColumnIndex, cell.StringCellValue)).ToArray();
            for (int i = mContentRow, end = mContentRow + dsheet.Rows.Count; i < end; ++i)
            {
                var row = sheet.GetRow(i);
                if (row == null) row = sheet.CreateRow(i);
                WriteRow(row, dsheet.Rows[i - mContentRow], titles);
            }
            for (int i = mContentRow + dsheet.Rows.Count, last = sheet.LastRowNum; i <= last; ++i)
            {
                var row = sheet.GetRow(i);
                if (row != null) sheet.RemoveRow(row);
            }
        }