批量操作-在工作表中添加多行

时间:2019-01-17 16:45:13

标签: smartsheet-api

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

我已经尝试过一种解决方法,只是遍历源中的每条记录并发布该数据。

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

            }

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

首选项将是使用批量操作,但是没有一个例子,我有点茫然。但是,该循环也无法解决,因此,如果有人有任何建议,我将不胜感激!

谢谢你, 钱宁

2 个答案:

答案 0 :(得分:3)

您看过Smartsheet https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html吗?这可能是有用的参考。它包含批量操作的示例用法,该批量操作可通过一次调用更新多行。

答案 1 :(得分:2)

泰勒,

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

我按列值列表进行分组,并为最终批量操作构建了记录。我使用了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;
相关问题