Exemple #1
0
        internal RangeTemplate(string name, IXLRange range, TempSheetBuffer buff, TemplateErrors errors, IDictionary <string, object> globalVariables)
        {
            _rowRange      = range;
            _cells         = new TemplateCells(this);
            _tagsEvaluator = new TagsEvaluator();
            var wb = _rowRange.Worksheet.Workbook;

            _buff            = buff;
            _errors          = errors;
            _globalVariables = globalVariables;
            _tags            = new TagsList(_errors);
            _rangeTags       = new TagsList(_errors);
            Name             = name;
            Source           = name;
            var rangeName = name + "_tpl";

            if (wb.NamedRanges.TryGetValue(rangeName, out var namedRange))
            {
                namedRange.Add(range);
            }
            else
            {
                wb.NamedRanges.Add(rangeName, range);
            }

            _evaluator = new FormulaEvaluator();
        }
Exemple #2
0
        internal RangeTemplate(IXLNamedRange range, TempSheetBuffer buff)
        {
            _rowRange      = range.Ranges.First();
            _cells         = new TemplateCells(this);
            _tagsEvaluator = new TagsEvaluator();
            var wb = _rowRange.Worksheet.Workbook;

            _buff      = buff;
            _tags      = new TagsList();
            _rangeTags = new TagsList();
            Name       = range.Name;
            Source     = range.Name;
            wb.NamedRanges.Add(range.Name + "_tpl", range.Ranges);
        }
Exemple #3
0
        internal RangeTemplate(IXLNamedRange range, TempSheetBuffer buff, TemplateErrors errors, IDictionary <string, object> globalVariables)
        {
            _rowRange      = range.Ranges.First();
            _cells         = new TemplateCells(this);
            _tagsEvaluator = new TagsEvaluator();
            var wb = _rowRange.Worksheet.Workbook;

            _buff            = buff;
            _errors          = errors;
            _globalVariables = globalVariables;
            _tags            = new TagsList(_errors);
            _rangeTags       = new TagsList(_errors);
            Name             = range.Name;
            Source           = range.Name;
            wb.NamedRanges.Add(range.Name + "_tpl", range.Ranges);
        }
        public void NamedRangesAreRemovedWithTempSheet()
        {
            using (var wb = new XLWorkbook())
            {
                var ws = wb.AddWorksheet("Sheet1");

                var tempSheetBuffer = new TempSheetBuffer(wb);
                wb.NamedRanges.Add("Temp range", tempSheetBuffer.GetRange(
                                       tempSheetBuffer.GetCell(1, 1).Address,
                                       tempSheetBuffer.GetCell(4, 4).Address));

                wb.NamedRanges.Count().Should().Be(1, "global named range is supposed to be added");
                tempSheetBuffer.Dispose();
                wb.NamedRanges.Count().Should().Be(0, "named range should be deleted with the temp buffer");
            }
        }
Exemple #5
0
        private static RangeTemplate Parse(IXLNamedRange range, TempSheetBuffer buff, TemplateErrors errors, IDictionary <string, object> globalVariables)
        {
            var prng   = range.Ranges.First();
            var result = new RangeTemplate(range, buff,
                                           prng.RowCount(), prng.ColumnCount(), errors, globalVariables);

            var innerRanges = GetInnerRanges(prng).ToArray();

            var sheet = prng.Worksheet;

            for (int iRow = 1; iRow <= result._rowCnt; iRow++)
            {
                for (int iColumn = 1; iColumn <= result._colCnt; iColumn++)
                {
                    var xlCell = prng.Cell(iRow, iColumn);
                    if (innerRanges.Any(x => x.Ranges.Cells().Contains(xlCell)))
                    {
                        xlCell = null;
                    }
                    result._cells.Add(iRow, iColumn, xlCell);
                }
                if (iRow != result._rowCnt)
                {
                    result._cells.AddNewRow();
                }
            }

            result._mergedRanges = sheet.MergedRanges.Where(x => prng.Contains(x) && !innerRanges.Any(nr => nr.Ranges.Any(r => r.Contains(x)))).ToArray();
            sheet.MergedRanges.RemoveAll(result._mergedRanges.Contains);
            result._condFormats = sheet.ConditionalFormats
                                  .Where(f => prng.Contains(f.Range) && !innerRanges.Any(ir => ir.Ranges.Contains(f.Range)))
                                  .ToArray();
            if (result._rowCnt > 1)
            {
                // Exclude special row
                result._rowCnt--;

                result._rowRange          = prng.Offset(0, 0, result._rowCnt, result._colCnt);
                result._optionsRow        = prng.LastRow();
                result._optionsRowIsEmpty = !result._optionsRow.CellsUsed(XLCellsUsedOptions.AllContents | XLCellsUsedOptions.MergedRanges).Any();
                result._totalsCondFormats = sheet.ConditionalFormats
                                            .Where(f => result._optionsRow.Contains(f.Range) && !innerRanges.Any(ir => ir.Ranges.Contains(f.Range)))
                                            .ToArray();
                var rs = prng.RangeAddress.FirstAddress.RowNumber;
                result._condFormats = result._condFormats.Where(x => x.Range.RangeAddress.FirstAddress.RowNumber - rs + 1 <= result._rowCnt).ToArray();
            }
            else
            {
                result._totalsCondFormats = new IXLConditionalFormat[0];
            }

            result._subranges = innerRanges.Select(rng =>
            {
                var tpl              = Parse(rng, buff, errors, globalVariables);
                tpl._buff            = result._buff;
                tpl._isSubrange      = true;
                tpl._globalVariables = globalVariables;
                return(tpl);
            }).ToArray();

            result.ParseTags(prng);

            if (result._rangeOption != null)
            {
                var source = result._rangeOption.GetParameter("source");
                if (!string.IsNullOrEmpty(source))
                {
                    result.Source = source;
                }
            }

            return(result);
        }
Exemple #6
0
 internal RangeTemplate(IXLNamedRange range, TempSheetBuffer buff, int rowCnt, int colCnt, TemplateErrors errors, IDictionary <string, object> globalVariables) : this(range, buff, errors, globalVariables)
 {
     _rowCnt = rowCnt;
     _colCnt = colCnt;
 }
Exemple #7
0
 internal RangeTemplate(IXLNamedRange range, TempSheetBuffer buff, int rowCnt, int colCnt) : this(range, buff)
 {
     _rowCnt = rowCnt;
     _colCnt = colCnt;
 }
Exemple #8
0
        private static RangeTemplate Parse(string name, IXLRange range, TempSheetBuffer buff, TemplateErrors errors, IDictionary <string, object> globalVariables)
        {
            var result = new RangeTemplate(name, range, buff,
                                           range.RowCount(), range.ColumnCount(), errors, globalVariables);

            var innerRanges = GetInnerRanges(range).ToArray();

            var sheet = range.Worksheet;

            for (int iRow = 1; iRow <= result._rowCnt; iRow++)
            {
                for (int iColumn = 1; iColumn <= result._colCnt; iColumn++)
                {
                    var xlCell = range.Cell(iRow, iColumn);
                    if (innerRanges.Any(x => x.Ranges.Cells().Contains(xlCell)))
                    {
                        xlCell = null;
                    }
                    result._cells.Add(iRow, iColumn, xlCell);
                }
                if (iRow != result._rowCnt)
                {
                    result._cells.AddNewRow();
                }
            }

            result._mergedRanges = sheet.MergedRanges.Where(x => range.Contains(x) && !innerRanges.Any(nr => nr.Ranges.Any(r => r.Contains(x)))).ToArray();
            sheet.MergedRanges.RemoveAll(result._mergedRanges.Contains);

            result.ParseTags(range);

            if (result._rowCnt > 1 && !result.IsHorizontal)
            {
                // Exclude special row
                result._rowCnt--;

                result._rowRange          = range.Offset(0, 0, result._rowCnt, result._colCnt);
                result._optionsRow        = range.LastRow();
                result._optionsRowIsEmpty = !result._optionsRow.CellsUsed(XLCellsUsedOptions.AllContents | XLCellsUsedOptions.MergedRanges).Any();
            }

            result._subranges = innerRanges.SelectMany(nrng => nrng.Ranges,
                                                       (nr, rng) =>
            {
                var tpl              = Parse(nr.Name, rng, buff, errors, globalVariables);
                tpl._buff            = result._buff;
                tpl._isSubrange      = true;
                tpl._globalVariables = globalVariables;
                return(tpl);
            }).ToArray();

            if (result._rangeOption != null)
            {
                var source = result._rangeOption.GetParameter("source");
                if (!string.IsNullOrEmpty(source))
                {
                    result.Source = source;
                }
            }

            return(result);
        }