MergeDuplicateEdges ( Microsoft.Office.Interop.Excel.Workbook workbook ) { Debug.Assert(workbook != null); AssertValid(); ListObject oEdgeTable; if ( !ExcelUtil.TryGetTable(workbook, WorksheetNames.Edges, TableNames.Edges, out oEdgeTable) || oEdgeTable.DataBodyRange == null ) { // (The DataBodyRange test catches the odd case where the user // deletes the first data row of the table. It looks like the row // is still there, but it's not. Continuing with a null // DataBodyRange can cause a variety of problems.) return; } ExcelUtil.ActivateWorksheet(oEdgeTable); // Clear AutoFiltering, which would make this code much more // complicated. ExcelUtil.ClearTableAutoFilters(oEdgeTable); // Get the vertex name data. Range oVertex1NameData, oVertex2NameData; Object [,] aoVertex1NameValues, aoVertex2NameValues; if ( !ExcelUtil.TryGetTableColumnDataAndValues(oEdgeTable, EdgeTableColumnNames.Vertex1Name, out oVertex1NameData, out aoVertex1NameValues) || !ExcelUtil.TryGetTableColumnDataAndValues(oEdgeTable, EdgeTableColumnNames.Vertex2Name, out oVertex2NameData, out aoVertex2NameValues) ) { return; } // Add an edge weight column if it doesn't already exist. ListColumn oEdgeWeightColumn; if ( !ExcelUtil.TryGetTableColumn(oEdgeTable, EdgeTableColumnNames.EdgeWeight, out oEdgeWeightColumn) && !ExcelUtil.TryAddTableColumn(oEdgeTable, EdgeTableColumnNames.EdgeWeight, ExcelUtil.AutoColumnWidth, null, out oEdgeWeightColumn) ) { return; } // Get the edge weight data. Range oEdgeWeightData; Object [,] aoEdgeWeightValues; if (!ExcelUtil.TryGetTableColumnDataAndValues(oEdgeTable, EdgeTableColumnNames.EdgeWeight, out oEdgeWeightData, out aoEdgeWeightValues)) { return; } // Determine whether the graph is directed. PerWorkbookSettings oPerWorkbookSettings = new PerWorkbookSettings(workbook); Boolean bGraphIsDirected = (oPerWorkbookSettings.GraphDirectedness == GraphDirectedness.Directed); // Now that the required information has been gathered, do the actual // merge. MergeDuplicateEdges(oEdgeTable, oVertex1NameData, aoVertex1NameValues, oVertex2NameData, aoVertex2NameValues, oEdgeWeightColumn, oEdgeWeightData, aoEdgeWeightValues, bGraphIsDirected); oEdgeTable.HeaderRowRange.Select(); }
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); } }