Пример #1
0
        private void CreateColumnCopies(uint colFromIdx, int numOfColumns, Func <CellProxy, CellProxy> fnCreate)
        {
            Action <uint, uint> action = delegate(uint rowIdx, uint colIdx) {
                RowColumn column = new RowColumn {
                    Row    = rowIdx,
                    Column = colIdx
                };
                if (column.Column == colFromIdx)
                {
                    foreach (int num in Enumerable.Range(((int)colFromIdx) + 1, numOfColumns))
                    {
                        RowColumn column2 = new RowColumn {
                            Row    = column.Row,
                            Column = (uint)num
                        };
                        CellProxy cell = this.GetCell(column.Row, column.Column);
                        if (cell != null)
                        {
                            CellProxy c = fnCreate(cell);
                            if (c != null)
                            {
                                this.AddCellToCache(c, column2.Row, column2.Column);
                            }
                        }
                    }
                }
            };

            this.LoopCells(action);
        }
Пример #2
0
        private void CreateColumnCopies(uint colFromIdx, int numOfColumns, Func <CellProxy, CellProxy> fnCreate)
        {
            Action <uint, uint> actionCopyColumn = (rowIdx, colIdx) =>
            {
                RowColumn rcOld = new RowColumn()
                {
                    Row = rowIdx, Column = colIdx
                };
                if (rcOld.Column == colFromIdx)
                {
                    foreach (var newColIdx in Enumerable.Range((int)colFromIdx + 1, numOfColumns))
                    {
                        RowColumn rcNew = new RowColumn()
                        {
                            Row = rcOld.Row, Column = (uint)newColIdx
                        };
                        CellProxy cOld = GetCell(rcOld.Row, rcOld.Column);
                        if (cOld != null)
                        {
                            CellProxy cNew = fnCreate(cOld);
                            if (cNew != null)
                            {
                                AddCellToCache(cNew, rcNew.Row, rcNew.Column);
                            }
                        }
                    }
                }
            };

            LoopCells(actionCopyColumn);
        }
Пример #3
0
        private void AddCellToCache(CellProxy c, uint rowIdx, uint colIdx)
        {
            SortedList <uint, CellProxy> list;

            this.Modified = true;
            if (!this._cachedCells.TryGetValue(rowIdx, out list))
            {
                list = new SortedList <uint, CellProxy>();
                this._cachedCells[rowIdx] = list;
            }
            list[colIdx] = c;
        }
Пример #4
0
        private void AddCellToCache(CellProxy c, uint rowIdx, uint colIdx)
        {
            this.Modified = true;

            SortedList <uint, CellProxy> rowCache;

            if (!_cachedCells.TryGetValue(rowIdx, out rowCache))
            {
                rowCache             = new SortedList <uint, CellProxy>();
                _cachedCells[rowIdx] = rowCache;
            }
            rowCache[colIdx] = c;
        }
Пример #5
0
        private CellProxy RemoveCellFromCache(uint rowIdx, uint colIdx)
        {
            SortedList <uint, CellProxy> list;

            this.Modified = true;
            CellProxy proxy = null;

            if (this._cachedCells.TryGetValue(rowIdx, out list) && list.TryGetValue(colIdx, out proxy))
            {
                list.Remove(colIdx);
                return(proxy);
            }
            return(null);
        }
Пример #6
0
        public void RecalcCellReferences(SheetChange sheetChange)
        {
            foreach (KeyValuePair <ulong, CellProxy> pair in this.Cells())
            {
                CellProxy proxy = pair.Value;
                if (proxy.Formula != null)
                {
                    proxy.Formula.Text      = ExcelFormula.TranslateForSheetChange(proxy.Formula.Text, sheetChange, this._wsheet.Name);
                    proxy.Formula.R1        = ExcelRange.TranslateForSheetChange(proxy.Formula.R1, sheetChange, this._wsheet.Name);
                    proxy.Formula.R2        = ExcelRange.TranslateForSheetChange(proxy.Formula.R2, sheetChange, this._wsheet.Name);
                    proxy.Formula.Reference = ExcelRange.TranslateForSheetChange(proxy.Formula.Reference, sheetChange, this._wsheet.Name);
                }
            }
            List <ConditionalFormatting> list = new List <ConditionalFormatting>();

            foreach (ConditionalFormatting formatting in this.GetElements <ConditionalFormatting>())
            {
                bool flag = false;
                List <StringValue> list2 = new List <StringValue>();
                foreach (StringValue value2 in formatting.SequenceOfReferences.Items)
                {
                    string str = ExcelRange.TranslateForSheetChange(value2.Value, sheetChange, this._wsheet.Name);
                    if (!str.StartsWith("#"))
                    {
                        list2.Add(new StringValue(str));
                    }
                    else
                    {
                        list.Add(formatting);
                        flag = true;
                        break;
                    }
                }
                if (flag)
                {
                    break;
                }
                formatting.SequenceOfReferences = new ListValue <StringValue>(list2);
                foreach (Formula formula in formatting.Descendants <Formula>())
                {
                    formula.Text = ExcelFormula.TranslateForSheetChange(formula.Text, sheetChange, this._wsheet.Name);
                }
            }
            foreach (ConditionalFormatting formatting2 in list)
            {
                this.RemoveElement(formatting2);
            }
        }
Пример #7
0
        private CellProxy RemoveCellFromCache(uint rowIdx, uint colIdx)
        {
            this.Modified = true;

            CellProxy c = null;
            SortedList <uint, CellProxy> rowCache;

            if (_cachedCells.TryGetValue(rowIdx, out rowCache))
            {
                if (rowCache.TryGetValue(colIdx, out c))
                {
                    rowCache.Remove(colIdx);
                    return(c);
                }
            }
            return(null);
        }
Пример #8
0
        public CellProxy EnsureCell(uint row, uint col)
        {
            CellProxy c = null;
            SortedList <uint, CellProxy> list;

            if (!this._cachedCells.TryGetValue(row, out list))
            {
                c = new CellProxy(this);
                this.AddCellToCache(c, row, col);
                return(c);
            }
            if (!list.TryGetValue(col, out c))
            {
                c = new CellProxy(this);
                if (list != null)
                {
                    this.AddCellToCache(c, list, col);
                }
            }
            return(c);
        }
Пример #9
0
        private void CreateRowCopies(uint rowFromIdx, int numOfRows, Func <CellProxy, CellProxy> fnCreate)
        {
            SortedList <uint, CellProxy> rowCache;

            if (_cachedCells.TryGetValue(rowFromIdx, out rowCache))
            {
                Row cachedRow;
                _cachedRows.TryGetValue(rowFromIdx, out cachedRow);

                foreach (var newRowIdx in Enumerable.Range((int)rowFromIdx + 1, numOfRows))
                {
                    if (cachedRow != null)
                    {
                        Row cachedRowCopy = (Row)cachedRow.CloneNode(false);
                        cachedRowCopy.RowIndex = (uint)newRowIdx;
                        _cachedRows.Add(cachedRowCopy.RowIndex, cachedRowCopy);
                    }

                    foreach (var colIdx in rowCache.Keys)
                    {
                        RowColumn rcOld = new RowColumn()
                        {
                            Row = rowFromIdx, Column = colIdx
                        };
                        RowColumn rcNew = new RowColumn()
                        {
                            Row = (uint)newRowIdx, Column = colIdx
                        };
                        CellProxy cOld = GetCell(rcOld.Row, rcOld.Column);
                        CellProxy cNew = fnCreate(cOld);
                        if (cNew != null)
                        {
                            AddCellToCache(cNew, rcNew.Row, rcNew.Column);
                        }
                    }
                }
            }
        }
Пример #10
0
        public CellProxy EnsureCell(uint row, uint col)
        {
            CellProxy c = null;
            SortedList <uint, CellProxy> rowCache;

            if (!_cachedCells.TryGetValue(row, out rowCache))
            {
                c = new CellProxy(this);
                AddCellToCache(c, row, col);
            }
            else
            {
                if (!rowCache.TryGetValue(col, out c))
                {
                    c = new CellProxy(this);
                    if (rowCache != null)
                    {
                        AddCellToCache(c, rowCache, col);
                    }
                }
            }
            return(c);
        }
Пример #11
0
        private void CreateRowCopies(uint rowFromIdx, int numOfRows, Func <CellProxy, CellProxy> fnCreate)
        {
            SortedList <uint, CellProxy> list;

            if (this._cachedCells.TryGetValue(rowFromIdx, out list))
            {
                Row row;
                this._cachedRows.TryGetValue(rowFromIdx, out row);
                foreach (int num in Enumerable.Range(((int)rowFromIdx) + 1, numOfRows))
                {
                    if (row != null)
                    {
                        Row row2 = (Row)row.CloneNode(false);
                        row2.RowIndex = (UInt32Value)(uint)num;
                        this._cachedRows.Add((uint)row2.RowIndex, row2);
                    }
                    foreach (uint num2 in list.Keys)
                    {
                        RowColumn column = new RowColumn {
                            Row    = rowFromIdx,
                            Column = num2
                        };
                        RowColumn column2 = new RowColumn {
                            Row    = (uint)num,
                            Column = num2
                        };
                        CellProxy cell = this.GetCell(column.Row, column.Column);
                        CellProxy c    = fnCreate(cell);
                        if (c != null)
                        {
                            this.AddCellToCache(c, column2.Row, column2.Column);
                        }
                    }
                }
            }
        }
Пример #12
0
 public void Load()
 {
     if (this._wsheet.GetOWorksheetPart() != null)
     {
         Action <Cell> action = delegate(Cell cell) {
             RowColumn column = ExcelAddress.ToRowColumn((string)cell.CellReference);
             CellProxy proxy  = this.EnsureCell(column.Row, column.Column);
             if (cell.DataType != null)
             {
                 proxy.DataType = new CellValues?(cell.DataType.Value);
                 if (((CellValues)cell.DataType.Value) == CellValues.InlineString)
                 {
                     proxy.Value = cell.InlineString.Text.Text;
                 }
                 else if (cell.CellValue != null)
                 {
                     proxy.Value = cell.CellValue.Text;
                 }
                 else
                 {
                     proxy.Value = string.Empty;
                 }
             }
             else if (cell.CellValue != null)
             {
                 proxy.Value = cell.CellValue.Text;
             }
             if (cell.StyleIndex != null)
             {
                 proxy.StyleIndex = new uint?((uint)cell.StyleIndex);
             }
             if (cell.ShowPhonetic != null)
             {
                 proxy.ShowPhonetic = new bool?((bool)cell.ShowPhonetic);
             }
             if (cell.ValueMetaIndex != null)
             {
                 proxy.ValueMetaIndex = new uint?((uint)cell.ValueMetaIndex);
             }
             if (cell.CellFormula != null)
             {
                 proxy.CreateFormula();
                 proxy.Formula.Text      = cell.CellFormula.Text;
                 proxy.Formula.R1        = (string)cell.CellFormula.R1;
                 proxy.Formula.R2        = (string)cell.CellFormula.R2;
                 proxy.Formula.Reference = (string)cell.CellFormula.Reference;
                 if (cell.CellFormula.AlwaysCalculateArray != null)
                 {
                     proxy.Formula.AlwaysCalculateArray = new bool?((bool)cell.CellFormula.AlwaysCalculateArray);
                 }
                 if (cell.CellFormula.Bx != null)
                 {
                     proxy.Formula.Bx = new bool?((bool)cell.CellFormula.Bx);
                 }
                 if (cell.CellFormula.CalculateCell != null)
                 {
                     proxy.Formula.CalculateCell = new bool?((bool)cell.CellFormula.CalculateCell);
                 }
                 if (cell.CellFormula.DataTable2D != null)
                 {
                     proxy.Formula.DataTable2D = new bool?((bool)cell.CellFormula.DataTable2D);
                 }
                 if (cell.CellFormula.DataTableRow != null)
                 {
                     proxy.Formula.DataTableRow = new bool?((bool)cell.CellFormula.DataTableRow);
                 }
                 if (cell.CellFormula.FormulaType != null)
                 {
                     proxy.Formula.FormulaType = new CellFormulaValues?((CellFormulaValues)cell.CellFormula.FormulaType);
                 }
                 if (cell.CellFormula.Input1Deleted != null)
                 {
                     proxy.Formula.Input1Deleted = new bool?((bool)cell.CellFormula.Input1Deleted);
                 }
                 if (cell.CellFormula.Input2Deleted != null)
                 {
                     proxy.Formula.Input2Deleted = new bool?((bool)cell.CellFormula.Input2Deleted);
                 }
                 if (cell.CellFormula.SharedIndex != null)
                 {
                     proxy.Formula.SharedIndex = new uint?((uint)cell.CellFormula.SharedIndex);
                 }
                 proxy.Value = null;
             }
         };
         OpenXmlReader reader = OpenXmlReader.Create(this._wsheet.GetOWorksheetPart());
         bool          flag   = false;
         while (reader.Read())
         {
             if (flag && reader.IsStartElement)
             {
                 if ((reader.ElementType == typeof(Row)) && reader.IsStartElement)
                 {
                     Row row = (Row)reader.LoadCurrentElement();
                     if ((from a in row.GetAttributes()
                          let ln = a.LocalName
                                   where (ln != "r") && (ln != "spans")
                                   select a).Any <OpenXmlAttribute>())
                     {
                         this._cachedRows.Add((uint)row.RowIndex, (Row)row.CloneNode(false));
                     }
                     foreach (Cell cell in row.Elements <Cell>())
                     {
                         action(cell);
                     }
                 }
                 else if (reader.ElementType == typeof(SheetData))
                 {
                     bool isStartElement = reader.IsStartElement;
                 }
                 else if (reader.IsStartElement)
                 {
                     OpenXmlElement item = reader.LoadCurrentElement();
                     this._cachedElements.Add(item);
                 }
             }
             else if (reader.ElementType == typeof(Worksheet))
             {
                 flag = reader.IsStartElement;
             }
         }
         this.Modified = false;
     }
 }
Пример #13
0
 private void AddCellToCache(CellProxy c, SortedList <uint, CellProxy> rowCache, uint colIdx)
 {
     this.Modified    = true;
     rowCache[colIdx] = c;
 }
Пример #14
0
        public void InsertOrDeleteColumns(uint colStart, int colDelta, bool copyPreviousStyle)
        {
            Func <Column, bool>         func     = null;
            Func <CellProxy, CellProxy> fnCreate = null;

            if (colDelta != 0)
            {
                this.Modified = true;
                Action <uint, uint> action = delegate(uint rowIdx, uint colIdx) {
                    if (colIdx >= colStart)
                    {
                        CellProxy c   = this.RemoveCellFromCache(rowIdx, colIdx);
                        int       num = ((int)colIdx) + colDelta;
                        if ((num >= colStart) && (num >= 1))
                        {
                            if (colIdx >= ExcelConstraints.MaxColumns)
                            {
                                throw new InvalidOperationException("Max number of columns exceeded");
                            }
                            this.AddCellToCache(c, rowIdx, (uint)num);
                        }
                    }
                };
                if (colDelta > 0)
                {
                    this.LoopCellsReverse(action);
                }
                else
                {
                    this.LoopCells(action);
                }
                Columns firstElement = this.GetFirstElement <Columns>();
                if (firstElement != null)
                {
                    List <Column> list = new List <Column>();
                    foreach (Column column in firstElement)
                    {
                        if (column.Min >= colStart)
                        {
                            column.Min = (uint)Math.Max((long)0L, (long)(((long)((ulong)column.Min)) + colDelta));
                        }
                        if (column.Max >= colStart)
                        {
                            column.Max = (uint)Math.Max((long)0L, (long)(((long)((ulong)column.Max)) + colDelta));
                        }
                        if ((column.Min <= 0) || (column.Max < column.Min))
                        {
                            list.Add(column);
                        }
                    }
                    foreach (Column column2 in list)
                    {
                        column2.Remove();
                    }
                    if (func == null)
                    {
                        func = col => col.Max == (colStart - 1);
                    }
                    Column column3 = Enumerable.Where <Column>(firstElement.Elements <Column>(), func).FirstOrDefault <Column>();
                    if (column3 != null)
                    {
                        column3.Max = (uint)(((ulong)column3.Max) + (ulong)colDelta);
                    }
                    if (firstElement.ChildElements.Count == 0)
                    {
                        this._cachedElements.Remove(firstElement);
                    }
                }
                if (colDelta > 0)
                {
                    if (copyPreviousStyle)
                    {
                        if (fnCreate == null)
                        {
                            fnCreate = delegate(CellProxy cOld) {
                                if (cOld.StyleIndex.HasValue)
                                {
                                    return(new CellProxy(this)
                                    {
                                        StyleIndex = cOld.StyleIndex
                                    });
                                }
                                return(null);
                            };
                        }
                        this.CreateColumnCopies(colStart - 1, colDelta, fnCreate);
                    }
                    else
                    {
                        for (uint i = colStart; i < (colStart + colDelta); i++)
                        {
                            this.EnsureSingleSpanColumn(i);
                            this.DeleteSingleSpanColumn(i);
                        }
                    }
                }
            }
        }
Пример #15
0
        public void Load()
        {
            WorksheetPart wp = _wsheet.GetOWorksheetPart();

            if (wp == null)
            {
                return;
            }

            Action <Cell> readCell = (cell) =>
            {
                RowColumn rc        = ExcelAddress.ToRowColumn(cell.CellReference);
                CellProxy cellProxy = this.EnsureCell(rc.Row, rc.Column);
                if (cell.DataType != null)
                {
                    cellProxy.DataType = cell.DataType.Value;
                    if (cell.DataType.Value == CellValues.InlineString)
                    {
                        cellProxy.Value = cell.InlineString.Text.Text;
                    }
                    else
                    {
                        if (cell.CellValue != null)
                        {
                            cellProxy.Value = cell.CellValue.Text;
                        }
                        else
                        {
                            cellProxy.Value = string.Empty;
                        }
                    }
                }
                else
                {
                    if (cell.CellValue != null)
                    {
                        cellProxy.Value = cell.CellValue.Text;
                    }
                }
                if (cell.StyleIndex != null)
                {
                    cellProxy.StyleIndex = cell.StyleIndex;
                }
                if (cell.ShowPhonetic != null)
                {
                    cellProxy.ShowPhonetic = cell.ShowPhonetic;
                }
                if (cell.ValueMetaIndex != null)
                {
                    cellProxy.ValueMetaIndex = cell.ValueMetaIndex;
                }
                if (cell.CellFormula != null)
                {
                    cellProxy.CreateFormula();
                    cellProxy.Formula.Text      = cell.CellFormula.Text;
                    cellProxy.Formula.R1        = cell.CellFormula.R1;
                    cellProxy.Formula.R2        = cell.CellFormula.R2;
                    cellProxy.Formula.Reference = cell.CellFormula.Reference;
                    if (cell.CellFormula.AlwaysCalculateArray != null)
                    {
                        cellProxy.Formula.AlwaysCalculateArray = cell.CellFormula.AlwaysCalculateArray;
                    }
                    if (cell.CellFormula.Bx != null)
                    {
                        cellProxy.Formula.Bx = cell.CellFormula.Bx;
                    }
                    if (cell.CellFormula.CalculateCell != null)
                    {
                        cellProxy.Formula.CalculateCell = cell.CellFormula.CalculateCell;
                    }
                    if (cell.CellFormula.DataTable2D != null)
                    {
                        cellProxy.Formula.DataTable2D = cell.CellFormula.DataTable2D;
                    }
                    if (cell.CellFormula.DataTableRow != null)
                    {
                        cellProxy.Formula.DataTableRow = cell.CellFormula.DataTableRow;
                    }
                    if (cell.CellFormula.FormulaType != null)
                    {
                        cellProxy.Formula.FormulaType = cell.CellFormula.FormulaType;
                    }
                    if (cell.CellFormula.Input1Deleted != null)
                    {
                        cellProxy.Formula.Input1Deleted = cell.CellFormula.Input1Deleted;
                    }
                    if (cell.CellFormula.Input2Deleted != null)
                    {
                        cellProxy.Formula.Input2Deleted = cell.CellFormula.Input2Deleted;
                    }
                    if (cell.CellFormula.SharedIndex != null)
                    {
                        cellProxy.Formula.SharedIndex = cell.CellFormula.SharedIndex;
                    }

                    cellProxy.Value = null; // Don't cache/store values for formulas
                }
            };

            OpenXmlReader reader      = OpenXmlReader.Create(_wsheet.GetOWorksheetPart());
            bool          inWorksheet = false;
            bool          inSheetData = false;

            while (reader.Read())
            {
                if (inWorksheet && reader.IsStartElement)
                {
                    if (reader.ElementType == typeof(Row) && reader.IsStartElement)
                    {
                        // ----------------------------------------
                        // Scan row if anything other than RowIndex and Spans has been set,
                        // if so then cache
                        Row r = (Row)reader.LoadCurrentElement();
                        var needToCacheRow = (from a in r.GetAttributes()
                                              let ln = a.LocalName
                                                       where ln != "r" && ln != "spans"
                                                       select a).Any();

                        if (needToCacheRow)
                        {
                            _cachedRows.Add(r.RowIndex, (Row)r.CloneNode(false));
                        }
                        foreach (Cell cell in r.Elements <Cell>())
                        {
                            readCell(cell);
                        }
                        // ----------------------------------------
                    }
                    else if (reader.ElementType == typeof(SheetData))
                    {
                        inSheetData = reader.IsStartElement;
                    }
                    else if (reader.IsStartElement)
                    {
                        var e = reader.LoadCurrentElement();
                        _cachedElements.Add(e);
                    }
                }
                else if (reader.ElementType == typeof(Worksheet))
                {
                    inWorksheet = reader.IsStartElement;
                }
            }

            // Reset modified to false (loading sets it to true due to CellProxy loading)
            this.Modified = false;
        }
Пример #16
0
        private IEnumerable <OpenXmlAttribute> EnumCellProxyAttributes(uint row, uint col, CellProxy cellProxy)
        {
            OpenXmlAttribute iteratorVariable0 = new OpenXmlAttribute {
                LocalName = "r",
                Value     = RowColumn.ToAddress(row, col)
            };

            yield return(iteratorVariable0);

            if (cellProxy.DataType.HasValue)
            {
                OpenXmlAttribute iteratorVariable1 = new OpenXmlAttribute {
                    LocalName = "t",
                    Value     = this.STCellType(cellProxy.DataType.Value)
                };
                yield return(iteratorVariable1);
            }
            if (cellProxy.StyleIndex.HasValue)
            {
                OpenXmlAttribute iteratorVariable2 = new OpenXmlAttribute {
                    LocalName = "s",
                    Value     = cellProxy.StyleIndex.Value.ToString()
                };
                yield return(iteratorVariable2);
            }
        }
Пример #17
0
        private void WriteSheetData(OpenXmlWriter writer)
        {
            writer.WriteStartElement(new SheetData());
            foreach (var rowItem in _cachedCells)
            {
                uint rowIdx   = rowItem.Key;
                var  cells    = rowItem.Value;
                var  rowAttrs = EnumRowAttributes(rowIdx, cells).ToList();
                if (rowAttrs.Count > 0 || cells.Count > 0)
                {
                    writer.WriteStartElement(new Row(), rowAttrs);

                    foreach (var cellItem in cells)
                    {
                        uint      colIdx    = cellItem.Key;
                        CellProxy cellProxy = cellItem.Value;
                        writer.WriteStartElement(new Cell(), EnumCellProxyAttributes(rowIdx, colIdx, cellProxy));
                        if (cellProxy.Formula != null)
                        {
                            CellFormula cf = new CellFormula(cellProxy.Formula.Text);
                            if (cellProxy.Formula.R1 != null)
                            {
                                cf.R1 = cellProxy.Formula.R1;
                            }
                            if (cellProxy.Formula.R2 != null)
                            {
                                cf.R2 = cellProxy.Formula.R2;
                            }
                            if (cellProxy.Formula.Reference != null)
                            {
                                cf.Reference = cellProxy.Formula.Reference;
                            }

                            if (cellProxy.Formula.AlwaysCalculateArray != null)
                            {
                                cf.AlwaysCalculateArray = cellProxy.Formula.AlwaysCalculateArray;
                            }
                            if (cellProxy.Formula.Bx != null)
                            {
                                cf.Bx = cellProxy.Formula.Bx;
                            }
                            if (cellProxy.Formula.CalculateCell != null)
                            {
                                cf.CalculateCell = cellProxy.Formula.CalculateCell;
                            }
                            if (cellProxy.Formula.DataTable2D != null)
                            {
                                cf.DataTable2D = cellProxy.Formula.DataTable2D;
                            }
                            if (cellProxy.Formula.DataTableRow != null)
                            {
                                cf.DataTableRow = cellProxy.Formula.DataTableRow;
                            }
                            if (cellProxy.Formula.FormulaType != null)
                            {
                                cf.FormulaType = cellProxy.Formula.FormulaType;
                            }
                            if (cellProxy.Formula.Input1Deleted != null)
                            {
                                cf.Input1Deleted = cellProxy.Formula.Input1Deleted;
                            }
                            if (cellProxy.Formula.Input2Deleted != null)
                            {
                                cf.Input2Deleted = cellProxy.Formula.Input2Deleted;
                            }
                            if (cellProxy.Formula.SharedIndex != null)
                            {
                                cf.SharedIndex = cellProxy.Formula.SharedIndex;
                            }

                            writer.WriteElement(cf);
                        }
                        if (cellProxy.Value != null)
                        {
                            writer.WriteElement(new CellValue(cellProxy.SerializedValue));
                        }
                        writer.WriteEndElement(); // c
                    }

                    writer.WriteEndElement(); // row
                }
            }
            writer.WriteEndElement(); // sheetData
        }
Пример #18
0
        public void InsertOrDeleteColumns(uint colStart, int colDelta, bool copyPreviousStyle)
        {
            if (colDelta == 0)
            {
                return;
            }

            this.Modified = true;

            Action <uint, uint> shiftCells = (rowIdx, colIdx) =>
            {
                if (colIdx >= colStart)
                {
                    CellProxy c = RemoveCellFromCache(rowIdx, colIdx);

                    int newColIdx = (int)colIdx + colDelta;
                    // If delta is negative, cells will be not put back, i.e. deleted
                    if (newColIdx >= colStart && newColIdx >= 1)
                    {
                        if (colIdx >= ExcelConstraints.MaxColumns)
                        {
                            throw new InvalidOperationException("Max number of columns exceeded");
                        }

                        AddCellToCache(c, rowIdx, (uint)newColIdx);
                    }
                }
            };

            if (colDelta > 0)
            {
                LoopCellsReverse(shiftCells);
            }
            else
            {
                LoopCells(shiftCells);
            }

            // Adjust cached <col> elements
            Columns cols = this.GetFirstElement <Columns>();

            if (cols != null)
            {
                List <Column> colsToRemove = new List <Column>();
                foreach (Column col in cols)
                {
                    if (col.Min >= colStart)
                    {
                        col.Min = (uint)Math.Max(0, col.Min + colDelta);
                    }
                    if (col.Max >= colStart)
                    {
                        col.Max = (uint)Math.Max(0, col.Max + colDelta);
                    }
                    if (col.Min <= 0 || col.Max < col.Min)
                    {
                        colsToRemove.Add(col);
                    }
                }
                foreach (Column col in colsToRemove)
                {
                    col.Remove();
                }

                Column colPrev = (from col in cols.Elements <Column>()
                                  where col.Max == colStart - 1
                                  select col).FirstOrDefault();
                if (colPrev != null)
                {
                    colPrev.Max = (uint)(colPrev.Max + colDelta);
                }
                if (cols.ChildElements.Count == 0)
                {
                    _cachedElements.Remove(cols);
                }
            }

            if (colDelta > 0)
            {
                if (copyPreviousStyle)
                {
                    CreateColumnCopies(colStart - 1, colDelta,
                                       cOld =>
                    {
                        if (cOld.StyleIndex != null)
                        {
                            var cNew        = new CellProxy(this);
                            cNew.StyleIndex = cOld.StyleIndex;
                            return(cNew);
                        }
                        return(null);
                    }
                                       );
                }
                else
                {
                    // Ensure this column does not have a column defintion
                    // so we don't copy previous column style

                    // TODO: make this more efficient
                    for (uint ctr = colStart; ctr < colStart + colDelta; ctr++)
                    {
                        EnsureSingleSpanColumn(ctr);
                        DeleteSingleSpanColumn(ctr);
                    }
                }
            }
        }
Пример #19
0
        public void InsertOrDeleteRows(uint rowStart, int rowDelta, bool copyPreviousStyle)
        {
            if (rowDelta == 0)
            {
                return;
            }

            this.Modified = true;

            IList <uint> rowIndexes;

            if (rowDelta > 0)
            {
                rowIndexes = _cachedCells.Keys.Reverse().ToList();
            }
            else
            {
                rowIndexes = _cachedCells.Keys.ToList();
            }

            var newCellProxies = new SortedList <uint, SortedList <uint, CellProxy> >();

            foreach (uint rowIdx in rowIndexes)
            {
                uint newRowIdx;
                if (rowIdx >= rowStart)
                {
                    newRowIdx = (uint)(rowIdx + rowDelta);
                }
                else
                {
                    newRowIdx = rowIdx;
                }

                newCellProxies[newRowIdx] = _cachedCells[rowIdx];
            }
            _cachedCells = newCellProxies;

            // Adjust cached <row> elements
            IEnumerable <uint> affectedCachedRowIndexes;

            if (rowDelta > 0)
            {
                affectedCachedRowIndexes = _cachedRows.Keys.Where(k => k >= rowStart).Reverse().ToList();
            }
            else
            {
                affectedCachedRowIndexes = _cachedRows.Keys.Where(k => k >= rowStart).ToList();
            }
            foreach (var rowIdx in affectedCachedRowIndexes)
            {
                Row r         = _cachedRows[rowIdx];
                int newRowIdx = (int)r.RowIndex.Value + rowDelta;
                _cachedRows.Remove(rowIdx);

                // If delta is negative, rows will be not put back, i.e. deleted
                if (newRowIdx >= rowStart && newRowIdx >= 1)
                {
                    r.RowIndex = (uint)(newRowIdx);
                    _cachedRows[r.RowIndex] = r;
                }
            }

            if (rowDelta > 0 && copyPreviousStyle)
            {
                CreateRowCopies(rowStart - 1, rowDelta,
                                cOld =>
                {
                    if (cOld.StyleIndex != null)
                    {
                        var cNew        = new CellProxy(this);
                        cNew.StyleIndex = cOld.StyleIndex;
                        return(cNew);
                    }
                    return(null);
                }
                                );
            }
        }
Пример #20
0
 private void WriteSheetData(OpenXmlWriter writer)
 {
     writer.WriteStartElement(new SheetData());
     foreach (KeyValuePair <uint, SortedList <uint, CellProxy> > pair in this._cachedCells)
     {
         uint key = pair.Key;
         SortedList <uint, CellProxy> cells      = pair.Value;
         List <OpenXmlAttribute>      attributes = this.EnumRowAttributes(key, cells).ToList <OpenXmlAttribute>();
         if ((attributes.Count > 0) || (cells.Count > 0))
         {
             writer.WriteStartElement(new Row(), attributes);
             foreach (KeyValuePair <uint, CellProxy> pair2 in cells)
             {
                 uint      col       = pair2.Key;
                 CellProxy cellProxy = pair2.Value;
                 writer.WriteStartElement(new Cell(), this.EnumCellProxyAttributes(key, col, cellProxy));
                 if (cellProxy.Formula != null)
                 {
                     CellFormula elementObject = new CellFormula(cellProxy.Formula.Text);
                     if (cellProxy.Formula.R1 != null)
                     {
                         elementObject.R1 = cellProxy.Formula.R1;
                     }
                     if (cellProxy.Formula.R2 != null)
                     {
                         elementObject.R2 = cellProxy.Formula.R2;
                     }
                     if (cellProxy.Formula.Reference != null)
                     {
                         elementObject.Reference = cellProxy.Formula.Reference;
                     }
                     if (cellProxy.Formula.AlwaysCalculateArray.HasValue)
                     {
                         bool?alwaysCalculateArray = cellProxy.Formula.AlwaysCalculateArray;
                         elementObject.AlwaysCalculateArray = alwaysCalculateArray.HasValue ? ((BooleanValue)alwaysCalculateArray.GetValueOrDefault()) : null;
                     }
                     if (cellProxy.Formula.Bx.HasValue)
                     {
                         bool?bx = cellProxy.Formula.Bx;
                         elementObject.Bx = bx.HasValue ? ((BooleanValue)bx.GetValueOrDefault()) : null;
                     }
                     if (cellProxy.Formula.CalculateCell.HasValue)
                     {
                         bool?calculateCell = cellProxy.Formula.CalculateCell;
                         elementObject.CalculateCell = calculateCell.HasValue ? ((BooleanValue)calculateCell.GetValueOrDefault()) : null;
                     }
                     if (cellProxy.Formula.DataTable2D.HasValue)
                     {
                         bool?nullable8 = cellProxy.Formula.DataTable2D;
                         elementObject.DataTable2D = nullable8.HasValue ? ((BooleanValue)nullable8.GetValueOrDefault()) : null;
                     }
                     if (cellProxy.Formula.DataTableRow.HasValue)
                     {
                         bool?dataTableRow = cellProxy.Formula.DataTableRow;
                         elementObject.DataTableRow = dataTableRow.HasValue ? ((BooleanValue)dataTableRow.GetValueOrDefault()) : null;
                     }
                     if (cellProxy.Formula.FormulaType.HasValue)
                     {
                         CellFormulaValues?formulaType = cellProxy.Formula.FormulaType;
                         elementObject.FormulaType = formulaType.HasValue ? ((EnumValue <CellFormulaValues>)formulaType.GetValueOrDefault()) : null;
                     }
                     if (cellProxy.Formula.Input1Deleted.HasValue)
                     {
                         bool?nullable14 = cellProxy.Formula.Input1Deleted;
                         elementObject.Input1Deleted = nullable14.HasValue ? ((BooleanValue)nullable14.GetValueOrDefault()) : null;
                     }
                     if (cellProxy.Formula.Input2Deleted.HasValue)
                     {
                         bool?nullable16 = cellProxy.Formula.Input2Deleted;
                         elementObject.Input2Deleted = nullable16.HasValue ? ((BooleanValue)nullable16.GetValueOrDefault()) : null;
                     }
                     if (cellProxy.Formula.SharedIndex.HasValue)
                     {
                         uint?sharedIndex = cellProxy.Formula.SharedIndex;
                         elementObject.SharedIndex = sharedIndex.HasValue ? ((UInt32Value)sharedIndex.GetValueOrDefault()) : null;
                     }
                     writer.WriteElement(elementObject);
                 }
                 if (cellProxy.Value != null)
                 {
                     writer.WriteElement(new CellValue(cellProxy.SerializedValue));
                 }
                 writer.WriteEndElement();
             }
             writer.WriteEndElement();
         }
     }
     writer.WriteEndElement();
 }
Пример #21
0
        private IEnumerable <OpenXmlAttribute> EnumCellProxyAttributes(uint row, uint col, CellProxy cellProxy)
        {
            yield return(new OpenXmlAttribute()
            {
                LocalName = "r", Value = RowColumn.ToAddress(row, col)
            });

            if (cellProxy.DataType != null)
            {
                yield return(new OpenXmlAttribute()
                {
                    LocalName = "t", Value = STCellType((CellValues)cellProxy.DataType)
                });
            }
            if (cellProxy.StyleIndex != null)
            {
                yield return(new OpenXmlAttribute()
                {
                    LocalName = "s", Value = cellProxy.StyleIndex.Value.ToString()
                });
            }
        }