Example #1
0
        internal SLNumberReference Clone()
        {
            SLNumberReference nr = new SLNumberReference();
            nr.WorksheetName = this.WorksheetName;
            nr.StartRowIndex = this.StartRowIndex;
            nr.StartColumnIndex = this.StartColumnIndex;
            nr.EndRowIndex = this.EndRowIndex;
            nr.EndColumnIndex = this.EndColumnIndex;
            nr.Formula = this.Formula;
            nr.NumberingCache = this.NumberingCache.Clone();

            return nr;
        }
        internal SLNumberReference Clone()
        {
            SLNumberReference nr = new SLNumberReference();

            nr.WorksheetName    = this.WorksheetName;
            nr.StartRowIndex    = this.StartRowIndex;
            nr.StartColumnIndex = this.StartColumnIndex;
            nr.EndRowIndex      = this.EndRowIndex;
            nr.EndColumnIndex   = this.EndColumnIndex;
            nr.Formula          = this.Formula;
            nr.NumberingCache   = this.NumberingCache.Clone();

            return(nr);
        }
        private List<SLC.SLDataSeries> FillChartDataSeries(string WorksheetName, int StartRowIndex, int StartColumnIndex, int EndRowIndex, int EndColumnIndex, bool RowsAsDataSeries, bool ShowHiddenData)
        {
            List<SLC.SLDataSeries> series = new List<SLC.SLDataSeries>();
            int i, j;
            SLCell c;
            SLCellPoint pt;
            Dictionary<int, bool> HiddenRows = new Dictionary<int, bool>();
            Dictionary<int, bool> HiddenColumns = new Dictionary<int, bool>();
            Dictionary<SLCellPoint, SLCell> cellstore = new Dictionary<SLCellPoint, SLCell>();

            #region GetCells
            for (i = StartRowIndex; i <= EndRowIndex; ++i)
            {
                HiddenRows[i] = false;
            }
            for (j = StartColumnIndex; j <= EndColumnIndex; ++j)
            {
                HiddenColumns[j] = false;
            }

            bool bFound = false;
            string sWorksheetRelID = string.Empty;
            if (WorksheetName.Equals(gsSelectedWorksheetName, StringComparison.OrdinalIgnoreCase))
            {
                bFound = false;
            }
            else
            {
                bFound = false;
                foreach (SLSheet sheet in slwb.Sheets)
                {
                    if (sheet.Name.Equals(WorksheetName, StringComparison.OrdinalIgnoreCase))
                    {
                        bFound = true;
                        sWorksheetRelID = sheet.Id;
                        break;
                    }
                }
            }

            if (bFound)
            {
                Dictionary<string, SLCellPoint> cellref = new Dictionary<string, SLCellPoint>();
                for (i = StartRowIndex; i <= EndRowIndex; ++i)
                {
                    for (j = StartColumnIndex; j <= EndColumnIndex; ++j)
                    {
                        pt = new SLCellPoint(i, j);
                        cellref[SLTool.ToCellReference(i, j)] = pt;
                    }
                }

                WorksheetPart wsp = (WorksheetPart)wbp.GetPartById(sWorksheetRelID);
                Row row;
                Column column;
                Cell cell;
                if (!ShowHiddenData)
                {
                    // running it twice because Row contains Cell classes and it's easier this way
                    using (OpenXmlReader oxr = OpenXmlReader.Create(wsp))
                    {
                        while (oxr.Read())
                        {
                            if (oxr.ElementType == typeof(Row))
                            {
                                row = (Row)oxr.LoadCurrentElement();
                                if (row.RowIndex != null)
                                {
                                    foreach (var rowindex in HiddenRows.Keys)
                                    {
                                        if (row.RowIndex.Value == rowindex && row.Hidden != null && row.Hidden.Value)
                                        {
                                            HiddenRows[rowindex] = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                using (OpenXmlReader oxr = OpenXmlReader.Create(wsp))
                {
                    while (oxr.Read())
                    {
                        if (oxr.ElementType == typeof(Column))
                        {
                            if (!ShowHiddenData)
                            {
                                column = (Column)oxr.LoadCurrentElement();
                                foreach (var colindex in HiddenColumns.Keys)
                                {
                                    if (column.Min <= colindex && colindex <= column.Max && column.Hidden != null && column.Hidden.Value)
                                    {
                                        HiddenColumns[colindex] = true;
                                    }
                                }
                            }
                        }
                        else if (oxr.ElementType == typeof(Cell))
                        {
                            cell = (Cell)oxr.LoadCurrentElement();
                            if (cell.CellReference != null)
                            {
                                if (cellref.ContainsKey(cell.CellReference.Value))
                                {
                                    c = new SLCell();
                                    c.FromCell(cell);
                                    cellstore[cellref[cell.CellReference.Value]] = c.Clone();
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                SLRowProperties rp;
                SLColumnProperties cp;

                if (!ShowHiddenData)
                {
                    for (j = StartColumnIndex; j <= EndColumnIndex; ++j)
                    {
                        if (slws.ColumnProperties.ContainsKey(j))
                        {
                            cp = slws.ColumnProperties[j];
                            if (cp.Hidden) HiddenColumns[j] = true;
                        }
                    }
                }

                for (i = StartRowIndex; i <= EndRowIndex; ++i)
                {
                    if (!ShowHiddenData && slws.RowProperties.ContainsKey(i))
                    {
                        rp = slws.RowProperties[i];
                        if (rp.Hidden) HiddenRows[i] = true;
                    }

                    for (j = StartColumnIndex; j <= EndColumnIndex; ++j)
                    {
                        pt = new SLCellPoint(i, j);
                        if (slws.Cells.ContainsKey(pt))
                        {
                            cellstore[pt] = slws.Cells[pt].Clone();
                        }
                    }
                }
            }

            #endregion

            int index = 0;
            int index2 = 0;
            string sCellValue;
            string sFormatCode;
            SLC.SLDataSeries ser;
            SLC.SLStringReference sr;
            SLC.SLNumberReference nr;
            SLStyle style;

            bool bIsStringReference = true;
            // we're going to assume that the format code applies to all in the "category" cells.
            string sAxisFormatCode = string.Empty;

            SLC.SLAxisDataSourceType cat = new SLC.SLAxisDataSourceType();
            if (RowsAsDataSeries)
            {
                bIsStringReference = true;
                sAxisFormatCode = SLConstants.NumberFormatGeneral;
                pt = new SLCellPoint(StartRowIndex, StartColumnIndex + 1);
                if (cellstore.ContainsKey(pt))
                {
                    // dates are also numbers, so we lump it together
                    if (cellstore[pt].DataType == CellValues.Number)
                    {
                        bIsStringReference = false;
                        style = new SLStyle(SLConstants.OfficeThemeMajorLatinFont, SLConstants.OfficeThemeMinorLatinFont, new List<System.Drawing.Color>(), new List<System.Drawing.Color>());
                        style.FromHash(listStyle[(int)cellstore[pt].StyleIndex]);
                        this.TranslateStylesToStyleIds(ref style);
                        sAxisFormatCode = style.FormatCode;
                    }
                }

                sr = new SLC.SLStringReference();
                nr = new SLC.SLNumberReference();
                if (bIsStringReference)
                {
                    cat.UseStringReference = true;
                    sr = new SLC.SLStringReference();
                    sr.WorksheetName = WorksheetName;
                    sr.StartRowIndex = StartRowIndex;
                    sr.StartColumnIndex = StartColumnIndex + 1;
                    sr.EndRowIndex = StartRowIndex;
                    sr.EndColumnIndex = EndColumnIndex;
                    sr.Formula = SLC.SLChartTool.GetChartReferenceFormula(WorksheetName, StartRowIndex, StartColumnIndex + 1, StartRowIndex, EndColumnIndex);
                }
                else
                {
                    cat.UseNumberReference = true;
                    nr.WorksheetName = WorksheetName;
                    nr.StartRowIndex = StartRowIndex;
                    nr.StartColumnIndex = StartColumnIndex + 1;
                    nr.EndRowIndex = StartRowIndex;
                    nr.EndColumnIndex = EndColumnIndex;
                    nr.Formula = SLC.SLChartTool.GetChartReferenceFormula(WorksheetName, StartRowIndex, StartColumnIndex + 1, StartRowIndex, EndColumnIndex);
                    nr.NumberingCache.FormatCode = sAxisFormatCode;
                }

                index2 = 0;
                // if the header row is hidden, I don't know what to do...
                for (j = StartColumnIndex + 1; j <= EndColumnIndex; ++j)
                {
                    if (HiddenColumns.ContainsKey(j) && !HiddenColumns[j])
                    {
                        pt = new SLCellPoint(StartRowIndex, j);
                        sCellValue = string.Empty;
                        if (cellstore.ContainsKey(pt))
                        {
                            c = cellstore[pt];
                            sCellValue = this.GetCellTrueValue(c);

                            if (bIsStringReference)
                            {
                                sr.Points.Add(new SLC.SLStringPoint()
                                {
                                    Index = (uint)index2,
                                    NumericValue = sCellValue
                                });
                            }
                            else
                            {
                                nr.NumberingCache.Points.Add(new SLC.SLNumericPoint()
                                {
                                    Index = (uint)index2,
                                    NumericValue = sCellValue
                                });
                            }
                        }
                        ++index2;
                    }
                }

                if (bIsStringReference)
                {
                    sr.PointCount = (uint)index2;
                    cat.StringReference = sr;
                }
                else
                {
                    nr.NumberingCache.PointCount = (uint)index2;
                    cat.NumberReference = nr;
                }

                index = 0;
                for (i = StartRowIndex + 1; i <= EndRowIndex; ++i)
                {
                    if (HiddenRows.ContainsKey(i) && !HiddenRows[i])
                    {
                        ser = new SLC.SLDataSeries(SimpleTheme.listThemeColors);
                        ser.Index = (uint)index;
                        ser.Order = (uint)index;
                        ser.IsStringReference = true;

                        sr = new SLC.SLStringReference();
                        pt = new SLCellPoint(i, StartColumnIndex);
                        sCellValue = string.Empty;
                        if (cellstore.ContainsKey(pt))
                        {
                            c = cellstore[pt];
                            sCellValue = this.GetCellTrueValue(c);
                        }
                        sr.WorksheetName = WorksheetName;
                        sr.StartRowIndex = i;
                        sr.StartColumnIndex = StartColumnIndex;
                        sr.EndRowIndex = i;
                        sr.EndColumnIndex = StartColumnIndex;
                        sr.Formula = SLC.SLChartTool.GetChartReferenceFormula(WorksheetName, i, StartColumnIndex);
                        sr.PointCount = 1;
                        sr.Points.Add(new SLC.SLStringPoint() { Index = 0, NumericValue = sCellValue });
                        ser.StringReference = sr;

                        ser.AxisData = cat.Clone();

                        ser.NumberData.UseNumberReference = true;

                        nr = new SLC.SLNumberReference();
                        nr.WorksheetName = WorksheetName;
                        nr.StartRowIndex = i;
                        nr.StartColumnIndex = StartColumnIndex + 1;
                        nr.EndRowIndex = i;
                        nr.EndColumnIndex = EndColumnIndex;
                        nr.Formula = SLC.SLChartTool.GetChartReferenceFormula(WorksheetName, i, StartColumnIndex + 1, i, EndColumnIndex);
                        nr.NumberingCache.FormatCode = SLConstants.NumberFormatGeneral;
                        index2 = 0;
                        for (j = StartColumnIndex + 1; j <= EndColumnIndex; ++j)
                        {
                            if (HiddenColumns.ContainsKey(j) && !HiddenColumns[j])
                            {
                                pt = new SLCellPoint(i, j);
                                sCellValue = string.Empty;
                                sFormatCode = string.Empty;
                                if (cellstore.ContainsKey(pt))
                                {
                                    c = cellstore[pt];
                                    sCellValue = this.GetCellTrueValue(c);

                                    style = new SLStyle(SLConstants.OfficeThemeMajorLatinFont, SLConstants.OfficeThemeMinorLatinFont, new List<System.Drawing.Color>(), new List<System.Drawing.Color>());
                                    style.FromHash(listStyle[(int)c.StyleIndex]);
                                    this.TranslateStylesToStyleIds(ref style);
                                    if (style.HasNumberingFormat) sFormatCode = style.FormatCode;

                                    nr.NumberingCache.Points.Add(new SLC.SLNumericPoint()
                                    {
                                        FormatCode = sFormatCode,
                                        Index = (uint)index2,
                                        NumericValue = sCellValue
                                    });
                                }
                                ++index2;
                            }
                        }
                        nr.NumberingCache.PointCount = (uint)index2;
                        ser.NumberData.NumberReference = nr;

                        series.Add(ser);
                        ++index;
                    }
                }

                // end of rows as data series
            }
            else
            {
                bIsStringReference = true;
                sAxisFormatCode = SLConstants.NumberFormatGeneral;
                pt = new SLCellPoint(StartRowIndex + 1, StartColumnIndex);
                if (cellstore.ContainsKey(pt))
                {
                    // dates are also numbers, so we lump it together
                    if (cellstore[pt].DataType == CellValues.Number)
                    {
                        bIsStringReference = false;
                        style = new SLStyle(SLConstants.OfficeThemeMajorLatinFont, SLConstants.OfficeThemeMinorLatinFont, new List<System.Drawing.Color>(), new List<System.Drawing.Color>());
                        style.FromHash(listStyle[(int)cellstore[pt].StyleIndex]);
                        this.TranslateStylesToStyleIds(ref style);
                        sAxisFormatCode = style.FormatCode;
                    }
                }

                sr = new SLC.SLStringReference();
                nr = new SLC.SLNumberReference();
                if (bIsStringReference)
                {
                    cat.UseStringReference = true;
                    sr.WorksheetName = WorksheetName;
                    sr.StartRowIndex = StartRowIndex + 1;
                    sr.StartColumnIndex = StartColumnIndex;
                    sr.EndRowIndex = EndRowIndex;
                    sr.EndColumnIndex = StartColumnIndex;
                    sr.Formula = SLC.SLChartTool.GetChartReferenceFormula(WorksheetName, StartRowIndex + 1, StartColumnIndex, EndRowIndex, StartColumnIndex);
                }
                else
                {
                    cat.UseNumberReference = true;
                    nr.WorksheetName = WorksheetName;
                    nr.StartRowIndex = StartRowIndex + 1;
                    nr.StartColumnIndex = StartColumnIndex;
                    nr.EndRowIndex = EndRowIndex;
                    nr.EndColumnIndex = StartColumnIndex;
                    nr.Formula = SLC.SLChartTool.GetChartReferenceFormula(WorksheetName, StartRowIndex + 1, StartColumnIndex, EndRowIndex, StartColumnIndex);
                    nr.NumberingCache.FormatCode = sAxisFormatCode;
                }

                index2 = 0;
                // if the header column is hidden, I don't know what to do...
                for (i = StartRowIndex + 1; i <= EndRowIndex; ++i)
                {
                    if (HiddenRows.ContainsKey(i) && !HiddenRows[i])
                    {
                        pt = new SLCellPoint(i, StartColumnIndex);
                        sCellValue = string.Empty;
                        if (cellstore.ContainsKey(pt))
                        {
                            c = cellstore[pt];
                            sCellValue = this.GetCellTrueValue(c);

                            if (bIsStringReference)
                            {
                                sr.Points.Add(new SLC.SLStringPoint()
                                {
                                    Index = (uint)index2,
                                    NumericValue = sCellValue
                                });
                            }
                            else
                            {
                                nr.NumberingCache.Points.Add(new SLC.SLNumericPoint()
                                {
                                    Index = (uint)index2,
                                    NumericValue = sCellValue
                                });
                            }
                        }
                        ++index2;
                    }
                }

                if (bIsStringReference)
                {
                    sr.PointCount = (uint)index2;
                    cat.StringReference = sr;
                }
                else
                {
                    nr.NumberingCache.PointCount = (uint)index2;
                    cat.NumberReference = nr;
                }

                index = 0;
                for (j = StartColumnIndex + 1; j <= EndColumnIndex; ++j)
                {
                    if (HiddenColumns.ContainsKey(j) && !HiddenColumns[j])
                    {
                        ser = new SLC.SLDataSeries(SimpleTheme.listThemeColors);
                        ser.Index = (uint)index;
                        ser.Order = (uint)index;
                        ser.IsStringReference = true;

                        sr = new SLC.SLStringReference();
                        pt = new SLCellPoint(StartRowIndex, j);
                        sCellValue = string.Empty;
                        if (cellstore.ContainsKey(pt))
                        {
                            c = cellstore[pt];
                            sCellValue = this.GetCellTrueValue(c);
                        }
                        sr.WorksheetName = WorksheetName;
                        sr.StartRowIndex = StartRowIndex;
                        sr.StartColumnIndex = j;
                        sr.EndRowIndex = StartRowIndex;
                        sr.EndColumnIndex = j;
                        sr.Formula = SLC.SLChartTool.GetChartReferenceFormula(WorksheetName, StartRowIndex, j);
                        sr.PointCount = 1;
                        sr.Points.Add(new SLC.SLStringPoint() { Index = 0, NumericValue = sCellValue });
                        ser.StringReference = sr;

                        ser.AxisData = cat.Clone();

                        ser.NumberData.UseNumberReference = true;

                        nr = new SLC.SLNumberReference();
                        nr.WorksheetName = WorksheetName;
                        nr.StartRowIndex = StartRowIndex + 1;
                        nr.StartColumnIndex = j;
                        nr.EndRowIndex = EndRowIndex;
                        nr.EndColumnIndex = j;
                        nr.Formula = SLC.SLChartTool.GetChartReferenceFormula(WorksheetName, StartRowIndex + 1, j, EndRowIndex, j);
                        nr.NumberingCache.FormatCode = SLConstants.NumberFormatGeneral;
                        index2 = 0;
                        for (i = StartRowIndex + 1; i <= EndRowIndex; ++i)
                        {
                            if (HiddenRows.ContainsKey(i) && !HiddenRows[i])
                            {
                                pt = new SLCellPoint(i, j);
                                sCellValue = string.Empty;
                                sFormatCode = string.Empty;
                                if (cellstore.ContainsKey(pt))
                                {
                                    c = cellstore[pt];
                                    sCellValue = this.GetCellTrueValue(c);

                                    style = new SLStyle(SLConstants.OfficeThemeMajorLatinFont, SLConstants.OfficeThemeMinorLatinFont, new List<System.Drawing.Color>(), new List<System.Drawing.Color>());
                                    style.FromHash(listStyle[(int)c.StyleIndex]);
                                    this.TranslateStylesToStyleIds(ref style);
                                    if (style.HasNumberingFormat) sFormatCode = style.FormatCode;

                                    nr.NumberingCache.Points.Add(new SLC.SLNumericPoint()
                                    {
                                        FormatCode = sFormatCode,
                                        Index = (uint)index2,
                                        NumericValue = sCellValue
                                    });
                                }
                                ++index2;
                            }
                        }
                        nr.NumberingCache.PointCount = (uint)index2;
                        ser.NumberData.NumberReference = nr;

                        series.Add(ser);
                        ++index;
                    }
                }

                // end of columns as data series
            }

            return series;
        }