Reputation: 865
I have an Excel file that calculates a product advise based on several input variables. To use this in a web application and let users define the input variables I want to use the Microsoft graph Excel API, but can't figure out if it's possible to make the calculations without actually copying & modifying the original Excel file.
Currently I got a workaround solution that requires three API calls to do this:
POST .../drive/root:/filename.xlsx:/copy {name:'newfilename.xlsx'}
)PATCH .../drive/root:/newfilename.xlsx:/workbook/worksheets('worksheetname')/range('address=A1') {values:'...'}
)GET .../drive/root:/newfilename.xlsx:/workbook/worksheets('worksheetname')/range('address=A2')
)Obviously this is not the desired solution, but I cannot find any easier solution in the documentation. Is there anyway to improve this process?
Upvotes: 1
Views: 446
Reputation: 2478
As long as your calculation depends only on the existing state (pre-defined templates, formulas) of the workbook, you can just have 1 copy of the file in the drive and re-use for many calculations without saving the document. Essentially, you can use Excel file as a calc-engine and use the resulting values from updates.
For this, you'd need to create a session that doesn't save updates before making any API call to modify data.
POST /{version}/me/drive/items/01CYZLFJGUJ7JHBSZDFZFL25KSZGQTVAUN/workbook/createSession
Body:
{ "persistChanges": false }
Response:
{
"@odata.context": "https://graph.microsoft.com/{version}/$metadata#microsoft.graph.sessionInfo",
"id": "{session-id}",
"persistChanges": false
}
The resulting session header should be attached in the following HTTP calls. As long as you do that, it doesn't save your changes.
on next calls, add HTTP header:
workbook-session-id: {session-id}
Note: you may run into session limitation on a given file. If you expect a lot of concurrent requests then you should make copies of the Excel file to distribute the load.
Upvotes: 1