gpt4 book ai didi

c# - 使用带有 Microsoft Graph API 的 Sharepoint 文档库处理 Excel 文件

转载 作者:太空宇宙 更新时间:2023-11-03 15:02:08 26 4
gpt4 key购买 nike

我下载并定制了这个项目asp mvc todo sample .这工作正常,onedrive excel 文件/项目没有任何问题。但我必须只在线使用 Office 365 Sharepoint 来做到这一点。我可以使用服务点获取文件 ID,如下所示:

var serviceEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items";

但我不确定如何使用 Sharepoint 定义工作簿端点 URL 以更新/操作相同的 excel 文件数据。当前工作簿端点 URL 如下(不起作用):

var workbookEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items/" + fileId + "/workbook";

我在 Sharepoint 上更新 excel 文件中单元格值的完整方法如下:

public static async Task UpdateExcelCellValue_Sharepoint(string accessToken, string cellAddress, string val)
{
string worksheetName = "Parameters";
var serviceEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var filesResponse = await client.GetAsync(serviceEndpoint + "?$select=name,id");
if (filesResponse.IsSuccessStatusCode)
{
var filesContent = await filesResponse.Content.ReadAsStringAsync();
JObject parsedResult = JObject.Parse(filesContent);
foreach (JObject file in parsedResult["value"])
{
var name = (string)file["id"];
if (name.Contains("173"))
{
fileId = (string)file["id"];
break;
}
}
}
else
{
//Handle failed response
}
//Set up workbook and worksheet endpoints
var workbookEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items/" + fileId + "/workbook";
var worksheetsEndpoint = workbookEndpoint + "/worksheets";
var patchMethod = new HttpMethod("PATCH");
var summaryTableRowJson = "{" +
"'values': '" + val + ".csv'" +
"}";
var colNamePatchBody = new StringContent(summaryTableRowJson);
colNamePatchBody.Headers.Clear();
colNamePatchBody.Headers.Add("Content-Type", "application/json");
var colNameRequestMessage = new HttpRequestMessage(patchMethod, worksheetsEndpoint +
"('" + worksheetName + "')/range(address='Parameters!B2')")
{ Content = colNamePatchBody };
var colNameResponseMessage = await client.SendAsync(colNameRequestMessage);
}

最佳答案

现在应该可以了。例如,这是一条有效的路径:

https://graph.microsoft.com/beta/sites/{tenant-name}.sharepoint.com/lists/{id}/drive/root:/Book.xlsx:/workbook。这是其中一种可能。

您始终可以尝试用 ID 替换路径,看看是否有效(两者都应该有效)。

关于c# - 使用带有 Microsoft Graph API 的 Sharepoint 文档库处理 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45715200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com