Smartsheet API不允许我保存。

huangapple go评论176阅读模式
英文:

Smartsheet API not letting me save

问题

我正试图更新我们在Smartsheet中用于值班表的表格。我正在清空所有行,然后根据我们的数据库重新添加它们。

以下是我代码的部分内容:

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

在上述最后一行,我收到以下错误消息:

Smartsheet.Api.InvalidRequestException: '此操作不允许使用属性 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。'

我无法在任何论坛中找到如何解决这个问题的信息。欢迎任何建议。

英文:

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&lt;string, long&gt; columnMap = new Dictionary&lt;string, long&gt;();
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&lt;Cell&gt;();
    newCells.Add(new Cell() { ColumnId = columnMap[&quot;WFM_ID&quot;], Value = emp.WFM_ID });
    newCells.Add(new Cell() { ColumnId = columnMap[&quot;Email&quot;], Value = emp.Email });
    newCells.Add(new Cell() { ColumnId = columnMap[&quot;Employee&quot;], Value = emp.Employee });
    newCells.Add(new Cell() { ColumnId = columnMap[&quot;Supervisor&quot;], Value = emp.Supervisor });
    newCells.Add(new Cell() { ColumnId = columnMap[&quot;FirstName&quot;], Value = emp.FirstName });
    newCells.Add(new Cell() { ColumnId = columnMap[&quot;LastName&quot;], Value = emp.LastName });
    newCells.Add(new Cell() { ColumnId = columnMap[&quot;Tier&quot;], Value = emp.Tier });
    newCells.Add(new Cell() { ColumnId = columnMap[&quot;Title&quot;], 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

答案1

得分: 1

尽管直觉上,似乎应该使用“更新工作表”操作来向工作表添加行,但这不是 Smartsheet API 的工作方式。要向工作表添加行,您需要使用“添加行”操作。

如果我理解正确,您的目标是:

  1. 删除指定工作表中的所有行。
  2. 向指定工作表中添加新行(使用来自数据库的员工数据)。

下面的代码示例显示了如何通过四个基本步骤实现这一目标:

  1. 获取工作表(使用“获取工作表”操作)并构建列映射。
  2. 从工作表中删除所有行(使用“删除行”操作)。
  3. 创建要添加到工作表的行的数组。
  4. 向工作表添加行(使用“添加行”操作)。

请注意 - 我用于测试的工作表仅包含3列:NameEmailPhone - 下面的代码示例相应地构建了测试数据。您可以省略以“// 创建(虚假)员工数据…”开头的代码部分(因为您的数据来自数据库),并需要编辑创建“Cell”对象的代码部分,以准确反映工作表的结构和要填充的单元格。您还需要更新“sheetId”属性的值为您的工作表的ID。

var sheetId = 1303196942526340;

// 1- 获取工作表并构建列映射
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- 从工作表中删除所有行
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);

// 创建(虚假)员工数据(模拟从数据库中获取的数据)
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- 创建要添加到工作表的行的数组
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
    };

    // 将行添加到rowsToAdd[]
    rowsToAdd[i] = trow;
    i += 1;
}

// 4- 向工作表添加行
ssc.SheetResources.RowResources.AddRows(sheetId, rowsToAdd);

请注意,这是一个 C# 代码示例,用于使用 Smartsheet API 执行所描述的任务。如果您需要其他语言的代码示例,请告诉我。

英文:

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:

  1. Delete all rows from the specified sheet.
  2. Add new rows to the specified sheet (using employee data from a database).

The code sample below shows how to achieve this, using 4 basic steps:

  1. Get sheet (by using the Get Sheet operation) and build column map.
  2. Delete all rows from sheet (by using the Delete Rows operation).
  3. Create Array of the rows to be added to the sheet.
  4. Add rows to the sheet (by using the Add Rows operation).

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&lt;string, long&gt; columnMap = new Dictionary&lt;string, long&gt;();
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 &lt; 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&#39;s you&#39;ll be getting from a database)
EmployeeData [] oEmploys = new EmployeeData[2];
EmployeeData employee1 = new EmployeeData();
employee1.SetInfo(&quot;John Doe&quot;, &quot;johndoe@test.com&quot;, &quot;555-111-2222&quot;);
EmployeeData employee2 = new EmployeeData();
employee2.SetInfo(&quot;Jane Doe&quot;, &quot;janedoe@test.com&quot;, &quot;555-333-4444&quot;);
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[&quot;Name&quot;], Value = emp.name },
        new Cell { ColumnId = columnMap[&quot;Email&quot;], Value = emp.email },
        new Cell { ColumnId = columnMap[&quot;Phone&quot;], 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);

huangapple
  • 本文由 发表于 2023年3月7日 05:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定