I am attempting to update a sheet that we use for our roster in smartsheet. I am clearing out all the rows and then adding them back based on out database.
Here is what I have as far as the code goes:
Sheet roster = ssc.SheetResources.GetSheet(SomeSheeNumberGoesHere, null, null, null, null, null, null, null);
Dictionary<string, long> columnMap = new Dictionary<string, long>();
foreach (Column column in roster.Columns)
columnMap.Add(column.Title, (long)column.Id);
roster.Rows.Clear();
foreach (EmployeeData emp in oEmploys)
{
var newCells = new List<Cell>();
newCells.Add(new Cell() { ColumnId = columnMap["WFM_ID"], Value = emp.WFM_ID });
newCells.Add(new Cell() { ColumnId = columnMap["Email"], Value = emp.Email });
newCells.Add(new Cell() { ColumnId = columnMap["Employee"], Value = emp.Employee });
newCells.Add(new Cell() { ColumnId = columnMap["Supervisor"], Value = emp.Supervisor });
newCells.Add(new Cell() { ColumnId = columnMap["FirstName"], Value = emp.FirstName });
newCells.Add(new Cell() { ColumnId = columnMap["LastName"], Value = emp.LastName });
newCells.Add(new Cell() { ColumnId = columnMap["Tier"], Value = emp.Tier });
newCells.Add(new Cell() { ColumnId = columnMap["Title"], Value = emp.Title });
Row trow = new Row() {
Cells = newCells
};
roster.Rows.Add(trow);
}
ssc.SheetResources.UpdateSheet(roster);
on that last line, I get the following error:
Smartsheet.Api.InvalidRequestException: 'The attribute(s) sheet.dependenciesEnabled, sheet.permalink, sheet.accessLevel, sheet.version, sheet.rows[], sheet.effectiveAttachmentOptions[], sheet.createdAt, sheet.totalRowCount, sheet.modifiedAt, sheet.columns[], sheet.hasSummaryFields, sheet.resourceManagementEnabled, sheet.ganttEnabled, sheet.userPermissions are not allowed for this operation.'
I can't find anywhere on any of the forums on how to fix this. any suggestions are welcome
Although, intuitively, it seems like you'd want to use the Update Sheet operation to add rows to a sheet, that's not how the Smartsheet API works. To add rows to a sheet, you'll need to use the Add Rows operation.
If I understand correctly, your objective is to:
The code sample below shows how to achieve this, using 4 basic steps:
Please note -- the sheet I'm testing with has only 3 columns: Name
, Email
, and Phone
-- and the code sample below constructs test data accordingly. You can omit the portion of code that's preceded with the comment // create (fake) Employee data...
(since your data is coming from a database) and will need to edit the portion of code that's creating the Cell
objects (so that it accurately reflects the structure of your sheet and the cells you want to populate). You'll also need to update the value of the sheetId
property to be your sheet's ID.
var sheetId = 1303196942526340;
// 1- get sheet and build column map
Sheet roster = ssc.SheetResources.GetSheet(sheetId, null, null, null, null, null, null, null);
Dictionary<string, long> columnMap = new Dictionary<string, long>();
foreach (Column column in roster.Columns) {
columnMap.Add(column.Title, (long)column.Id);
}
// 2- delete all rows from sheet
long [] rowIds = new long[roster.Rows.Count];
for (int ctr = 0; ctr < roster.Rows.Count; ctr++) {
rowIds[ctr] = (long)roster.Rows[ctr].Id;
}
ssc.SheetResources.RowResources.DeleteRows(sheetId, rowIds, true);
// create (fake) Employee data (to mimic the data that's you'll be getting from a database)
EmployeeData [] oEmploys = new EmployeeData[2];
EmployeeData employee1 = new EmployeeData();
employee1.SetInfo("John Doe", "johndoe@test.com", "555-111-2222");
EmployeeData employee2 = new EmployeeData();
employee2.SetInfo("Jane Doe", "janedoe@test.com", "555-333-4444");
oEmploys[0] = employee1;
oEmploys[1]= employee2;
// 3- create Array of the rows to be added to the sheet
Row [] rowsToAdd = new Row[oEmploys.Count()];
int i = 0;
foreach (EmployeeData emp in oEmploys)
{
Cell[] newCells = new Cell[] {
new Cell { ColumnId = columnMap["Name"], Value = emp.name },
new Cell { ColumnId = columnMap["Email"], Value = emp.email },
new Cell { ColumnId = columnMap["Phone"], Value = emp.phone }
};
Row trow = new Row() {
Cells = newCells,
ToBottom = true
};
// add row to rowsToAdd[]
rowsToAdd[i] = trow;
i += 1;
}
// 4- add rows to the sheet
ssc.SheetResources.RowResources.AddRows(sheetId, rowsToAdd);