Dawit Ghebrehiwet
Dawit Ghebrehiwet

Reputation: 13

smartsheet C# SDK writing to empty cell seems not work

Using the smartsheet C# SDK i have written a method setCellValue to write a value to a cell given the row index, the column index and the value. The method works fine when there is already a value in the cell but it does not work when the cell is empty.

public void setCellValue(int rowIndex, int columnIndex, String value, Boolean setCellDisplayValue = true)
{
    Cell cellOld = getCell(rowIndex, columnIndex);

    Cell cellNew = new Cell
    {
        Value = value,
        DisplayValue = value
    };

    if (true == setCellDisplayValue) {
        cellNew.DisplayValue = value;
    }

    if (null != cellOld)
    {
        cellNew.ColumnId = cellOld.ColumnId;
    }

    var listOfNewCells = new List<Cell>();

    listOfNewCells.Add(cellNew);

    Row rowNew = new Row
    {
        Cells = listOfNewCells
    };

    Row rowOld = sheetAPITest.GetRowByRowNumber(rowIndex);

    if (null != rowOld)
    {
        rowNew.Id = rowOld.Id;
    }

    var listOfNewRows = new List<Row>();

    listOfNewRows.Add(rowNew);

    smartsheetClient.SheetResources.RowResources.UpdateRows(sheetIdAPITest, listOfNewRows);
}

I also wrote a helper method getCell.

public Cell getCell(int rowIndex, int columnIndex)
{
    Row row = sheetAPITest.GetRowByRowNumber(rowIndex);

    if (null == row)
    {
        return null;
    }

    Column column = sheetAPITest.GetColumnByIndex(columnIndex);

    if (null == column)
    {
        return null;
    }

    return row.Cells.First(c => c.ColumnId == column.Id);
}

The problem seems to be that sheetAPITest.GetRowByRowNumber(rowIndex) returns null when there is no value in the row. I need the row because i need the row id to create the new row with the content.

What am I missing? Any ideas how i can solve the problem? Maybe there is a better approach? Thanks in advance.

Upvotes: 0

Views: 190

Answers (1)

Kim Brandl
Kim Brandl

Reputation: 13500

You can use the Add Rows method to add the new row and populate one or more cells in the row.

Note that by default, the Add Rows method will add the new row(s) to the end of the sheet, but you can change this default behavior by specifying row location for each row.

The following code sample adds 2 new rows to the top of the specified sheet and populates 2 cells within each of those rows:

// Specify cell values for first row
Cell[] cellsA = new Cell[] {
  new Cell
  {
    ColumnId = 7960873114331012,
    Value = true
  },
  new Cell
  {
    ColumnId = 642523719853956,
    Value = "New status"
  }
};

// Specify contents of first row
Row rowA = new Row
{
  ToTop = true,
  Cells = cellsA
};

// Specify cell values of second row
Cell[] cellsB = new Cell[] {
  new Cell
  {
    ColumnId = 7960873114331012,
    Value = true
  },
  new Cell
  {
    ColumnId = 642523719853956,
    Value = "New status"
  }
};

// Specify contents of second row
Row rowB = new Row
{
  ToTop = true,
  Cells = cellsB
};

// Add rows to sheet
IList<Row> newRows = smartsheet.SheetResources.RowResources.AddRows(
  2331373580117892,               // long sheetId
  new Row[] { rowA, rowB }        // IEnumerable<Row> rowsToAdd
);

Upvotes: 2

Related Questions