Smrf.NodeXL.ExcelTemplate.VertexWorksheetPopulator.FillVertexTable C# (CSharp) Method

FillVertexTable() protected method

protected FillVertexTable ( ListObject oVertexTable, HashSet oUniqueVertexNames ) : void
oVertexTable ListObject
oUniqueVertexNames HashSet
return void
    FillVertexTable
    (
        ListObject oVertexTable,
        HashSet<String> oUniqueVertexNames
    )
    {
        Debug.Assert(oVertexTable != null);
        Debug.Assert(oUniqueVertexNames != null);
        AssertValid();

        // There may already be some vertex names in the table.  For each
        // existing name, remove the name from the HashSet.

        Int32 iExistingRows = 0;

        Range oVertexNameRange;

        if (ExcelTableUtil.TryGetTableColumnData(oVertexTable,
            VertexTableColumnNames.VertexName, out oVertexNameRange) )
        {
            iExistingRows = oVertexNameRange.Rows.Count;

            // Read the vertex names all at once.

            Object [,] aoVertexNameValues =
                ExcelUtil.GetRangeValues(oVertexNameRange);

            // Loop through the vertices.

            for (Int32 iRowOneBased = 1; iRowOneBased <= iExistingRows;
                iRowOneBased++)
            {
                // Get the vertex name and remove it from the HashSet.

                String sVertexName;

                if ( ExcelUtil.TryGetNonEmptyStringFromCell(
                        aoVertexNameValues, iRowOneBased, 1, out sVertexName) )
                {
                    oUniqueVertexNames.Remove(sVertexName);
                }
            }
        }

        // Now create an array for the vertices that remain in the HashSet.
        // These are vertices that were in the edge table but not the vertex
        // table.

        Int32 iRowsToAdd = oUniqueVertexNames.Count;

        if (iRowsToAdd == 0)
        {
            return;
        }

        String [,] asAddedVertexNameValues = new String [iRowsToAdd, 1];

        Int32 iIndex = 0;

        foreach (String sUniqueVertexName in oUniqueVertexNames)
        {
            asAddedVertexNameValues[iIndex, 0] = sUniqueVertexName;
            iIndex++;
        }

        // The table may be empty or contain empty rows.  If so, the remaining
        // vertices should be appended after the last non-empty row.

        Int32 iLastNonEmptyRowOneBased;

        Range oDataBodyRange = oVertexTable.DataBodyRange;

        if (
            oDataBodyRange == null
            ||
            !ExcelUtil.TryGetLastNonEmptyRow(oDataBodyRange,
                out iLastNonEmptyRowOneBased)
            )
        {
            // There were no non-empty data rows in the table.  Use an offset
            // of 1 from the header row instead.

            oDataBodyRange = oVertexTable.HeaderRowRange;
            iExistingRows = 1;
        }
        else
        {
            iExistingRows = iLastNonEmptyRowOneBased - oDataBodyRange.Row + 1;
        }

        // Get the index of the vertex name column.

        ListColumn oVertexNameColumn;

        if (!ExcelTableUtil.TryGetTableColumn(oVertexTable,
            VertexTableColumnNames.VertexName, out oVertexNameColumn) )
        {
            // This can't happen, because GetRequiredTables() has
            // verified that the column exists.

            Debug.Assert(false);
        }

        Int32 iVertexNameColumnIndexOneBased = oVertexNameColumn.Index;

        Debug.Assert(oVertexTable.Parent is Worksheet);

        Worksheet oVertexWorksheet = (Worksheet)oVertexTable.Parent;

        Range oAddedVertexNameRange = oVertexWorksheet.get_Range(

            (Range)oDataBodyRange.Cells[iExistingRows + 1,
                iVertexNameColumnIndexOneBased],

            (Range)oDataBodyRange.Cells[iExistingRows + iRowsToAdd,
                iVertexNameColumnIndexOneBased]
            );

        oAddedVertexNameRange.set_Value(
            Missing.Value, asAddedVertexNameValues);
    }