更新2022-12-01
这个问题现在已经过时了。Smartsheet现在支持所请求的特性。接受的答案包含更新的更多细节。
上下文
开发人员希望使用smartsheet填充现有的smartsheet工作表。要填充的数据包括数据和公式。
但是,根据API文档(http://www.smartsheet.com/developers/api-documentation),不能使用API添加公式。
包含公式、指向其他单元格、系统值或甘特值的单元格不能通过API插入或更新。
问题
测试验证了这一点。试图使用smartsheet添加一个简单的公式会导致将公式转换为不透明文本。检查发现,公式是用单引号修改的,这使它变成了不透明的文本,而不是公式。
问题
是否有任何方法(通过手动输入)强迫smartsheet重新评估插入的不透明文本,从而将不透明文本转换回公式?
如果不可能(通过手动输入除外),是否可以复制现有的具有公式的工作表,然后将非公式数据填充到工作表中,所有这些都使用smartsheet?
目标
基本目标是找到一种将公式数据填充到smartsheet应用程序中的方法,而无需手动输入公式数据。
发布于 2014-04-08 18:30:34
更新:Smartsheet现在支持通过API添加或更新公式,这些公式可以在添加一行和更新行的文档中找到。
主要的区别是在row对象中设置formula
,而不是设置value
。
原始答案
您是正确的,公式目前不支持通过API。不过,我们确实计划在将来添加此功能。
目前,如果您试图将一个公式发送到API,它将作为一个字符串处理,并将一个单引号添加到公式的开头。然后,将字符串转换回公式的唯一方法是在Smartsheet中手动删除单引号。
可用选项
如果您总是使用相同的公式,那么使用模板的建议肯定会有效。该过程如下所示:
注意:从未使用过的行不能更新,因为它们不存在。因此,在模板中,可以通过在要更新的位置放置一个单词来初始化行。例如,您可以将“占位符”一词放入要更新的所有位置。
我在下面添加了两个示例,一个使用curl,另一个使用我们的Java。
卷曲示例
从模板创建一个新工作表。确保在下面的命令中替换YOUR_TOKEN
和YOUR_TEMPLATE_OR_SHEET_ID
。
curl https://api.smartsheet.com/1.1/sheets?include=data,attachments,discussions -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -X POST -d '{"name":"newSheetFromTemplate","fromId":"YOUR_TEMPLATE_OR_SHEET_ID"}'
然后从响应中获取工作表id,并发出命令以获取行id。
curl https://api.smartsheet.com/1.1/sheet/YOUR_SHEET_ID -H "Authorization: Bearer YOUR_TOKEN"
最后,从响应中获取行id和列id,并发出命令更新适当的单元格。我正在用下面的命令更新两个值为1和2的单元格。
curl https://api.smartsheet.com/1.1/row/YOUR_ROW_ID/cells -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -X PUT -d '[ {"columnId": YOUR_COLUMN_ID, "value": 1}]'
curl https://api.smartsheet.com/1.1/row/YOUR_ROW_ID/cells -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -X PUT -d '[ {"columnId": YOUR_COLUMN_ID, "value": 2}]'
SDK示例
这个例子需要安装我们的Java。还有一个C# SDK,它以非常类似的方式工作。
import java.util.EnumSet;
import java.util.List;
import com.smartsheet.api.Smartsheet;
import com.smartsheet.api.SmartsheetBuilder;
import com.smartsheet.api.models.Cell;
import com.smartsheet.api.models.Column;
import com.smartsheet.api.models.ObjectInclusion;
import com.smartsheet.api.models.Row;
import com.smartsheet.api.models.Sheet;
import com.smartsheet.api.SmartsheetException;
public class Test {
public static void main(String[] args) throws SmartsheetException {
// Setup a Smartsheet object
Smartsheet smartsheet = new SmartsheetBuilder().setAccessToken("YOUR_TOKEN").build();
// Create a copy of a sheet from the template
Sheet newSheet = new Sheet.CreateFromTemplateOrSheetBuilder().setFromId(YOUR_TEMPLATE_OR_SHEET_ID).setName("newSheetName").build();
newSheet = smartsheet.sheets().createSheetFromExisting(newSheet, EnumSet.allOf(ObjectInclusion.class));
// Get the columns/rows/data for the sheet we just created
newSheet = smartsheet.sheets().getSheet(newSheet.getId(), EnumSet.allOf(ObjectInclusion.class));
// Grab the column and rows that will be updated in the new sheet
Column column1 = newSheet.getColumnByIndex(0);
Row row1 = newSheet.getRowByRowNumber(1);
Row row2 = newSheet.getRowByRowNumber(2);
// Setup two cells for the the specified columns
List<Cell> newCell1 = new Cell.UpdateRowCellsBuilder().addCell(column1.getId(), 1).build();
List<Cell> newCell2 = new Cell.UpdateRowCellsBuilder().addCell(column1.getId(), 2).build();
// Update the cell for the specified row
smartsheet.rows().updateCells(row1.getId(), newCell1);
smartsheet.rows().updateCells(row2.getId(), newCell2);
}
}
https://stackoverflow.com/questions/22925389
复制相似问题