[英]how to Add a formula in excel cell using Microsoft graph API?
I am trying to add a content to a cell in Excel using Microsoft Graph's Excel API . 我正在尝试使用Microsoft Graph的Excel API将内容添加到Excel中的单元格。 I am able to add content using
PATCH
method but when I try to add a formula, it does not behave like a formula. 我可以使用
PATCH
方法添加内容,但是当我尝试添加公式时,它的行为不像公式。 If I pass something like 'formulas': '=sum(2+2)'
, it does not behave as it should. 如果我传递类似
'formulas': '=sum(2+2)'
,它的行为将不正常。
Result i am getting is in snip: 我得到的结果是剪断:
Is this doable? 这可行吗?
here is my code: 这是我的代码:
//Set up workbook and worksheet endpoints
var workbookEndpoint = "https://graph.microsoft.com/v1.0/me/drive/items/" +
fileId + "/workbook";
var worksheetsEndpoint = workbookEndpoint + "/worksheets";
var patchMethod = new HttpMethod("PATCH");
var summaryTableRowJson = "{" +
"'formulas': '=sum(2+2)'" +
"}";
var colNamePatchBody = new StringContent(summaryTableRowJson);
colNamePatchBody.Headers.Clear();
colNamePatchBody.Headers.Add("Content-Type", "application/json");
var colNameRequestMessage = new HttpRequestMessage(patchMethod, worksheetsEndpoint +
"('" + worksheetName + "')/range(address='Sheet1!B2')")
{
Content = colNamePatchBody
};
var colNameResponseMessage = await client.SendAsync(colNameRequestMessage);
You need to pass this in via the formulas
rather than the values
property: 您需要通过
formulas
而不是values
属性来传递它:
{
"formulas" : "=sum(2+2)"
}
You should also consider using the Microsoft Graph Client Library for .NET instead of rolling your own raw HTTP calls. 您还应该考虑使用Microsoft Graph Client Library for .NET,而不是滚动自己的原始HTTP调用。 It will save you a ton of headaches over time.
随着时间的流逝,它将为您节省很多头痛。 It also results in much cleaner code:
它还产生了更简洁的代码:
await graphClient.Me
.Drive
.Items["id"]
.Workbook
.Worksheets["Sheet1"]
.Range("C12")
.Request()
.PatchAsync(new WorkbookRange()
{
Formulas = JArray.Parse(@"[['=2.2']]")
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.