Beispiel #1
0
        protected void ReplaceVar(object dataSource, bool isClearEmptyVar = true)
        {
            List <unvell.ReoGrid.Cell> cells = this.GetAllMergedCells();

            foreach (unvell.ReoGrid.Cell cellFrom in cells)
            {
                string varText = FindVarString(cellFrom.DisplayText);
                if (varText != null)
                {
                    Miles.Coro.Common.MemberAccessor.DelegatedExpressionMemberAccessor memberAccessor = Miles.Coro.Common.MemberAccessor.DelegatedExpressionMemberAccessor.Instance;

                    Model.Tb_tmp_xls_map map = dicVarMap.ContainsKey(varText) ? dicVarMap[varText] : null;
                    if (map != null)
                    {
                        string dataField         = map.Field_Name;
                        object value             = memberAccessor.GetValue(dataSource, dataField);
                        unvell.ReoGrid.Cell cell = this.Sheet.Cells[cellFrom.Row, cellFrom.Column];
                        if (value != null)
                        {
                            SetCellFormatData(cell, value, map.Field_Format);
                        }
                        else
                        {
                            if (isClearEmptyVar)
                            {
                                SetCellFormatData(cell, "", null);
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        protected void ReplaceVar(Dictionary <string, object> dicVars, bool isClearEmptyVar = true)
        {
            List <unvell.ReoGrid.Cell> cells = this.GetAllMergedCells();

            foreach (unvell.ReoGrid.Cell cellFrom in cells)
            {
                string varText = FindVarString(cellFrom.DisplayText);
                if (varText != null && dicVars.ContainsKey(varText))
                {
                    object value             = dicVars[varText];
                    unvell.ReoGrid.Cell cell = this.Sheet.Cells[cellFrom.Row, cellFrom.Column];
                    if (value != null)
                    {
                        SetCellFormatData(cell, value, null);
                    }
                    else
                    {
                        if (isClearEmptyVar)
                        {
                            SetCellFormatData(cell, "", null);
                        }
                    }
                }
            }
        }
Beispiel #3
0
        protected void InsertItems(IList items, XlsItemGroup xlsItemGroup, int itemStartRow, int itemStartCol)
        {
            int itemCount = items.Count;
            int rowCount  = xlsItemGroup.RowCount;

            if (itemCount > 1)
            {
                this.Sheet.InsertRows(itemStartRow + rowCount, rowCount * (itemCount - 1));
            }

            //按照插入行行的合并情况,循环列合并,新增加的行的列
            for (int row = 1; row < itemCount; row++)
            {
                SetItemGroupMergedCell(this.Sheet, itemStartRow + row * rowCount, itemStartCol, xlsItemGroup.CellsTmp);
            }

            for (int row = 0; row < itemCount; row++)
            {
                object dataSource = items[row];
                foreach (unvell.ReoGrid.Cell cellFrom in xlsItemGroup.CellsTmp)
                {
                    int realRow = itemStartRow + row * rowCount;
                    unvell.ReoGrid.Cell cell = this.Sheet.Cells[cellFrom.Row + realRow, cellFrom.Column + itemStartCol];
                    string varText           = FindVarString(cellFrom.DisplayText);
                    if (varText != null)
                    {
                        Miles.Coro.Common.MemberAccessor.DelegatedExpressionMemberAccessor memberAccessor = Miles.Coro.Common.MemberAccessor.DelegatedExpressionMemberAccessor.Instance;

                        Model.Tb_tmp_xls_map map = xlsItemGroup.DicVarMap.ContainsKey(varText) ? xlsItemGroup.DicVarMap[varText] : null;
                        if (map != null)
                        {
                            string dataField = map.Field_Name;
                            object value     = memberAccessor.GetValue(dataSource, dataField);
                            if (value != null)
                            {
                                //根据格式来设置单元格值
                                SetCellFormatData(cell, value, map.Field_Format);
                                this.Sheet.AutoFitRowHeight(realRow);
                            }
                        }
                    }
                    else
                    {
                        cell.Data = cellFrom.Data;
                    }
                }
            }
        }
Beispiel #4
0
        List <Info_link_bill_tent> GetItems()
        {
            List <Info_link_bill_tent> items = new List <Info_link_bill_tent>();

            for (int row = 2; row < reportTmpBillInfo.Sheet.RowCount;)
            {
                unvell.ReoGrid.Cell cell = reportTmpBillInfo.Sheet.Cells[row, 0];
                Info_link_bill_tent item = cell.Tag as Info_link_bill_tent;
                if (item != null)
                {
                    items.Add(item);
                }

                row += 17;
            }

            return(items);
        }
Beispiel #5
0
        private void BtnDelete_Click(object sender, EventArgs e)
        {
            RangePosition pos = this.reoGridControl1.CurrentWorksheet.SelectionRange;

            unvell.ReoGrid.Cell cell = reportTmpBillInfo.Sheet.Cells[pos.Row, pos.Col];
            Info_link_bill_tent item = cell.Tag as Info_link_bill_tent;

            if (item == null)
            {
                return;
            }
            if (this.GetItems().Count > 1)
            {
                reportTmpBillInfo.Sheet.DeleteRows(cell.Row, 17);
            }
            else
            {
                SetItemCellValue(new Info_link_bill_tent(), 2, true);
            }
        }
Beispiel #6
0
        private void BtnEdit_Click(object sender, EventArgs e)
        {
            RangePosition pos = this.reoGridControl1.CurrentWorksheet.SelectionRange;

            unvell.ReoGrid.Cell cell = this.reoGridControl1.CurrentWorksheet.Cells[pos.Row, pos.Col];
            Info_link_bill_tent item = cell.Tag as Info_link_bill_tent;

            if (item == null)
            {
                return;
            }
            FmLinkBillInfo fmLinkBill = new FmLinkBillInfo(item);

            fmLinkBill.SelectValue += (obj) =>
            {
                Info_link_bill_tent info = obj as Info_link_bill_tent;
                int rowOffset            = pos.Row;

                SetItemCellValue(info, rowOffset);
            };
            fmLinkBill.ShowDialog();
        }
Beispiel #7
0
        protected void InsertTable(IList items, Func <object, XlsItemBindInfo[]> funcItemToArray, object dataSource = null)
        {
            int itemCount = items.Count;
            //动态数据模板起始位置行
            int itemStartRow    = (int)tmpXls.Item_Start_Row;
            int itemStartColumn = (int)tmpXls.Item_Start_Column;

            if (itemCount > 1)
            {
                this.Sheet.InsertRows(itemStartRow + ItemRowGroupCount, ItemRowGroupCount * (itemCount - 1));
            }

            List <unvell.ReoGrid.Cell> cellsTmp = GetRangeMergedCells(this.Sheet, itemStartRow, itemStartColumn, ItemRowGroupCount, ItemColumnGroupCount);

            //按照插入行行的合并情况,循环列合并,新增加的行的列
            for (int row = 1; row < itemCount; row++)
            {
                SetItemGroupMergedCell(this.Sheet, row * ItemRowGroupCount, itemStartColumn, cellsTmp);
            }

            for (int row = 0; row < itemCount; row++)
            {
                XlsItemBindInfo[] xlsItems = funcItemToArray(items[row]);
                int i = 0;
                foreach (unvell.ReoGrid.Cell fromCell in cellsTmp)
                {
                    int                 realRow = fromCell.Row + row * ItemRowGroupCount;
                    XlsItemBindInfo     xlsItem = xlsItems[i];
                    unvell.ReoGrid.Cell cell    = this.Sheet.Cells[realRow, fromCell.Column]; //+itemStartColumn
                    if (xlsItem.UseTmpText && fromCell.Data != null)
                    {
                        cell.Data = fromCell.Data;
                    }
                    SetCellFormatData(cell, xlsItem.Value, xlsItem.Format, dataSource);
                    this.Sheet.AutoFitRowHeight(realRow);
                    i++;
                }
            }
        }
Beispiel #8
0
        internal RangePosition SetPartialGrid(RangePosition toRange, PartialGrid data,
                                              PartialGridCopyFlag flag, ExPartialGridCopyFlag exFlag)
        {
            if (toRange.IsEmpty)
            {
                return(toRange);
            }

            toRange = FixRange(toRange);

            int rows = data.Rows;
            int cols = data.Columns;

            if (rows + toRange.Row > this.rows.Count)
            {
                rows = this.rows.Count - toRange.Row;
            }
            if (cols + toRange.Col > this.cols.Count)
            {
                cols = this.cols.Count - toRange.Col;
            }

            if (((flag & PartialGridCopyFlag.CellData) == PartialGridCopyFlag.CellData ||
                 (flag & PartialGridCopyFlag.CellStyle) == PartialGridCopyFlag.CellStyle))
            {
                for (int r = 0; r < rows; r++)
                {
                    for (int c = 0; c < cols; c++)
                    {
                        Cell fromCell = data.Cells == null ? null : data.Cells[r, c];

                        int tr = toRange.Row + r;
                        int tc = toRange.Col + c;

                        bool processed = false;

                        if (fromCell != null)
                        {
                            #region Merge from right side
                            // from cell copied as part of merged cell
                            if (
                                // is part of merged cell
                                !fromCell.MergeStartPos.IsEmpty

                                && fromCell.MergeStartPos.Col < toRange.Col
                                // is right side     -------+--      (undo from delete column at end of merged range)
                                && (fromCell.InternalCol - fromCell.MergeStartPos.Col > tc - toRange.Col
                                    // not inside       --+----+--
                                    && fromCell.MergeEndPos.Col <= toRange.EndCol))
                            {
                                // from cell inside existed merged range
                                // these two ranges should be merged
                                // the original range must be expanded
                                Cell fromMergedStart = CreateAndGetCell(fromCell.MergeStartPos);
                                fromMergedStart.MergeEndPos = new CellPosition(fromMergedStart.MergeEndPos.Row, tc);
                                fromMergedStart.Colspan     = (short)(tc - fromMergedStart.InternalCol + 1);

                                for (int ic = fromMergedStart.InternalCol; ic < fromMergedStart.InternalCol + fromMergedStart.Colspan; ic++)
                                {
                                    var insideCell = cells[tr, ic];
                                    if (insideCell != null)
                                    {
                                        insideCell.MergeEndPos = new CellPosition(insideCell.MergeEndPos.Row, tc);
                                    }
                                }

                                Cell tocell = CreateAndGetCell(tr, tc);
                                tocell.MergeStartPos = fromMergedStart.InternalPos;
                                tocell.MergeEndPos   = new CellPosition(fromMergedStart.MergeEndPos.Row, tc);
                                tocell.Colspan       = 0;
                                tocell.Rowspan       = 0;

                                if (tocell.IsEndMergedCell)
                                {
                                    fromMergedStart.Bounds = GetRangePhysicsBounds(new RangePosition(
                                                                                       fromMergedStart.InternalPos, fromMergedStart.MergeEndPos));
                                }

                                processed = true;
                            }
                            #endregion
                            #region Merge from left side
                            // usually used when undo action: when deleting column from left side of merged cell
                            else if (
                                !fromCell.MergeEndPos.IsEmpty

                                // added 3/15/2016: check two unrelated ranges
                                && toRange.ContainsRow(fromCell.Row)

                                && fromCell.MergeEndPos.Col > toRange.EndCol &&
                                fromCell.MergeStartPos.Col <= toRange.EndCol
                                )
                            {
                                // target partial range will override exsited range
                                // need to update existed range at right side
                                int rightCol = Math.Min(fromCell.MergeEndPos.Col, this.cols.Count - 1);

                                Cell tocell = CreateAndGetCell(tr, tc);
                                tocell.MergeStartPos = new CellPosition(fromCell.MergeStartPos.Row, fromCell.MergeStartPos.Col + tc - fromCell.InternalCol);
                                tocell.MergeEndPos   = new CellPosition(fromCell.MergeEndPos.Row, rightCol);

                                for (int ic = toRange.EndCol + 1; ic <= rightCol; ic++)
                                {
                                    var existedEndCell = CreateAndGetCell(tr, ic);
                                    existedEndCell.MergeStartPos = tocell.MergeStartPos;
                                    existedEndCell.Rowspan       = 0;
                                    existedEndCell.Colspan       = 0;
                                }

                                if (tocell.IsStartMergedCell)
                                {
                                    tocell.Rowspan = (short)(tocell.MergeEndPos.Row - tocell.MergeStartPos.Row + 1);
                                    tocell.Colspan = (short)(tocell.MergeEndPos.Col - tocell.MergeStartPos.Col + 1);

                                    tocell.Bounds = GetRangeBounds(tocell.InternalPos, tocell.MergeEndPos);

                                    // copy cell content
                                    CellUtility.CopyCellContent(tocell, fromCell);

                                    UpdateCellFont(tocell);
                                }
                                else
                                {
                                    tocell.Rowspan = 0;
                                    tocell.Colspan = 0;
                                }

                                processed = true;
                            }
                            #endregion                             // Merge from left side
                            #region Merge from bottom
                            else if (
                                !fromCell.MergeStartPos.IsEmpty
                                // above
                                && fromCell.MergeStartPos.Row < toRange.Row
                                // merged start row in the above of target fill range
                                && fromCell.InternalRow - fromCell.MergeStartPos.Row > tr - toRange.Row
                                // not inside current merged range
                                && fromCell.MergeEndPos.Row <= toRange.EndRow)
                            {
                                var mergedStartCell = CreateAndGetCell(fromCell.MergeStartPos);
                                mergedStartCell.Rowspan = (short)(tr - mergedStartCell.InternalRow + 1);

                                for (int ir = fromCell.MergeStartPos.Row; ir < tr; ir++)
                                {
                                    var existedCell = CreateAndGetCell(ir, tc);
                                    existedCell.MergeEndPos = new CellPosition(tr, fromCell.MergeEndPos.Col);
                                }

                                var tocell = CreateAndGetCell(tr, tc);
                                tocell.MergeStartPos = mergedStartCell.InternalPos;
                                tocell.MergeEndPos   = new CellPosition(tr, fromCell.MergeEndPos.Col);
                                tocell.Rowspan       = 0;
                                tocell.Colspan       = 0;

                                if (tocell.IsEndMergedCell)
                                {
                                    mergedStartCell.Bounds = GetRangeBounds(mergedStartCell.InternalPos, mergedStartCell.MergeEndPos);
                                }

                                processed = true;
                            }
                            #endregion                             // Merge from bottom
                            #region Merge from top
                            // usually used when undo action: when deleting column from top side of merged cell
                            else if (
                                !fromCell.MergeEndPos.IsEmpty

                                // added 3/15/2016: check two unrelated ranges
                                && toRange.ContainsColumn(fromCell.Column)

                                && fromCell.MergeEndPos.Row > toRange.EndRow &&
                                fromCell.MergeStartPos.Row <= toRange.EndRow)
                            {
                                // target partial range will override exsited range
                                // need to update existed range at right side
                                int bottomRow = Math.Min(fromCell.MergeEndPos.Row, this.rows.Count - 1);

                                for (int ir = toRange.EndRow + 1; ir <= bottomRow; ir++)
                                {
                                    var existedEndCell = CreateAndGetCell(ir, tc);
                                    existedEndCell.MergeStartPos = new CellPosition(fromCell.MergeStartPos.Row, existedEndCell.MergeStartPos.Col);
                                    existedEndCell.Rowspan       = 0;
                                    existedEndCell.Colspan       = 0;
                                }

                                Cell tocell = CreateAndGetCell(tr, tc);
                                tocell.MergeStartPos = fromCell.MergeStartPos;
                                tocell.MergeEndPos   = new CellPosition(bottomRow, fromCell.MergeEndPos.Col);

                                if (tocell.IsStartMergedCell)
                                {
                                    tocell.Rowspan = (short)(tocell.MergeEndPos.Row - tocell.MergeStartPos.Row + 1);
                                    tocell.Colspan = (short)(tocell.MergeEndPos.Col - tocell.MergeStartPos.Col + 1);

                                    tocell.Bounds = GetRangeBounds(tocell.InternalPos, tocell.MergeEndPos);

                                    // copy cell content
                                    CellUtility.CopyCellContent(tocell, fromCell);

                                    UpdateCellFont(tocell);
                                }
                                else
                                {
                                    tocell.Rowspan = 0;
                                    tocell.Colspan = 0;
                                }

                                processed = true;
                            }
                            #endregion                             // Merge from top
                        }

                        if (!processed)
                        {
                            Cell toCell = CreateAndGetCell(tr, tc);

                            if (toCell.Rowspan == 0 && toCell.Colspan == 0)
                            {
                                continue;
                            }

                            if (fromCell != null)
                            {
                                #region Copy Data
                                if ((flag & PartialGridCopyFlag.CellData) == PartialGridCopyFlag.CellData)
                                {
                                    CellUtility.CopyCellContent(toCell, fromCell);
                                }
                                #endregion                                 // Copy Data

                                #region Format Formula
#if FORMULA
                                if ((flag & PartialGridCopyFlag.CellFormula) == PartialGridCopyFlag.CellFormula)
                                {
                                    if (fromCell.HasFormula)
                                    {
                                        if (fromCell.formulaTree == null)
                                        {
                                            try
                                            {
                                                fromCell.formulaTree = Formula.Parser.Parse(this.workbook, fromCell, fromCell.InnerFormula);
                                            }
                                            catch
                                            {
                                                fromCell.formulaStatus = FormulaStatus.SyntaxError;
                                            }
                                        }

                                        if (fromCell.formulaTree != null)
                                        {
                                            var rs = new ReplacableString(fromCell.InnerFormula);
                                            Stack <List <Cell> > dirtyCells = new Stack <List <Cell> >();
                                            FormulaRefactor.CopyFormula(fromCell.Position, fromCell.formulaTree, toCell, rs, dirtyCells);
                                        }

                                        toCell.FontDirty = true;
                                    }
                                }
                                else
                                {
                                    toCell.InnerFormula = null;
                                }
#endif // FORMULA
                                #endregion // Formula Formula

                                #region Copy Merged info

                                // is single cell
                                if (toCell.Rowspan == 1 && toCell.Colspan == 1)
                                {
                                    // then copy span info
                                    toCell.Rowspan = fromCell.Rowspan;
                                    toCell.Colspan = fromCell.Colspan;

                                    if (!fromCell.MergeStartPos.IsEmpty)
                                    {
                                        toCell.MergeStartPos = fromCell.MergeStartPos.Offset(tr - fromCell.InternalRow, tc - fromCell.InternalCol);

#if DEBUG
                                        Debug.Assert(toCell.MergeStartPos.Row >= 0 && toCell.MergeStartPos.Row < this.rows.Count);
                                        Debug.Assert(toCell.MergeStartPos.Col >= 0 && toCell.MergeStartPos.Col < this.cols.Count);
#endif
                                    }

                                    if (!fromCell.MergeEndPos.IsEmpty)
                                    {
                                        toCell.MergeEndPos = fromCell.MergeEndPos.Offset(tr - fromCell.InternalRow, tc - fromCell.InternalCol);

#if DEBUG
                                        Debug.Assert(toCell.MergeEndPos.Row >= 0 && toCell.MergeEndPos.Row < this.rows.Count);
                                        Debug.Assert(toCell.MergeEndPos.Col >= 0 && toCell.MergeEndPos.Col < this.cols.Count);
#endif
                                    }
                                }
                                else
                                {
                                    UpdateCellFont(toCell);
                                }
                                #endregion // Copy Merged info

                                #region Cell Styles
                                if (((flag & PartialGridCopyFlag.CellStyle) == PartialGridCopyFlag.CellStyle) &&
                                    fromCell.InnerStyle != null)
                                {
                                    if (fromCell.StyleParentKind == StyleParentKind.Own)
                                    {
                                        // from cell has own style, need copy the style
                                        toCell.InnerStyle = new WorksheetRangeStyle(fromCell.InnerStyle);
                                    }
                                    else
                                    {
                                        // from cell doesn't have own style, copy the reference of style
                                        toCell.InnerStyle = fromCell.InnerStyle;
                                    }

                                    // copy style parent flag
                                    toCell.StyleParentKind = fromCell.StyleParentKind;

                                    // TODO: render alignment is not contained in cell's style
                                    // copy the style may also need copy the render alignment
                                    // or we need to update the cell format again?
                                    if (fromCell.InnerStyle.HAlign == ReoGridHorAlign.General)
                                    {
                                        toCell.RenderHorAlign = fromCell.RenderHorAlign;
                                    }
                                }
                                #endregion                                 // Cell Styles

                                if (toCell.IsEndMergedCell)
                                {
                                    Cell cell = GetCell(toCell.MergeStartPos);
                                    Debug.Assert(cell != null);

                                    UpdateCellBounds(cell);
                                }
                                else if (toCell.Rowspan == 1 && toCell.Colspan == 1)
                                {
                                    UpdateCellFont(toCell);
                                }
                            }
                            else
                            {
                                cells[tr, tc] = null;
                            }
                        }
                    }
                }
            }

            // h-borders
            if ((flag & PartialGridCopyFlag.HBorder) == PartialGridCopyFlag.HBorder)
            {
                if (data.HBorders == null)
                {
                    // cut left side border
                    if (toRange.Col > 0)
                    {
                        for (int r = toRange.Row; r <= toRange.EndRow; r++)
                        {
                            this.CutBeforeHBorder(r, toRange.Col);
                        }
                    }

                    // set borders to null
                    this.hBorders.Iterate(toRange.Row, toRange.Col, rows, cols, true, (r, c, fromHBorder) =>
                    {
                        this.hBorders[r, c] = null;
                        return(1);
                    }
                                          );
                }
                else
                {
                    // TODO: need to improve performance
                    for (int r = 0; r < rows + 1; r++)
                    {
                        for (int c = 0; c < cols; c++)
                        {
                            int tr = toRange.Row + r;
                            int tc = toRange.Col + c;

                            this.CutBeforeHBorder(tr, tc);

                            var fromHBorder = data.HBorders[r, c];

                            if (fromHBorder == null)
                            {
                                hBorders[tr, tc] = null;
                            }
                            else
                            {
                                RangeBorderStyle style = fromHBorder.Style;

                                int hcols = fromHBorder.Span;
                                if (hcols > cols - c)
                                {
                                    hcols = cols - c;
                                }

                                this.GetHBorder(tr, tc).Span = hcols;

                                if (data.HBorders[r, c].Style != null)
                                {
                                    // in last col
                                    //if (c == cols - 1)
                                    //	SetHBorders(tr, tc, hcols, style, fromHBorder.Pos);
                                    //else
                                    //	hBorders[tr, tc].Border = style;

                                    SetHBorders(tr, tc, hcols, style, fromHBorder.Pos);
                                }
                                else
                                {
                                    hBorders[tr, tc].Style = RangeBorderStyle.Empty;
                                }
                            }
                        }
                    }
                }
            }

            // v-borders
            if ((flag & PartialGridCopyFlag.VBorder) == PartialGridCopyFlag.VBorder)
            {
                if (data.VBorders == null)
                {
                    // cut top side border
                    if (toRange.Row > 0)
                    {
                        for (int c = toRange.Col; c <= toRange.EndCol; c++)
                        {
                            CutBeforeVBorder(toRange.Row, c);
                        }
                    }

                    // set border to null
                    this.vBorders.Iterate(toRange.Row, toRange.Col, rows, cols, true, (r, c, fromVBorder) =>
                    {
                        this.vBorders[r, c] = null;
                        return(1);
                    }
                                          );
                }
                else
                {
                    // TODO: need to improve performance
                    for (int r = 0; r < rows; r++)
                    {
                        for (int c = 0; c < cols + 1; c++)
                        {
                            int tr = toRange.Row + r;
                            int tc = toRange.Col + c;

                            this.CutBeforeVBorder(tr, tc);

                            var fromVBorder = data.VBorders[r, c];

                            if (fromVBorder == null)
                            {
                                vBorders[tr, tc] = null;
                            }
                            else
                            {
                                RangeBorderStyle style = fromVBorder.Style;

                                int vrows = fromVBorder.Span;
                                if (vrows > rows - r)
                                {
                                    vrows = rows - r;
                                }
                                GetVBorder(tr, tc).Span = vrows;

                                if (data.VBorders[r, c].Style != null)
                                {
                                    // is last row
                                    //if (r == rows - 1)
                                    //	SetVBorders(tr, tc, vrows, style, fromVBorder.Pos);
                                    //else
                                    //	vBorders[tr, tc].Border = fromVBorder.Border;
                                    this.SetVBorders(tr, tc, vrows, style, fromVBorder.Pos);
                                }
                                else
                                {
                                    this.vBorders[tr, tc].Style = RangeBorderStyle.Empty;
                                }
                            }
                        }
                    }
                }
            }

            return(new RangePosition(toRange.Row, toRange.Col, rows, cols));
        }
Beispiel #9
0
        internal PartialGrid GetPartialGrid(RangePosition range, PartialGridCopyFlag flag, ExPartialGridCopyFlag exFlag,
                                            bool checkIntersectedRange = false)
        {
            range = FixRange(range);

            if (checkIntersectedRange)
            {
                var intersectedRange = CheckIntersectedMergingRange(range);
                if (intersectedRange != RangePosition.Empty)
                {
                    throw new RangeIntersectionException(intersectedRange);
                }
            }

            int rows = range.Rows;
            int cols = range.Cols;

            PartialGrid data = new PartialGrid()
            {
                Columns = cols,
                Rows    = rows,
            };

            if ((flag & PartialGridCopyFlag.CellData) == PartialGridCopyFlag.CellData ||
                (flag & PartialGridCopyFlag.CellStyle) == PartialGridCopyFlag.CellStyle)
            {
                data.Cells = new CellArray();

                for (int r = range.Row; r <= range.EndRow; r++)
                {
                    for (int c = range.Col; c <= range.EndCol; c++)
                    {
                        var cell = this.cells[r, c];

                        int toRow = r - range.Row;
                        int toCol = c - range.Col;

                        //if (cell == null && data.Cells[toRow, toCol] == null)
                        //{
                        //	c++;
                        //	continue;
                        //}

                        Cell toCell = null;

                        if (cell != null)
                        {
                            toCell = new Cell(this);
                            CellUtility.CopyCell(toCell, cell);
                        }
                        else
                        {
                            StyleParentKind pKind = StyleParentKind.Own;
                            var             style = StyleUtility.FindCellParentStyle(this, r, c, out pKind);

                            style = StyleUtility.DistinctStyle(style, Worksheet.DefaultStyle);

                            if (style != null)
                            {
                                toCell                 = new Cell(this);
                                toCell.Colspan         = 1;
                                toCell.Rowspan         = 1;
                                toCell.InnerStyle      = style;
                                toCell.StyleParentKind = StyleParentKind.Own;
                            }
                        }

                        if (toCell != null)
                        {
                            data.Cells[toRow, toCol] = toCell;
                        }

                        //c += (cell == null || cell.Colspan < 1) ? 1 : cell.Colspan;
                    }
                }
            }

            if ((flag & PartialGridCopyFlag.HBorder) == PartialGridCopyFlag.HBorder)
            {
                data.HBorders = new HBorderArray();

                hBorders.Iterate(range.Row, range.Col, rows + 1, cols, true, (r, c, hBorder) =>
                {
                    // only copy borders they belong to the cell (unless BorderOutsideOwner is specified)
                    if (((exFlag & ExPartialGridCopyFlag.BorderOutsideOwner) == ExPartialGridCopyFlag.BorderOutsideOwner) ||
                        (hBorder != null && hBorder.Pos == HBorderOwnerPosition.None) ||
                        (
                            (r != range.Row ||
                             (hBorder != null &&
                              (hBorder.Pos & HBorderOwnerPosition.Top) == HBorderOwnerPosition.Top))
                            &&
                            (r != range.EndRow + 1 ||
                             (hBorder != null &&
                              (hBorder.Pos & HBorderOwnerPosition.Bottom) == HBorderOwnerPosition.Bottom)))
                        )
                    {
                        int toCol = c - range.Col;
                        ReoGridHBorder thBorder = ReoGridHBorder.Clone(hBorder);
                        if (thBorder != null && thBorder.Span > cols - toCol)
                        {
                            thBorder.Span = cols - toCol;
                        }
                        data.HBorders[r - range.Row, toCol] = thBorder;
                    }
                    return(1);
                });
            }

            if ((flag & PartialGridCopyFlag.VBorder) == PartialGridCopyFlag.VBorder)
            {
                data.VBorders = new VBorderArray();

                vBorders.Iterate(range.Row, range.Col, rows, cols + 1, true, (r, c, vBorder) =>
                {
                    // only copy borders they belong to the cell (unless BorderOutsideOwner is specified)
                    if (((exFlag & ExPartialGridCopyFlag.BorderOutsideOwner) == ExPartialGridCopyFlag.BorderOutsideOwner) ||
                        (vBorder != null && vBorder.Pos == VBorderOwnerPosition.None) ||
                        (
                            (c != range.Col ||
                             (vBorder != null &&
                              (vBorder.Pos & VBorderOwnerPosition.Left) == VBorderOwnerPosition.Left))
                            &&
                            (c != range.EndCol + 1 ||
                             (vBorder != null &&
                              (vBorder.Pos & VBorderOwnerPosition.Right) == VBorderOwnerPosition.Right)))
                        )
                    {
                        int toRow = r - range.Row;
                        ReoGridVBorder tvBorder = ReoGridVBorder.Clone(vBorder);
                        if (tvBorder != null && tvBorder.Span > rows - toRow)
                        {
                            tvBorder.Span = rows - toRow;
                        }
                        data.VBorders[toRow, c - range.Col] = tvBorder;
                    }
                    return(1);
                });
            }

            return(data);
        }
Beispiel #10
0
        /// <summary>
        /// Force end current editing operation with the specified reason.
        /// Uses specified data instead of the data of user edited.
        /// </summary>
        /// <param name="data">New data to be set to the edited cell</param>
        /// <param name="reason">Ending Reason of editing operation</param>
        /// <returns>True if currently in editing mode, and operation has been
        /// finished successfully.</returns>
        public bool EndEdit(object data, EndEditReason reason)
        {
            if (currentEditingCell == null || endEditProcessing)
            {
                return(false);
            }

            endEditProcessing = true;

            if (data == null)
            {
                data = this.controlAdapter.GetEditControlText();
            }

            if (AfterCellEdit != null)
            {
                CellAfterEditEventArgs arg = new CellAfterEditEventArgs(currentEditingCell)
                {
                    EndReason = reason,
                    NewData   = data,
                };

                AfterCellEdit(this, arg);
                data   = arg.NewData;
                reason = arg.EndReason;
            }

            switch (reason)
            {
            case EndEditReason.Cancel:
                break;

            case EndEditReason.NormalFinish:
                if (data is string)
                {
                    var datastr = (string)data;

                    if (string.IsNullOrEmpty(datastr))
                    {
                        data = null;
                    }
                    else
                    {
                        double numericValue = 0;

                        // convert data into cell data format
                        switch (currentEditingCell.DataFormat)
                        {
                        case CellDataFormatFlag.Number:
                        case CellDataFormatFlag.Currency:
                            if (double.TryParse(datastr, out numericValue))
                            {
                                data = numericValue;
                            }
                            break;

                        case CellDataFormatFlag.Percent:
                            if (datastr.EndsWith("%"))
                            {
                                double val;
                                if (double.TryParse(datastr.Substring(0, datastr.Length - 1), out val))
                                {
                                    data = val / 100;
                                }
                            }
                            else if (datastr == "%")
                            {
                                data = null;
                            }
                            break;

                        case CellDataFormatFlag.DateTime:
                        {
                            DateTime dt = DateTime.Now;
                            if (DateTime.TryParse(datastr, out dt))
                            {
                                data = dt;
                            }
                        }
                        break;
                        }
                    }
                }

                if (string.IsNullOrEmpty(backupData))
                {
                    backupData = null;
                }

                var body = currentEditingCell.body;

                if (body != null)
                {
                    data = body.OnEndEdit(data);
                }

                if (!object.Equals(data, backupData))
                {
                    DoAction(new SetCellDataAction(currentEditingCell.InternalRow, currentEditingCell.InternalCol, data));
                }

                break;
            }

            this.controlAdapter.HideEditControl();
            this.controlAdapter.Focus();
            currentEditingCell = null;

            endEditProcessing = false;

            return(true);
        }
Beispiel #11
0
        internal bool StartEdit(Cell cell, string newText)
        {
            // abort if either spreadsheet or cell is readonly
            if (this.HasSettings(WorksheetSettings.Edit_Readonly) ||
                cell == null || cell.IsReadOnly)
            {
                return(false);
            }

            if (this.focusPos != cell.Position)
            {
                this.FocusPos = cell.Position;
            }
            else
            {
                this.ScrollToCell(cell);
            }

            string editText = null;

            if (newText == null)
            {
                if (!string.IsNullOrEmpty(cell.InnerFormula))
                {
                    editText = "=" + cell.InnerFormula;
                }
                else if (cell.InnerData is string)
                {
                    editText = (string)cell.InnerData;
                }
#if DRAWING
                else if (cell.InnerData is Drawing.RichText)
                {
                    editText = ((Drawing.RichText)cell.InnerData).ToString();
                }
#endif // DRAWING
                else
                {
                    editText = Convert.ToString(cell.InnerData);
                }

                this.backupData = editText;
            }
            else
            {
                editText = newText;

                this.backupData = cell.DisplayText;
            }

            if (cell.DataFormat == CellDataFormatFlag.Percent &&
                this.HasSettings(WorksheetSettings.Edit_FriendlyPercentInput))
            {
                double val;

                if (double.TryParse(editText, out val))
                {
                    editText = (newText == null ? (val * 100) : val) + "%";
                }
            }

            if (BeforeCellEdit != null)
            {
                CellBeforeEditEventArgs arg = new CellBeforeEditEventArgs(cell)
                {
                    EditText = editText,
                };

                BeforeCellEdit(this, arg);

                if (arg.IsCancelled)
                {
                    return(false);
                }

                editText = arg.EditText;
            }

#if EX_SCRIPT
            // v0.8.2: 'beforeCellEdit' renamed to 'onCellEdit'
            // v0.8.5: 'onCellEdit' renamed to 'oncelledit'
            object scriptReturn = RaiseScriptEvent("oncelledit", new RSCellObject(this, cell.InternalPos, cell));
            if (scriptReturn != null && !ScriptRunningMachine.GetBoolValue(scriptReturn))
            {
                return(false);
            }
#endif

            if (cell.body != null)
            {
                bool canContinue = cell.body.OnStartEdit();
                if (!canContinue)
                {
                    return(false);
                }
            }

            if (currentEditingCell != null)
            {
                EndEdit(this.controlAdapter.GetEditControlText());
            }

            this.currentEditingCell = cell;

            this.controlAdapter.SetEditControlText(editText);

            if (cell.DataFormat == CellDataFormatFlag.Percent && editText.EndsWith("%"))
            {
                this.controlAdapter.SetEditControlCaretPos(editText.Length - 1);
            }

            RGFloat x = 0;

            RGFloat width = (cell.Width - 1) * this.renderScaleFactor;

            int cellIndentSize = 0;

            //if ((cell.InnerStyle.Flag & PlainStyleFlag.Indent) == PlainStyleFlag.Indent)
            //{
            //indentSize = (int)Math.Round(cell.InnerStyle.Indent * this.indentSize * this.scaleFactor);
            //width -= indentSize;
            //}

#if WINFORM
            if (width < cell.TextBounds.Width)
            {
                width = cell.TextBounds.Width;
            }
#elif WPF
            // why + 6 ?
            if (width < cell.TextBounds.Width)
            {
                width = cell.TextBounds.Width + 6;
            }
#endif

            width--;
            //width = (width - 1);

            RGFloat scale = this.renderScaleFactor;

            #region Horizontal alignment
            switch (cell.RenderHorAlign)
            {
            default:
            case ReoGridRenderHorAlign.Left:
                this.controlAdapter.SetEditControlAlignment(ReoGridHorAlign.Left);
                x = cell.Left * scale + 1 + cellIndentSize;
                break;

            case ReoGridRenderHorAlign.Center:
                this.controlAdapter.SetEditControlAlignment(ReoGridHorAlign.Center);
                x = (cell.Left * scale + (((cell.Width - 1) * scale - 1) - width) / 2) + 1;
                break;

            case ReoGridRenderHorAlign.Right:
                this.controlAdapter.SetEditControlAlignment(ReoGridHorAlign.Right);
                x = (cell.Right - 1) * scale - width - cellIndentSize;
                break;
            }

            if (cell.InnerStyle.HAlign == ReoGridHorAlign.DistributedIndent)
            {
                this.controlAdapter.SetEditControlAlignment(ReoGridHorAlign.Center);
            }
            #endregion             // Horizontal alignment

            RGFloat y = cell.Top * scale + 1;

            var activeViewport = viewportController.FocusView as IViewport;

            int boxX = (int)Math.Round(x + viewportController.FocusView.Left - (activeViewport == null ? 0 : (activeViewport.ViewLeft * scale)));
            int boxY = (int)Math.Round(y + viewportController.FocusView.Top - (activeViewport == null ? 0 : (activeViewport.ViewTop * scale)));

            RGFloat height = (cell.Height - 1) * scale - 1;

            if (!cell.IsMergedCell && cell.InnerStyle.TextWrapMode != TextWrapMode.NoWrap)
            {
                if (height < cell.TextBounds.Height)
                {
                    height = cell.TextBounds.Height;
                }
            }

            int offsetHeight = 0;            // (int)Math.Round(height);// (int)Math.Round(height + 2 - (cell.Height));

            if (offsetHeight > 0)
            {
                switch (cell.InnerStyle.VAlign)
                {
                case ReoGridVerAlign.Top:
                    break;

                default:
                case ReoGridVerAlign.Middle:
                    boxY -= offsetHeight / 2;
                    break;

                case ReoGridVerAlign.Bottom:
                    boxY -= offsetHeight;
                    break;
                }
            }

            Rectangle rect = new Rectangle(boxX, boxY, width, height);

            this.controlAdapter.ShowEditControl(rect, cell);

            return(true);
        }
Beispiel #12
0
 internal bool StartEdit(Cell cell)
 {
     return(this.StartEdit(cell, null));
 }