使用 Smartsheet API 根据所选单元格中的数据比较两个行列表
Comparing two list of rows based on Data in a selected cell using Smartsheet API
我想比较两个行数据列表。现在我想看看这两个列表在各自的单元格值中是否包含相同的标题。
我可以使用 Smartsheet API C# 对列表进行排序并比较每一行中的每个 select 元素的哪些方法?
我已经有了一个列名 table 来搜索列名并引用实际的列 ID。但我似乎无法理解如何?
任何意见都会有所帮助,如果我听起来很愚蠢,我很抱歉,但我通常不会寻求帮助。
我在 Smartsheet 中有两个 sheet。一个 sheet 包含所有给定的数据,以及它经历接受或拒绝的过程。如果完全接受,它的状态为 "Moved to Project"。当代码运行时,它会将所有具有该状态的行放入一个列表,然后该列表将用于移动和与其他列表进行比较。
移动到项目列表将与我们的项目管理活动列表进行比较。
我一直试图通过 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]);
}
}
只要标题和项目名称匹配,它就应该输出是,如果不匹配则输出否。
但是我得到了一个未处理的异常: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 确认代码有效,但在我的实例中它仍然出现错误,所以我将把我目前拥有的代码作为一个整体来查看其他函数是否可能导致问题.
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]);
}
好的,我有两个sheet,项目sheet是这样的:
包含要插入的行的作业 sheet 如下所示:
这是代码:
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]);
}
}
}
作为一个 nit,我修改了循环的顺序,以便要添加的行形成外循环(假设有些项目可能没有要插入的相应行项目,不需要查看),当我找到适合我的项目时,我退出内部循环。
输出如下所示:
我确实顺利通过了测试,看来您的代码可以解决问题。也许简化您的示例输入,以便您可以验证您是否得到了您想要的。这也可能告诉我们这是否是数据驱动的问题。
我想比较两个行数据列表。现在我想看看这两个列表在各自的单元格值中是否包含相同的标题。
我可以使用 Smartsheet API C# 对列表进行排序并比较每一行中的每个 select 元素的哪些方法?
我已经有了一个列名 table 来搜索列名并引用实际的列 ID。但我似乎无法理解如何?
任何意见都会有所帮助,如果我听起来很愚蠢,我很抱歉,但我通常不会寻求帮助。
我在 Smartsheet 中有两个 sheet。一个 sheet 包含所有给定的数据,以及它经历接受或拒绝的过程。如果完全接受,它的状态为 "Moved to Project"。当代码运行时,它会将所有具有该状态的行放入一个列表,然后该列表将用于移动和与其他列表进行比较。
移动到项目列表将与我们的项目管理活动列表进行比较。
我一直试图通过 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]);
}
}
只要标题和项目名称匹配,它就应该输出是,如果不匹配则输出否。
但是我得到了一个未处理的异常: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 确认代码有效,但在我的实例中它仍然出现错误,所以我将把我目前拥有的代码作为一个整体来查看其他函数是否可能导致问题.
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]);
}
好的,我有两个sheet,项目sheet是这样的:
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]);
}
}
}
作为一个 nit,我修改了循环的顺序,以便要添加的行形成外循环(假设有些项目可能没有要插入的相应行项目,不需要查看),当我找到适合我的项目时,我退出内部循环。
输出如下所示:
我确实顺利通过了测试,看来您的代码可以解决问题。也许简化您的示例输入,以便您可以验证您是否得到了您想要的。这也可能告诉我们这是否是数据驱动的问题。