【问题标题】:smartsheet C# SDK writing to empty cell seems not worksmartsheet C# SDK 写入空单元格似乎不起作用
【发布时间】:2018-11-08 08:52:57
【问题描述】:

使用 smartsheet C# SDK,我编写了一个 setCellValue 方法,在给定行索引、列索引和值的情况下将值写入单元格。当单元格中已经有一个值时,该方法可以正常工作,但当单元格为空时它不起作用。

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);
}

我还写了一个辅助方法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);
}

问题似乎是当行中没有值时 sheetAPITest.GetRowByRowNumber(rowIndex) 返回 null 。我需要该行,因为我需要行 ID 来创建包含内容的新行。

我错过了什么?有什么想法可以解决这个问题吗?也许有更好的方法?提前致谢。

【问题讨论】:

  • 为什么在没有任何评论的情况下投反对票?我已经在网上搜索并查看了 SDK 的代码和文档,但没有找到在给定行索引、列索引和值的情况下写入特定单元格的方法。

标签: smartsheet-api


【解决方案1】:

您可以使用Add Rows 方法添加新行并填充该行中的一个或多个单元格。

请注意,默认情况下,Add Rows 方法会将新行添加到工作表的末尾,但您可以通过specifying row location 为每一行更改此默认行为。

以下代码示例将 2 个新行添加到指定工作表的顶部,并在每行中填充 2 个单元格:

// 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
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多