在 Smartsheet c# SDK 中,row.ParentRowId 很有帮助,但我没有看到一种方法来确定给定行在层次结构中的哪个级别?例如,如果我有类似...
- 项目名称
- 第 1 节
- 任务1
- 任务 2
- 第 2 节
- 任务 3
- 任务 4
- 第 1 节
在 Microsoft 项目中,有一个“大纲级别”字段,其中项目名称为“1”,第 1 节和第 2 节为“2”,示例中的每个任务为“3”。
我看不到如何使用 Smartsheet 的 SDK 确定(或更改)行的“大纲级别”。
谢谢,
在 Smartsheet c# SDK 中,row.ParentRowId 很有帮助,但我没有看到一种方法来确定给定行在层次结构中的哪个级别?例如,如果我有类似...
在 Microsoft 项目中,有一个“大纲级别”字段,其中项目名称为“1”,第 1 节和第 2 节为“2”,示例中的每个任务为“3”。
我看不到如何使用 Smartsheet 的 SDK 确定(或更改)行的“大纲级别”。
谢谢,
使用 Smartsheet API,行是另一行的子行这一事实由Row 对象上的parentId属性的存在来指示。如果存在,则 Row 对象上的该属性指定父行的 Id 的值。
具体而言,对于 C# SDK,此属性公开为Row对象的ParentId属性(注意“<strong>ParentId”的大写)。使用 C# SDK:
以下代码片段使用 C# SDK 通过检查工作表中每一行的ParentId值来识别行层次结构。
// Set the Access Token.
Token token = new Token();
token.AccessToken = YOUR_ACCESS_TOKEN;
// Use the Smartsheet Builder to create a Smartsheet.
SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(token.AccessToken).Build();
// Get Sheet.
long sheetId = YOUR_SHEET_ID;
Sheet sheet = smartsheet.SheetResources.GetSheet(sheetId, null, null, null, null, null, null, null);
// Identify Row hierarchy by examining the ParentId property of each row.
foreach (Row row in sheet.Rows)
{
if (row.ParentId != null)
{
Response.Write("Row #" + row.RowNumber.ToString() + " (Id=" + row.Id.ToString() + ") is a child of Row Id " + row.ParentId.ToString() + "<br/><br/>");
}
else
{
Response.Write("Row #" + row.RowNumber.ToString() + " (Id=" + row.Id.ToString() + ") is a top-level row.<br/><br/>");
}
}
Smartsheet API 当前不公开类似于“大纲级别”的属性 - 但您应该能够通过使用上述ParentId属性派生每个 Row 的“大纲级别”。
通过使用 API 文档中描述的位置说明符Row 属性,您可以使用 Smartsheet API 更改行的“大纲级别”(即缩进) :http: //smartsheet-platform.github.io/api-docs/#行包含标志。
使用 C# SDK,您可以在调用Row.AddRowBuilder(…)函数时指定位置说明符 Row 属性的某种组合,以描述该行相对于其他已经存在的行应放置在工作表中的位置。我在上面链接到的文档描述了通过设置行的位置说明符属性的各种组合可以获得的放置结果。
例如,以下脚本添加一个新行作为指定父行下的第一个子行:
// Set the Access Token.
Token token = new Token();
token.AccessToken = YOUR_ACCESS_TOKEN;
// Use the Smartsheet Builder to create a Smartsheet.
SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(token.AccessToken).Build();
// Get Sheet.
long sheetId = YOUR_SHEET_ID;
Sheet sheet = smartsheet.SheetResources.GetSheet(sheetId, null, null, null, null, null, null, null);
// Identify Row hierarchy by examining the ParentId property of each row.
foreach (Row row in sheet.Rows)
{
if (row.ParentId != null)
{
Response.Write("Row #" + row.RowNumber.ToString() + " (Id=" + row.Id.ToString() + ") is a child of Row Id " + row.ParentId.ToString() + "<br/><br/>");
}
else
{
Response.Write("Row #" + row.RowNumber.ToString() + " (Id=" + row.Id.ToString() + ") is a top-level row.<br/><br/>");
}
}
/************************************************
* Add new row.
/************************************************/
// Specify cell values for the row.
Cell[] cells = new Cell[] { new Cell.AddCellBuilder(2069395137685380, "New Task").Build(), new Cell.AddCellBuilder(4743407416436612, "Added via API").SetStrict(false).Build() };
// Specify the Id of the parent row.
long parentId = 1085142354683780;
// Specify contents and placement of new row.
// Use the 5 parameters of the "AddRowBuilder" function to specify row's placement: toTop, toBottom, parentId, siblingId, above
// Specifying parentId only will add the new row as the first child row under the specified parent row.
Row newRow = new Row.AddRowBuilder(null, null, parentId, null, null).SetCells(cells).Build();
// Add row to sheet.
smartsheet.SheetResources.RowResources.AddRows(sheetId, new Row[] { newRow });
这是我为跟踪我们的缩进所做的工作。每个缩进都是一个级别。因此,在我的图像中,第 2 行将具有 2 级,第 7 行将具有 3 级。第 8 行将具有 2 级,其父行号为 7(第 7 行)。
foreach (var detailRow in detailSheet.Rows)
{
long rowId = (detailRow.Id.HasValue) ? detailRow.Id.Value : 0;
long parentId = (detailRow.ParentId.HasValue) ? detailRow.ParentId.Value : 0;
int parentRowNumber = 0;
int level = 0;
if (parentId != 0)
{
foreach (RowLevel lvl in rowLevelList)
{
if (lvl.RowId != parentId) continue;
parentRowNumber = lvl.Row;
level = lvl.Level + 1;
break;
}
}
RowLevel rowLevel = new RowLevel();
rowLevel.RowId = rowId;
rowLevel.ParentId = parentId;
rowLevel.Row = detailRow.RowNumber.Value;
rowLevel.ParentRowNumber = parentRowNumber;
rowLevel.Level = level;
rowLevelList.Add(rowLevel);
System.Diagnostics.Debug.WriteLine("Row: " + detailRow.RowNumber);
System.Diagnostics.Debug.WriteLine("Row Id: " + rowId);
System.Diagnostics.Debug.WriteLine("Parent Id: " + parentId);
System.Diagnostics.Debug.WriteLine("Parent Row Number: " + parentRowNumber);
System.Diagnostics.Debug.WriteLine("Level: " + level);
System.Diagnostics.Debug.WriteLine("");
}
一个跟踪级别和父行号的小类
private class RowLevel
{
public int Row = 0;
public int Level = 0;
public int ParentRowNumber = 0;
public long ParentId = 0;
public long RowId = 0;
}
希望这可以帮助!