简体   繁体   English

如何使用Microsoft图形API在Excel单元格中添加公式?

[英]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.

相关问题 如何使用 Microsoft Graph API 和 Graph Client 添加 SharePoint 选项卡? - How to add a SharePoint tab using Microsoft Graph API and Graph Client? 如何将求和公式添加到Excel中的单元格 - How To Add Sum formula to cell in Excel 使用Sharepoint文档库和Microsoft Graph API处理Excel文件 - Work with Excel file using Sharepoint document library with Microsoft Graph API 使用 Microsoft Graph API 更新 Excel 命名范围中的值 - Update Values in Excel Named Ranges using Microsoft Graph API 如何使用 Microsoft Graph API 获取 excel 图表的图像? 来自 C# - How do I get the image of an excel chart using Microsoft Graph API ? from c# 如何使用Microsoft Graph Client添加扩展名? - How to add extensions using Microsoft Graph Client? 使用 Graph API 和委派权限将成员添加到 Microsoft Teams - Add a member to Microsoft Teams using Graph API and delegated permissions 如何使用c#使用公式Today()读取Excel单元格? - How to read a excel cell with formula Today() , using c#? 如何在 Microsoft Graph API 的访问令牌中添加权限 - How to add the permissions in the access token of Microsoft Graph API 如何使用 Microsoft Graph API 获取所有组的用户名? - How to get user names of all groups using Microsoft Graph API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM