Search code examples
c#exceptionsmartsheet-api

InvalidRowLocation Smartsheet C#


Can't seem to set indentation level of a row to 1. AddRows results in an InvalidRowLocation exception.

    static string[] DeserializeJson(string json)
    {
        try
        {
            SortedDictionary<string, string> values = JsonConvert.DeserializeObject<SortedDictionary<string, string>>(json);
            return new string[] { values["Status"], values["ServerIP"], values["ClientIP"], values["Date"] };
        }
        catch
        {
            return new string[] { json };
        }
    }

    static Row MakeRow(Sheet sheet, string[] values)
    {
        List<Cell> cells = new List<Cell>();
        Cell cell;
        IEnumerator<Column> cols = sheet.Columns.GetEnumerator();
        DateTime date;
        for (int n = 0; n < values.Length; n++)
        {
            cols.MoveNext();
            Column col = cols.Current;
            if (col.Type == ColumnType.DATE)
            {
                date = DateTime.ParseExact(values[n], dateFormat, CultureInfo.InvariantCulture);
                cell = new Cell.AddCellBuilder(col.Id, DateTime.Now).Build();
            }
            else
                cell = new Cell.AddCellBuilder(col.Id, values[n]).Build();
            cells.Add(cell);
        }
        Row row = new Row.AddRowBuilder(null, true, null, null, null).SetCells(cells).Build();
        if (values.Length > 1)
            row.Indent = 1;    // Results in an exception
        return row;
    }

    static void AddRows(SmartsheetClient client, Sheet sheet, string[] lines)
    {
        List<Row> rows = new List<Row>();
        foreach (string line in lines)
            rows.Add(MakeRow(sheet, DeserializeJson(line)));
        client.SheetResources.RowResources.AddRows(sheetId, rows);
    }

Solution

  • Indenting in Smartsheet creates a "parent-child" relationship between the rows. So, to indent a new row you'll need to give it the rowId of its parent row as the parentId.

    It would look something like this

     Row row = new Row.AddRowBuilder(null, true, 7531436244775314, null, null).SetCells(cells).Build();