我想比较两个行数据列表。现在,我想看看两个列表在各自的单元格值中是否包含相同的标题。
我可以使用哪些方法使用Smartsheet API C#对列表进行排序并比较每一行中的每个选择元素?
我已经有一个列名表来搜索列名并引用实际的列ID。但是我似乎无法理解如何?
任何输入都会有所帮助,如果我听起来很笨,但我通常不寻求帮助,对不起。
我在Smartsheet中有两张纸。一张纸包含所有给出的数据以及经过接受或拒绝过程的所有数据。如果被完全接受,则其状态为“已移至项目”。代码运行时,会将具有该状态的所有行放入一个列表,然后将其用于移动并与其他列表进行比较。
已移至项目列表将与我们的项目管理活动列表进行比较。
我坚持尝试通过API比较单元格值,也许我只是看错了。我已经尝试使用Enum Except来比较列表,但是它不起作用,并且我想我需要创建一个嵌套循环来对每个元素进行排序和比较。
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]);
}
}
只要标题和项目名称匹配,它应该输出yes,否则输出no。
但是相反,我得到了一个未处理的异常:System.ArgumentNullException:值不能为空。
我已将其精确定位到嵌套循环。我确定我只是做了一些愚蠢的事情。
编辑: 这就是地图的定义及其获取数据的方式。
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);
编辑2:使用Tim确认代码可以正常工作,但在我的情况下仍然出现错误,因此,我将放置当前整体上的代码,以查看其他功能是否可能引起问题。 / p>
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]);
}
答案 0 :(得分:2)
好,我有两张纸,项目纸看起来像这样: 包含要插入的行的工作表如下所示: 这是代码:
using System;
using System.Collections.Generic;
// Add nuget reference to smartsheet-csharp-sdk (https://www.nuget.org/packages/smartsheet-csharp-sdk/)
using Smartsheet.Api;
using Smartsheet.Api.Models;
using System.Linq;
namespace sdk_csharp_sample
{
class Program
{
static Dictionary<string, long> columnMap = new Dictionary<string, long>();
static Dictionary<string, long> columnMapPMO = new Dictionary<string, long>();
static void Main(string[] args)
{
// Initialize client
SmartsheetClient ss = new SmartsheetBuilder()
.SetHttpClient(new RetryHttpClient())
.Build();
heet insert = ss.SheetResources.GetSheet(...148L, null, null, null, null, null, null, null);
Sheet pmosheet = ss.SheetResources.GetSheet(...556L, null, null, null, null, null, null, null);
// Build column map for later reference
foreach (Column column in insert.Columns)
columnMap.Add(column.Title, (long)column.Id);
foreach (Column column in pmosheet.Columns)
columnMapPMO.Add(column.Title, (long)column.Id);
IList<Row> rowsToCompare = pmosheet.Rows;
IList<Row> rowsToMove = insert.Rows;
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");
}
}
}
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]);
}
}
}
就像我这样,我修改了循环的顺序,以使要添加的行形成外部循环(假设有些项目可能没有要插入的相应行项目,无需查看),当我找到适合我的项目时,我退出内循环。
输出看起来像这样:
我从头到尾都通过了测试,因此看来您的代码可以解决问题。也许可以简化示例输入,以便您可以验证自己想要的东西。这也可能告诉我们这是否是数据驱动的问题。