Пример #1
0
        private void VerticalTable(object[] items, FormulaEvaluator evaluator)
        {
            var rangeStart = _buff.NextAddress;

            for (int i = 0; i < items.Length; i++)
            {
                var        rowStart = _buff.NextAddress;
                IXLAddress rowEnd   = null;
                int        row      = 1;
                var        tags     = _tags.CopyTo(_rowRange);

                // render row cells
                for (var iCell = 0; iCell < _cells.Count; iCell++)
                {
                    var cell = _cells[iCell];
                    if (cell.Row > _rowCnt)
                    {
                        break;
                    }

                    if (cell.CellType == TemplateCellType.None)
                    {
                        RenderSubrange(items[i], evaluator, cell, tags, ref iCell, ref row);
                    }
                    else if (cell.CellType == TemplateCellType.NewRow)
                    {
                        row++;
                        rowEnd = _buff.PrevAddress;
                        _buff.NewRow();
                        if (row > _rowCnt)
                        {
                            break;
                        }
                    }
                    else
                    {
                        RenderCell(items, i, evaluator, cell);
                    }
                }

                var newRowRng = _buff.GetRange(rowStart, rowEnd);
                foreach (var mrg in _mergedRanges.Where(r => !_optionsRow.Contains(r)))
                {
                    var newMrg = mrg.Relative(_rowRange, newRowRng);
                    newMrg.Merge(false);
                }

                if (_rowCnt > 1)
                {
                    _buff.AddConditionalFormats(_condFormats, _rowRange, newRowRng);
                }
                tags.Execute(new ProcessingContext(newRowRng, items[i]));
            }

            // Render options row
            if (!_optionsRowIsEmpty)
            {
                foreach (var cell in _cells.Where(c => c.Row == _rowCnt + 1).OrderBy(c => c.Column))
                {
                    RenderCell(evaluator, cell);
                }
                _buff.NewRow();
            }

            // Execute range options tags
            var resultRange = _buff.GetRange(rangeStart, _buff.PrevAddress);

            if (_rowCnt == 1)
            {
                var rows = resultRange.RowCount() - (_optionsRowIsEmpty ? 0 : 1);
                _buff.AddConditionalFormats(_condFormats, _rowRange, resultRange.Offset(0, 0, rows, resultRange.ColumnCount()));
            }
            if (!_optionsRowIsEmpty)
            {
                var optionsRow = resultRange.LastRow().AsRange();
                foreach (var mrg in _mergedRanges.Where(r => _optionsRow.Contains(r)))
                {
                    var newMrg = mrg.Relative(_optionsRow, optionsRow);
                    newMrg.Merge();
                }
                _buff.AddConditionalFormats(_totalsCondFormats, _optionsRow, optionsRow);
            }

            if (_isSubrange)
            {
                _rangeTags.Execute(new ProcessingContext(resultRange, new DataSource(items)));
            }
        }
Пример #2
0
        private void VerticalTable(object[] items, FormulaEvaluator evaluator)
        {
            var rangeStart = _buff.NextAddress;

            for (int i = 0; i < items.Length; i++)
            {
                var        startAddr         = _buff.NextAddress;
                IXLAddress rowEnd            = null;
                int        row               = 1;
                var        tags              = _tags.CopyTo(_rowRange);
                var        renderedSubranges = new List <string>();

                // render row cells
                for (var iCell = 0; iCell < _cells.Count; iCell++)
                {
                    var cell = _cells[iCell];
                    if (cell.Row > _rowCnt)
                    {
                        break;
                    }

                    if (cell.CellType == TemplateCellType.None)
                    {
                        var xlCell = _rowRange.Cell(cell.Row, cell.Column);
                        var ownRng = _subranges.First(r => r._cells.Any(c => c.CellType != TemplateCellType.None && c.XLCell != null && Equals(c.XLCell.Address, xlCell.Address)));
                        if (!renderedSubranges.Contains(ownRng.Name))
                        {
                            RenderSubrange(ownRng, items[i], evaluator, cell, tags, ref iCell, ref row);
                            renderedSubranges.Add(ownRng.Name);
                        }
                    }
                    else if (cell.CellType == TemplateCellType.NewRow)
                    {
                        row++;
                        rowEnd = _buff.PrevAddress;
                        _buff.NewRow(startAddr);
                        if (row > _rowCnt)
                        {
                            break;
                        }
                    }
                    else
                    {
                        RenderCell(items, i, evaluator, cell);
                    }
                }

                var newRowRng = _buff.GetRange(startAddr, rowEnd);
                foreach (var mrg in _mergedRanges.Where(r => !_optionsRow.Contains(r)))
                {
                    var newMrg = mrg.Relative(_rowRange, newRowRng);
                    newMrg.Merge(false);
                }

                tags.Execute(new ProcessingContext(newRowRng, items[i], evaluator));
            }

            // Render options row
            if (!_isOptionsRowEmpty)
            {
                foreach (var cell in _cells.Where(c => c.Row == _rowCnt + 1).OrderBy(c => c.Column))
                {
                    RenderCell(evaluator, cell);
                }
                _buff.NewRow(rangeStart);
            }

            // Execute range options tags
            var resultRange = _buff.GetRange(rangeStart, _buff.PrevAddress);

            if (!_isOptionsRowEmpty)
            {
                var optionsRow = resultRange.LastRow().AsRange();
                foreach (var mrg in _mergedRanges.Where(r => _optionsRow.Contains(r)))
                {
                    var newMrg = mrg.Relative(_optionsRow, optionsRow);
                    newMrg.Merge();
                }
            }

            // arrage rows height
            var worksheet  = _rowRange.Worksheet;
            var rowNumbers = _cells.Where(xc => xc.XLCell != null && xc.Row <= _rowCnt)
                             .Select(xc => xc.XLCell.Address.RowNumber)
                             .Distinct()
                             .ToArray();
            var heights = rowNumbers
                          .Select(c => worksheet.Row(c).Height)
                          .ToArray();
            var firstRow = rowNumbers.Min();

            foreach (var row in Enumerable.Range(rangeStart.RowNumber, _buff.PrevAddress.RowNumber))
            {
                worksheet.Row(firstRow + row - 1).Height = heights[(row - 1) % heights.Length];
            }

            if (_isSubrange)
            {
                _rangeTags.Execute(new ProcessingContext(resultRange, new DataSource(items), evaluator));
                // if the range was increased by processing tags (for example, Group), move the buffer to the last cell
                _buff.SetPrevCellToLastUsed();
            }
        }