WriteGraphMetricColumnWithIDToWorkbook ( GraphMetricColumnWithID oGraphMetricColumnWithID, ListObject oTable ) { Debug.Assert(oGraphMetricColumnWithID != null); Debug.Assert(oTable != null); AssertValid(); // Get the required column information. Range oVisibleColumnData, oIDColumnData; if (!TryGetRequiredColumnWithIDInformation(oGraphMetricColumnWithID, oTable, out oVisibleColumnData, out oIDColumnData)) { return; } // Store the column's GraphMetricValueWithID objects in a dictionary. // The key is the GraphMetricValueWithID.RowID and the value is the // GraphMetricValueWithID. Dictionary <Int32, GraphMetricValueWithID> oIDDictionary = new Dictionary <Int32, GraphMetricValueWithID>(); foreach (GraphMetricValueWithID oGraphMetricValueWithID in oGraphMetricColumnWithID.GraphMetricValuesWithID) { oIDDictionary.Add(oGraphMetricValueWithID.RowID, oGraphMetricValueWithID); } Debug.Assert(oTable.Parent is Worksheet); Worksheet oWorksheet = (Worksheet)oTable.Parent; Int32 iIDColumnNumberOneBased = oIDColumnData.Column; // Loop through the areas, and split each area into subranges if the // area contains too many rows. foreach (Range oColumnSubrange in ExcelRangeSplitter.SplitRange(oVisibleColumnData)) { Int32 iRows = oColumnSubrange.Rows.Count; Range oIDColumnSubrange = ExcelRangeSplitter.GetParallelSubrange( oColumnSubrange, iIDColumnNumberOneBased); Debug.Assert(oIDColumnSubrange.Rows.Count == iRows); Object [,] aoColumnValues = ExcelUtil.GetSingleColumn2DArray(iRows); Object [,] aoIDColumnValues = ExcelUtil.GetRangeValues(oIDColumnSubrange); // Loop through the rows. for (Int32 iRowOneBased = 1; iRowOneBased <= iRows; iRowOneBased++) { String sID; Int32 iID; // Get the ID stored in the row. if ( !ExcelUtil.TryGetNonEmptyStringFromCell( aoIDColumnValues, iRowOneBased, 1, out sID) || !Int32.TryParse(sID, out iID) ) { continue; } // Is the ID one of the IDs specified within the // GraphMetricColumn object? GraphMetricValueWithID oGraphMetricValueWithID; if (!oIDDictionary.TryGetValue(iID, out oGraphMetricValueWithID)) { // No. continue; } // Set the column cell in this row to the specified value. aoColumnValues[iRowOneBased, 1] = oGraphMetricValueWithID.Value; } oColumnSubrange.set_Value(Missing.Value, aoColumnValues); if (oGraphMetricColumnWithID.ConvertUrlsToHyperlinks) { ExcelUtil.ConvertUrlsToHyperlinks(oColumnSubrange); } } }
ImportWorkbookIntoGraph ( Worksheet oSourceWorksheet, Range oNonEmptySourceRange, Int32 iColumnNumberToUseForVertex1OneBased, Int32 iColumnNumberToUseForVertex2OneBased, ICollection <Int32> oEdgeColumnNumbersToImportOneBased, ICollection <Int32> oVertex1ColumnNumbersToImportOneBased, ICollection <Int32> oVertex2ColumnNumbersToImportOneBased, Boolean bSourceColumnsHaveHeaders ) { Debug.Assert(oSourceWorksheet != null); Debug.Assert(oNonEmptySourceRange != null); Debug.Assert(iColumnNumberToUseForVertex1OneBased >= 1); Debug.Assert(iColumnNumberToUseForVertex2OneBased >= 1); Debug.Assert(oEdgeColumnNumbersToImportOneBased != null); Debug.Assert(oVertex1ColumnNumbersToImportOneBased != null); Debug.Assert(oVertex2ColumnNumbersToImportOneBased != null); AssertValid(); String [] asWorkbookColumnNames = GetWorkbookColumnNames( oSourceWorksheet, oNonEmptySourceRange, bSourceColumnsHaveHeaders); Int32 iColumns = oNonEmptySourceRange.Columns.Count; Int32 iFirstNonEmptyColumnOneBased = oNonEmptySourceRange.Columns.Column; if (bSourceColumnsHaveHeaders) { // Skip the header row. if (oNonEmptySourceRange.Rows.Count < 2) { OnInvalidSourceWorkbook( "If the columns in the other workbook have headers, then" + " there must be at least two rows.", oSourceWorksheet, 1, 1 ); } ExcelUtil.OffsetRange(ref oNonEmptySourceRange, 1, 0); ExcelUtil.ResizeRange(ref oNonEmptySourceRange, oNonEmptySourceRange.Rows.Count - 1, iColumns); } IGraph oGraph = new Graph(GraphDirectedness.Undirected); IVertexCollection oVertices = oGraph.Vertices; IEdgeCollection oEdges = oGraph.Edges; // The key is a vertex name and the value is the corresponding IVertex // object. Dictionary <String, IVertex> oVertexNameDictionary = new Dictionary <String, IVertex>(); foreach (Range oSubrange in ExcelRangeSplitter.SplitRange(oNonEmptySourceRange, 500)) { Object [,] oSubrangeValues = ExcelUtil.GetRangeValues(oSubrange); Int32 iSubrangeRows = oSubrangeValues.GetUpperBound(0); for (Int32 iRowOneBased = 1; iRowOneBased <= iSubrangeRows; iRowOneBased++) { String sVertex1Name, sVertex2Name; if ( !ExcelUtil.TryGetNonEmptyStringFromCell(oSubrangeValues, iRowOneBased, iColumnNumberToUseForVertex1OneBased - iFirstNonEmptyColumnOneBased + 1, out sVertex1Name) || !ExcelUtil.TryGetNonEmptyStringFromCell(oSubrangeValues, iRowOneBased, iColumnNumberToUseForVertex2OneBased - iFirstNonEmptyColumnOneBased + 1, out sVertex2Name) ) { continue; } IVertex oVertex1 = WorksheetReaderBase.VertexNameToVertex( sVertex1Name, oVertices, oVertexNameDictionary); IVertex oVertex2 = WorksheetReaderBase.VertexNameToVertex( sVertex2Name, oVertices, oVertexNameDictionary); IEdge oEdge = oEdges.Add(oVertex1, oVertex2); // Add the edge and vertex attributes. AddAttributeValuesToEdgeOrVertex(asWorkbookColumnNames, oSubrangeValues, iRowOneBased, iFirstNonEmptyColumnOneBased, oEdgeColumnNumbersToImportOneBased, oEdge); AddAttributeValuesToEdgeOrVertex(asWorkbookColumnNames, oSubrangeValues, iRowOneBased, iFirstNonEmptyColumnOneBased, oVertex1ColumnNumbersToImportOneBased, oVertex1); AddAttributeValuesToEdgeOrVertex(asWorkbookColumnNames, oSubrangeValues, iRowOneBased, iFirstNonEmptyColumnOneBased, oVertex2ColumnNumbersToImportOneBased, oVertex2); } } // Store metadata on the graph indicating the sets of keys that may be // present on the graph's edges and vertices. oGraph.SetValue(ReservedMetadataKeys.AllEdgeMetadataKeys, GetColumnNamesToImport(oNonEmptySourceRange, asWorkbookColumnNames, oEdgeColumnNumbersToImportOneBased) ); List <Int32> oVertexColumnNumbersToImportOneBased = new List <Int32>(); oVertexColumnNumbersToImportOneBased.AddRange( oVertex1ColumnNumbersToImportOneBased); oVertexColumnNumbersToImportOneBased.AddRange( oVertex2ColumnNumbersToImportOneBased); oGraph.SetValue(ReservedMetadataKeys.AllVertexMetadataKeys, GetColumnNamesToImport(oNonEmptySourceRange, asWorkbookColumnNames, oVertexColumnNumbersToImportOneBased) ); return(oGraph); }