I Created InsertColomn Method

Oct 20, 2013 at 2:36 PM
    Hi,
    I'm Keiju Hirokawa.
    I Created InsertColomn Method that Inserts a new colomn into the spreadsheet.
    Please use this Method.
    If you take notice of something in this Method please give me advices.

【ExcelWorksheet.cs】
        /// <summary>
        /// Inserts a new colomn into the spreadsheet.  Existing colomn below the position are 
        /// shifted right.  All formula are updated to take account of the new colomn.
        /// </summary>
        /// <param name="colFrom"></param>
        /// <param name="cols"></param>
        public void InsertColomn(int colFrom, int cols)
        {
            InsertColomn(colFrom, cols, 0);
        }
        
        /// <summary>
        /// Inserts a new colomn into the spreadsheet.  Existing colomn below the position are 
        /// shifted right.  All formula are updated to take account of the new colomn.
        /// </summary>
        /// <param name="colFrom"></param>
        /// <param name="cols"></param>
        /// <param name="copyStylesFromColomn"></param>
        public void InsertColomn(int colFrom, int cols, int copyStylesFromColomn)
        {
            //Insert the new colomn into the collection
            List<ExcelCell> copyStylesCells = new List<ExcelCell>();
            if (copyStylesFromColomn > 0)
            {
                for (int i = 0; i < _cells.Count; i++)
                {
                    if ((_cells[i] as ExcelCell).Column == copyStylesFromColomn)
                    {
                        copyStylesCells.Add(_cells[i] as ExcelCell);
                    }
                }
            }

            //Insert logic
            _cells.InsertColumn(colFrom, cols);
            _columns.InsertColumn(colFrom, cols);
            
            _formulaCells.InsertColumnsUpdateIndex(colFrom, cols);
            
            if (_comments != null) _comments._comments.InsertColumnsUpdateIndex(colFrom, cols);
            if (_vmlDrawings != null) _vmlDrawings._drawings.InsertColumnsUpdateIndex(colFrom, cols);
            
            foreach (ExcelCell cell in _formulaCells)
            {
                if (cell.SharedFormulaID < 0)
                {
                    cell.Formula = ExcelCell.UpdateFormulaReferences(cell.Formula, 0, cols, 0, colFrom);
                }
                else
                {
                    throw new Exception("Shared formula error");
                }
            }
            
            FixMergedCellsByColmnAdd(colFrom, cols, false);
            
            //Copy the styles
            foreach (ExcelCell cell in copyStylesCells)
            {
                Cells[cell.Row, colFrom, cell.Row, colFrom + cols - 1].StyleID = cell.StyleID;
            }
        }

        /// <summary>
        /// Adds a value to the row of merged cells to fix for inserts or deletes
        /// </summary>
        /// <param name="position"></param>
        /// <param name="cols"></param>
        /// <param name="delete"></param>
        private void FixMergedCellsByColmnAdd(int position, int cols, bool delete)
        {
            List<int> removeIndex = new List<int>();
            for (int i = 0; i < _mergedCells.Count; i++)
            {
                ExcelAddressBase addr = new ExcelAddressBase(_mergedCells[i]), newAddr;
                if (delete)
                {
                    newAddr = addr.DeleteColumn(position, cols);
                    if (newAddr == null)
                    {
                        removeIndex.Add(i);
                        continue;
                    }
                }
                else
                {
                    newAddr = addr.AddColumn(position, cols);
                }

                //The address has changed.
                if (newAddr._address != addr._address)
                {
                    //Set merged prop for cells
                    for (int col = newAddr._fromCol; col <= newAddr._toCol; col++)
                    {
                        for (int row = newAddr._fromRow; row <= newAddr._toRow; row++)
                        {
                            Cell(row,col).Merge = true;
                        }
                    }
                }
                _mergedCells.List[i] = newAddr._address;
            }

            for (int i = removeIndex.Count - 1; i >= 0; i--)
            {
                _mergedCells.List.RemoveAt(removeIndex[i]);
            }
        }
【RangeCollection.cs】
        /// Insert a number of column in the collecion
        /// </summary>
        /// <param name="rowID"></param>
        /// <param name="rows"></param>
        /// <returns>Index of first rangeItem</returns>
        internal void InsertColumn(int ColumnIndex, int columns)
        {
            ulong colAdd = (((ulong)columns) << 15);

            bool isCell;
            for (int i = 0; i < _cells.Count; i++)
            {
                isCell = false;

                //check whether cell
                var cell = _cells[_cellIndex[i].ListPointer] as ExcelCell;
                if (cell != null)
                {
                    isCell = true;
                }
                if (isCell &&
                    cell.Column < ColumnIndex)
                {
                    continue;
                }

                //check whether colomn
                var col = _cells[_cellIndex[i].ListPointer] as ExcelColumn;
                int preColumnMax = 0;

                if (!isCell)
                {
                    if (col != null
                       && col.ColumnMin < ColumnIndex)
                    {
                        continue;
                    }
                    else
                    {
                        preColumnMax = col.ColumnMax;
                    }
                }

                _cellIndex[i].RangeID += colAdd;
                _cells[_cellIndex[i].ListPointer].RangeID += colAdd;

                if (!isCell)
                {
                    (_cells[_cellIndex[i].ListPointer] as ExcelColumn)._columnMax = preColumnMax + columns;
                }
            }
        }

        /// <summary>
        /// Insert a number of columns in the collecion but dont update the cell only the index
        /// </summary>
        /// <param name="rowID"></param>
        /// <param name="rows"></param>
        /// <returns>Index of first rangeItem</returns>
        internal void InsertColumnsUpdateIndex(int ColumnIndex, int columns)
        {
            ulong colAdd = (((ulong)columns) << 15);

            for (int i = 0; i < _cells.Count; i++)
            {
                if ((_cells[i] as ExcelCell).Column < ColumnIndex) continue;

                _cellIndex[i].RangeID += colAdd;
            }
        }