【问题标题】:Smartsheet c# SDK Parent-ChildSmartsheet c# SDK 父子
【发布时间】:2015-11-28 17:03:42
【问题描述】:

在 Smartsheet c# SDK 中,row.ParentRowId 很有帮助,但我没有看到一种方法来确定给定行在层次结构中的哪个级别?例如,如果我有类似...

  • 项目名称
    • 第 1 节
      • 任务 1
      • 任务 2
    • 第 2 节
      • 任务 3
      • 任务 4

在 Microsoft 项目中,有一个“大纲级别”字段,其中项目名称为“1”,第 1 节和第 2 节为“2”,示例中的每个任务为“3”。

我不知道如何使用 Smartsheet 的 SDK 确定(或更改)行的“大纲级别”。

谢谢,

【问题讨论】:

    标签: c# parent-child smartsheet-api smartsheet-c#-sdk-v2


    【解决方案1】:

    使用 Smartsheet API,行是另一行的子行这一事实由 Row 对象上存在 parentId 属性来指示。如果存在,则 Row 对象上的该属性指定父行的 Id 的值。

    关于 C# SDK,这个属性被暴露为 Row 对象的 ParentId 属性(注意“ParentId”的大写) .使用 C# SDK:

    • 如果响应中 Row 对象的 ParentId 属性为 null,则表明该 Row 是顶级行(即,不是任何其他行的子行)。李>
    • 如果 Row 对象的 ParentId 属性不为空,则表明该 Row 是具有与 ParentId 对应的 Id 的行的子行 价值。

    以下代码 sn-p 使用 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 属性派生每行的“大纲级别” .

    您可以使用 Smartsheet API 更改行的“大纲级别”(即缩进),方法是使用 API 文档中描述的 位置说明符行属性:http://smartsheet-platform.github.io/api-docs/#row-include-flags

    使用 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 行将具有 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;
      }
      

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-17
        • 1970-01-01
        • 1970-01-01
        • 2018-05-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多