I want to compare two list of row data. Right now I want to see if the two list contain the same title in their respective cell values.
What methods with using Smartsheet API C# could I use to sort through the list and compare each select element in each row?
I already have a column name table to search for the column name and reference the actual column id. But I can not seem to fathom how?
Any input would be helpful and I'm sorry if I sound plain dumb but I usually do not ask for help.
I have two sheets in Smartsheet. One sheet contains all the data that is given and as it goes through a process of acceptance or rejection. If completely accepted it is given a status of "Moved to Project". When the code runs it will place all rows with that status to a List that will then be used to move and compare against other list.
The Moved to Project List will be compared to our Project Management Active List.
I am stuck at trying to compare cell values through the API and maybe I'm just looking at it wrong. I've tried Enum Except to compare list but it is not working and I'm thinking I will need to create a nested loop to sort through and compare each element.
foreach (Row row in rowsToCompare)
{
Cell PMOPName = getPMOCellByColumnName(row, "Project Name");
foreach (Row innerrow in rowsToMove)
{
Cell MainTitle = getCellByColumnName(innerrow, "Title");
if (PMOPName.DisplayValue == MainTitle.DisplayValue)
{
Console.WriteLine("Yes");
}
else
Console.WriteLine("No");
}
}
static Cell getCellByColumnName(Row row, string columnName)
{
return row.Cells.FirstOrDefault(cell => cell.ColumnId ==
columnMap[columnName]);
}
static Cell getPMOCellByColumnName(Row row, string columnName)
{
return row.Cells.FirstOrDefault(cell => cell.ColumnId ==
columnMapPMO[columnName]);
}
}
Whenever there is a match of title and project name it should output yes and if not a no.
But instead I get a Unhandled Exception: System.ArgumentNullException: Value cannot be null.
I've pinpointed it to the nested loop. I'm sure I just did something stupid.
EDIT: So this is the definition of the map and how it get it's data.
static Dictionary<string, long> columnMap = new Dictionary<string, long>();
static Dictionary<string, long> columnMapPMO = new Dictionary<string,
long();
// Build column map for later reference
foreach (Column column in sheet.Columns)
columnMap.Add(column.Title, (long)column.Id);
foreach (Column column in pmosheet.Columns)
columnMapPMO.Add(column.Title, (long)column.Id);
EDIT 2: Confirming with Tim the code works but in my instance it is still coming up with an error so I will place the code that I currently have as a whole to see if possible the other functions could be causing issues.
static void Main(string[] args)
{
SmartsheetClient ss = new SmartsheetBuilder()
// TODO: Set your API access in environment variable
SMARTSHEET_ACCESS_TOKEN or else here
.SetAccessToken(token.AccessToken)
.Build();
var sheet = ss.SheetResources.GetSheet(
sheetId, // long sheetId
null, // IEnumerable<SheetLevelInclusion>
includes
null, // IEnumerable<SheetLevelExclusion>
excludes
null, // IEnumerable<long> rowIds
null, // IEnumerable<int> rowNumbers
null, // IEnumerable<long> columnIds
null, // Nullable<long> pageSize
null // Nullable<long> page
);
var pmosheet = ss.SheetResources.GetSheet(
copyId,
null,
null,
null,
null,
null,
null,
null
);
// Build column map for later reference
foreach (Column column in sheet.Columns)
columnMap.Add(column.Title, (long)column.Id);
foreach (Column column in pmosheet.Columns)
columnMapPMO.Add(column.Title, (long)column.Id);
// Accumulate rows needing update and archive here
List<Row> rowsToMove = new List<Row>();
List<Row> rowsToArchive = new List<Row>();
List<Row> rowsToCompare = new List<Row>();
//Loops through the Ideation Sheet and execute function to evaluate
//each row and add those row to the move list.
foreach (Row row in sheet.Rows)
{
Row rowToMove = evaluateRowAndBuildUpdates(row);
if (rowToMove != null)
{
rowsToMove.Add(rowToMove);
}
}
Console.WriteLine("\n");
foreach (Row row in pmosheet.Rows)
{
Row rowtoCompare = compareRowandCopy(row);
if (rowtoCompare != null)
rowsToCompare.Add(rowtoCompare);
}
Console.WriteLine("\n");
foreach (Row innerrow in rowsToMove)
{
Cell MainTitle = getCellByColumnName(innerrow, "Title");
foreach (Row row in rowsToCompare)
{
Cell PMOPName = getPMOCellByColumnName(row, "Project Name");
if (PMOPName.DisplayValue == MainTitle.DisplayValue)
{
Console.WriteLine("Yes");
break;
}
else
Console.WriteLine("No");
}
}
System.Environment.Exit(1); //End of Program
}
static Row evaluateRowAndBuildUpdates(Row sourceRow)
{
Row rowToUpdate = null;
// Find cell we want to examine
Cell statusCell = getCellByColumnName(sourceRow, "Status");
if (statusCell.DisplayValue == "Moved to Project")
{
Cell remainingCell = getCellByColumnName(sourceRow, "Status");
Cell titleCell = getCellByColumnName(sourceRow, "Title");
if (remainingCell.DisplayValue == "Moved to Project")
{
rowToUpdate = new Row
{
Id = sourceRow.Id,
};
Console.WriteLine("Ideation");
}
Console.WriteLine(titleCell.DisplayValue + " ID: " +
sourceRow.Id.ToString());
}
return rowToUpdate;
}
static Row compareRowandCopy(Row sourceRow)
{
Row rowToCopy = null;
Cell pmoStatusCell = getPMOCellByColumnName(sourceRow, "Project
Name");
if (pmoStatusCell.DisplayValue != null)
{
rowToCopy = new Row
{
Id = sourceRow.Id,
};
}
Console.WriteLine("PMO");
Console.WriteLine(pmoStatusCell.DisplayValue + " ID: " +
sourceRow.Id.ToString());
return rowToCopy;
}
static Cell getCellByColumnName(Row row, string columnName)
{
return row.Cells.FirstOrDefault(cell => cell.ColumnId ==
columnMap[columnName]);
}
static Cell getPMOCellByColumnName(Row row, string columnName)
{
return row.Cells.FirstOrDefault(cell => cell.ColumnId ==
columnMapPMO[columnName]);
}