Exemplo n.º 1
0
        WriteAllSettings()
        {
            AssertValid();
            Debug.Assert(m_oSettings != null);

            ListObject oPerWorkbookSettingsTable;

            if (!TryGetPerWorkbookSettingsTable(out oPerWorkbookSettingsTable))
            {
                return;
            }

            // Clear the table.

            ExcelUtil.ClearTable(oPerWorkbookSettingsTable);

            // Attempt to get the optional table columns that contain the settings.

            Range oNameColumnData, oValueColumnData;

            if (
                !ExcelUtil.TryGetTableColumnData(oPerWorkbookSettingsTable,
                                                 PerWorkbookSettingsTableColumnNames.Name, out oNameColumnData)
                ||
                !ExcelUtil.TryGetTableColumnData(oPerWorkbookSettingsTable,
                                                 PerWorkbookSettingsTableColumnNames.Value,
                                                 out oValueColumnData)
                )
            {
                return;
            }

            // Copy the settings to arrays.

            Int32 iSettings = m_oSettings.Count;

            Object [,] aoNameColumnValues =
                ExcelUtil.GetSingleColumn2DArray(iSettings);

            Object [,] aoValueColumnValues =
                ExcelUtil.GetSingleColumn2DArray(iSettings);

            Int32 i = 1;

            foreach (KeyValuePair <String, Object> oKeyValuePair in m_oSettings)
            {
                aoNameColumnValues[i, 1]  = oKeyValuePair.Key;
                aoValueColumnValues[i, 1] = oKeyValuePair.Value;
                i++;
            }

            // Write the arrays to the columns.

            ExcelUtil.SetRangeValues(oNameColumnData, aoNameColumnValues);
            ExcelUtil.SetRangeValues(oValueColumnData, aoValueColumnValues);
        }
Exemplo n.º 2
0
        WriteGraphMetricColumnsToWorkbook
        (
            GraphMetricColumn [] graphMetricColumns,
            Microsoft.Office.Interop.Excel.Workbook workbook
        )
        {
            Debug.Assert(graphMetricColumns != null);
            Debug.Assert(workbook != null);
            AssertValid();

            // (Note: Don't sort grapMetricColumns by worksheet name/table name in
            // an effort to minimize worksheet switches in the code below.  That
            // would interfere with the column order specified by the
            // IGraphMetricCalculator2 implementations.

            // Create a dictionary of tables that have been written to.  The key is
            // the worksheet name + table name, and the value is a WrittenTableInfo
            // object that contains information about the table.

            Dictionary <String, WrittenTableInfo> oWrittenTables =
                new Dictionary <String, WrittenTableInfo>();

            // Loop through the columns.

            String     sCurrentWorksheetPlusTable = String.Empty;
            ListObject oTable = null;

            foreach (GraphMetricColumn oGraphMetricColumn in graphMetricColumns)
            {
                String sThisWorksheetPlusTable = oGraphMetricColumn.WorksheetName
                                                 + oGraphMetricColumn.TableName;

                if (sThisWorksheetPlusTable != sCurrentWorksheetPlusTable)
                {
                    // This is a different table.  Get its ListObject.

                    if (!ExcelUtil.TryGetTable(workbook,
                                               oGraphMetricColumn.WorksheetName,
                                               oGraphMetricColumn.TableName, out oTable))
                    {
                        // The table couldn't be found.

                        continue;
                    }

                    sCurrentWorksheetPlusTable = sThisWorksheetPlusTable;
                }

                WrittenTableInfo oWrittenTableInfo;

                if (!oWrittenTables.TryGetValue(sThisWorksheetPlusTable,
                                                out oWrittenTableInfo))
                {
                    // Show all the table's columns.  If a graph metric column
                    // isn't visible, it can't be written to.

                    ExcelHiddenColumns oExcelHiddenColumns =
                        ExcelColumnHider.ShowHiddenColumns(oTable);

                    oWrittenTableInfo               = new WrittenTableInfo();
                    oWrittenTableInfo.Table         = oTable;
                    oWrittenTableInfo.HiddenColumns = oExcelHiddenColumns;
                    oWrittenTableInfo.Cleared       = false;

                    oWrittenTables.Add(sThisWorksheetPlusTable, oWrittenTableInfo);
                }

                // Apparent Excel bug: Adding a column when the header row is not
                // the default row height increases the header row height.  Work
                // around this by saving the height and restoring it below.

                Double dHeaderRowHeight = (Double)oTable.HeaderRowRange.RowHeight;

                // Write the column.

                Debug.Assert(oTable != null);

                if (oGraphMetricColumn is GraphMetricColumnWithID)
                {
                    WriteGraphMetricColumnWithIDToWorkbook(
                        (GraphMetricColumnWithID)oGraphMetricColumn, oTable);
                }
                else if (oGraphMetricColumn is GraphMetricColumnOrdered)
                {
                    if (!oWrittenTableInfo.Cleared)
                    {
                        // GraphMetricColumnOrdered columns require that the table
                        // be cleared before any graph metric values are written to
                        // it.

                        ExcelUtil.ClearTable(oTable);
                        oWrittenTableInfo.Cleared = true;

                        // Clear AutoFiltering, which interferes with writing an
                        // ordered list to the column.

                        ExcelUtil.ClearTableAutoFilters(oTable);
                    }

                    WriteGraphMetricColumnOrderedToWorkbook(
                        (GraphMetricColumnOrdered)oGraphMetricColumn, oTable);
                }
                else
                {
                    Debug.Assert(false);
                }

                oTable.HeaderRowRange.RowHeight = dHeaderRowHeight;
            }

            // Restore any hidden columns in the tables that were written to.

            foreach (KeyValuePair <String, WrittenTableInfo> oKeyValuePair in
                     oWrittenTables)
            {
                WrittenTableInfo oWrittenTableInfo = oKeyValuePair.Value;

                ExcelColumnHider.RestoreHiddenColumns(oWrittenTableInfo.Table,
                                                      oWrittenTableInfo.HiddenColumns);
            }
        }