ReadVertexNames
(
Microsoft.Office.Interop.Excel.Worksheet oSourceWorksheet
)
{
Debug.Assert(oSourceWorksheet != null);
AssertValid();
String sA1String;
if ( ExcelUtil.TryGetNonEmptyStringFromCell(oSourceWorksheet, 1, 1,
out sA1String) )
{
OnInvalidSourceWorkbook(
"A matrix workbook that has vertex names must have an empty A1"
+ " cell.",
oSourceWorksheet, 1, 1
);
}
// Get the row of vertex names, which is the range from B1 to the
// rightmost contiguous, non-empty cell.
Object [,] aoVertexNames;
if ( !ExcelUtil.TryGetContiguousValuesInRow(oSourceWorksheet, 1, 2,
out aoVertexNames) )
{
OnInvalidSourceWorkbook(
"A matrix workbook that has vertex names must have the first"
+ " vertex name in B1.",
oSourceWorksheet, 1, 2
);
}
Int32 iVertexNames = aoVertexNames.GetUpperBound(1);
String [] asVertexNames = new String[iVertexNames];
// Read the vertex names.
for (Int32 iColumnOneBased = 1; iColumnOneBased <= iVertexNames;
iColumnOneBased++)
{
if ( !ExcelUtil.TryGetNonEmptyStringFromCell( aoVertexNames,
1, iColumnOneBased,
out asVertexNames[iColumnOneBased - 1] ) )
{
OnInvalidSourceWorkbook(SpacesOnlyMessage, oSourceWorksheet,
1, iColumnOneBased + 1);
}
}
// Get the column of vertex names, which is the range from A2 to the
// bottommost contiguous, non-empty cell.
if ( !ExcelUtil.TryGetContiguousValuesInColumn(oSourceWorksheet, 2, 1,
out aoVertexNames) )
{
OnInvalidSourceWorkbook(
"A matrix workbook that has vertex names must have the first"
+ " vertex name in A2.",
oSourceWorksheet, 2, 1
);
}
Int32 iVertexNamesInColumnA = aoVertexNames.GetUpperBound(0);
if (iVertexNamesInColumnA != iVertexNames)
{
OnInvalidSourceWorkbook( String.Format(
"A matrix workbook that has vertex names must have the same"
+ " number of names in row 1 as in column A. It looks like"
+ " there are {0} {1} in row 1 and {2} {3} in column A."
,
iVertexNames,
StringUtil.MakePlural("name", iVertexNames),
iVertexNamesInColumnA,
StringUtil.MakePlural("name", iVertexNamesInColumnA)
),
oSourceWorksheet, iVertexNamesInColumnA + 1, 1
);
}
// Compare the vertex names in row 1 to the names in column A.
for (Int32 iRowOneBased = 1; iRowOneBased <= iVertexNames;
iRowOneBased++)
{
String sVertexName;
if ( !ExcelUtil.TryGetNonEmptyStringFromCell(aoVertexNames,
iRowOneBased, 1, out sVertexName) )
{
OnInvalidSourceWorkbook(SpacesOnlyMessage, oSourceWorksheet,
iRowOneBased + 1, 1);
}
if ( sVertexName != asVertexNames[iRowOneBased - 1] )
{
OnInvalidSourceWorkbook( String.Format(
"The vertex name in {0} does not match the vertex name"
+ " in {1}. The vertex names in row 1 must exactly match"
+ " the vertex names in column A."
,
ExcelUtil.GetCellAddress(oSourceWorksheet,
iRowOneBased + 1, 1),
ExcelUtil.GetCellAddress(oSourceWorksheet,
1, iRowOneBased + 1)
),
oSourceWorksheet, iRowOneBased + 1, 1
);
}
}
aoVertexNames = null;
// Check for duplicate names. Why not use a HashSet from the start
// instead of an array? Because the elements need to be ordered as
// they appear in the workbook, and a HashSet doesn't provide ordering.
HashSet<String> oDuplicateNameChecker = new HashSet<String>();
for (Int32 i = 0; i < iVertexNames; i++)
{
String sVertexName = asVertexNames[i];
if ( oDuplicateNameChecker.Contains(sVertexName) )
{
OnInvalidSourceWorkbook( String.Format(
"The vertex name in {0} is a duplicate. Vertex names must"
+ " be unique."
,
ExcelUtil.GetCellAddress(oSourceWorksheet,
1, i + 2)
),
oSourceWorksheet, 1, i + 2
);
}
oDuplicateNameChecker.Add(sVertexName);
}
return (asVertexNames);
}