smartsheet-api - smartsheet C# SDK 写入空单元格似乎不起作用
问题描述
使用 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 来创建包含内容的新行。
我错过了什么?有什么想法可以解决这个问题吗?也许有更好的方法?提前致谢。
解决方案
您可以使用Add Rows方法添加新行并填充该行中的一个或多个单元格。
请注意,默认情况下,Add Rows方法会将新行添加到工作表的末尾,但您可以通过为每行指定行位置来更改此默认行为。
以下代码示例将 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
);
推荐阅读
- python - 如何在 python 中生成与操作系统无关的文件哈希?
- sql - 如何为 SQL 执行相当于双循环附加的操作
- python - 添加/减去两个 pyspark CountVectorizer 稀疏向量列
- rest - 内容类型'application/json;charset=UTF-8' 不支持 Spring Boot REST API
- maxscript - Maxscript - 如何在 3ds Max 2020 中将渲染更改为纹理输出目录?
- html - MS Outlook 2013 (Windows 7) 中图像周围的奇怪框架
- algorithm - 如何使用 2D 表最大化棒材切割的产品?
- dependency-injection - 服务中的导入 dart:html 出现异常 找不到动态提供程序
- javascript - 我有一个数字数组。我需要返回一个新数组,其中重复的数字在它们自己的嵌套数组中
- c# - 确定独特的构建