批量操作 - 将多行添加到 sheet

Bulk Operations - Adding multiple rows to sheet

我正在尝试通过智能sheet API(使用c# SDK)将数据从数据table写入sheet。我查看了文档,发现它支持批量操作,但我正在努力寻找该功能的示例。

我试图解决这个问题,只是循环遍历我的源中的每条记录和 post 该数据。

//Get column properties  (column Id ) for existing smartsheet and add them to List for AddRows parameter
            //Compare to existing Column names in Data table for capture of related column id
            var columnArray = getSheet.Columns;
            foreach (var column in columnArray)
            {
                foreach (DataColumn columnPdiExtract in pdiExtractDataTable.Columns)
                {
                    //Console.WriteLine(columnPdiExtract.ColumnName);
                    if(column.Title == columnPdiExtract.ColumnName)
                    {
                        long columnIdValue = column.Id ?? 0;
                        //addColumnArrayIdList.Add(columnIdValue);
                        addColumnArrayIdList.Add(new KeyValuePair<string, long>(column.Title,columnIdValue));
                    }
                }
            }



            foreach(var columnTitleIdPair in addColumnArrayIdList)
            {
                Console.WriteLine(columnTitleIdPair.Key);

                var results = from row in pdiExtractDataTable.AsEnumerable() select row.Field<Double?>(columnTitleIdPair.Key);

                foreach (var record in results)
                {
                    Cell[] cells = new Cell[]
                    {
                        new Cell
                        {
                            ColumnId = columnTitleIdPair.Value,
                            Value = record
                        }
                    };
                    cellRecords = cells.ToList();
                    cellRecordsInsert.Add(cellRecords);

                }

            Row rows = new Row
            {
                ToTop = true,
                Cells = cellRecords
            };

            IList<Row> newRows = smartsheet.SheetResources.RowResources.AddRows(sheetId, new Row[] { rows });

            }

我希望为每个单元格生成一个值,将其附加到列表中,然后 post 通过行对象生成它。但是,我的循环是这样附加列值的:A1: 1, B2: 2, C3: 3 而不是 A1: 1, B1: 2, C3: 3

首选是使用批量操作,但没有示例我有点不知所措。但是,循环也没有解决,所以如果有人有任何建议,我将不胜感激!

谢谢, 钱宁

你看过 Smartsheet C# sample read / write sheet 吗?这可能是一个有用的参考。它包含使用一次调用更新多行的批量操作的示例。

泰勒,

感谢您的帮助。您引导我朝着正确的方向前进,我找到了解决方案。

我按列值列表分组并为最后的批量操作建立了记录。我使用了一个 For 循环,但在此方法之前,每个列分组中的元素都被清理并分配了一个 0,以便它们在每个分组中保留相同的值计数。

      // Pair column and cell values for row building - match 
      // Data source column title names with Smartsheet column title names

        List<Cell> pairedColumnCells = new List<Cell>();

        //Accumulate cells 
        List<Cell> cellsToImport = new List<Cell>();

        //Accumulate rows for additions here
        List<Row> rowsToInsert = new List<Row>();

        var groupByCells = PairDataSourceAndSmartsheetColumnToGenerateCells(
                             sheet, 
                             dataSourceDataTable).GroupBy(
                                 c => c.ColumnId,
                                 c => c.Value, 
                                (key, g) => new { 
                                    ColumnId = key, Value = g.ToList<object>() 
                                });

        var countGroupOfCells = groupByCells.FirstOrDefault().Value.Count();

        for (int i = 0; i <= countGroupOfCells - 1; i++)
        {

            foreach (var groupOfCells in groupByCells)
            {
                var cellListEelement = groupOfCells.Value.ElementAt(i);

                var cellToAdd = new Cell
                {
                    ColumnId = groupOfCells.ColumnId,
                    Value = cellListEelement
                };

                cellsToImport.Add(cellToAdd);
            }

            Row rows = new Row
            {
                ToTop = true,
                Cells = cellsToImport
            };

            rowsToInsert.Add(rows);

            cellsToImport = new List<Cell>();


        }

        return rowsToInsert;