使用 Smartsheet API,行是另一行的子行这一事实由Row 对象上的parentId属性的存在来指示。如果存在,则 Row 对象上的该属性指定父行的 Id 的值。
具体而言,对于 C# SDK,此属性公开为Row对象的ParentId属性(注意“<strong>ParentId”的大写)。使用 C# SDK:
- 如果响应中 Row 对象的ParentId属性为空,则表明该 Row 是顶级行(即,不是任何其他行的子行)。
- 如果 Row 对象的ParentId属性不为空,则表示该 Row 是具有与ParentId值对应的Id的行的子行。
以下代码片段使用 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 });