Skip to content

Commit 2e07753

Browse files
committed
Added WorkbooApplication, needs more work
1 parent b7b71d4 commit 2e07753

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

O365/excel.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Note: Support for workbooks stored in OneDrive Consumer platform is still not available.
44
At this time, only the files stored in business platform is supported by Excel REST APIs.
55
"""
6-
6+
import json
77
import logging
88
import datetime as dt
99
from urllib.parse import quote
@@ -1833,3 +1833,72 @@ def add_named_range(self, name, reference, comment='', is_formula=False):
18331833
if not response:
18341834
return None
18351835
return self.named_range_constructor(parent=self, **{self._cloud_data_key: response.json()})
1836+
1837+
1838+
class WorkbookApplication(ApiComponent):
1839+
_endpoints = {
1840+
'get_workbookapplication': '/{id}/workbook/application',
1841+
'post_calculation': '/{id}/workbook/application/calculate'
1842+
}
1843+
1844+
def __init__(self, workbook_or_id, *, parent=None, con=None, **kwargs):
1845+
"""
1846+
Create A WorkbookApplication representation
1847+
1848+
:param workbook_or_id: Either a workbook object, or the id of the workbook you want to interact with
1849+
:param parent: parent for this operation
1850+
:param Connection con: connection to use if no parent specified
1851+
"""
1852+
1853+
if parent and con:
1854+
raise ValueError('Need a parent or a connection but not both')
1855+
1856+
self.con = parent.con if parent else con
1857+
1858+
# Assign the workbook id
1859+
if isinstance(workbook_or_id, WorkBook):
1860+
self.workbook_id.object_id.split(":").last()
1861+
1862+
else:
1863+
# Should probably do some more thorough testing
1864+
self.workbook_id = workbook_or_id
1865+
1866+
# Choose the main_resource passed in kwargs over parent main_resource
1867+
main_resource = kwargs.pop('main_resource', None) or (
1868+
getattr(parent, 'main_resource', None) if parent else None)
1869+
1870+
super().__init__(
1871+
protocol=parent.protocol if parent else kwargs.get('protocol'),
1872+
main_resource=main_resource)
1873+
1874+
def __str__(self):
1875+
return self.__repr__()
1876+
1877+
def __repr__(self):
1878+
return 'WorkbookApplication for Workbook: {}'.format(self.workbook_id or 'Not set')
1879+
1880+
def __bool__(self):
1881+
return self.workbook_id is not None
1882+
1883+
def get_workbookapplication(self):
1884+
""" Gets workbookApplication """
1885+
url = self.build_url(self._endpoints.get('get_workbookapplication').format(id=quote(self.workbook_id)))
1886+
headers = {
1887+
1888+
}
1889+
response = self.con.get(url, headers)
1890+
if not response:
1891+
return None
1892+
return response.json()
1893+
1894+
def run_calculations(self, calculation_type):
1895+
if calculation_type not in ["Recalculate", "Full", "FullRebuild"]:
1896+
raise ValueError("calculation type must be one of: Recalculate, Full, FullRebuild")
1897+
1898+
url = self.build_url(self._endpoints.get('get_workbookapplication').format(id=quote(self.workbook_id)))
1899+
body = json.dumps({"calculationType": calculation_type})
1900+
1901+
response = self.con.post(url, body)
1902+
if not response:
1903+
return False
1904+
return response.status_code == 200

tests/test_excel.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from O365 import Account, MSGraphProtocol, Connection
2+
from O365.excel import WorkbookApplication
3+
from tests.config import CLIENT_ID, CLIENT_SECRET
4+
5+
6+
class Workbook(object):
7+
pass
8+
9+
10+
class TestWorkbookApplication:
11+
12+
def setup_class(self):
13+
# credentials = ("client id", "client secret")
14+
self.credentials = (CLIENT_ID, CLIENT_SECRET)
15+
self.workbook_id = "B6EF05E2E8ABA433!646"
16+
self.protocol = MSGraphProtocol()
17+
con = Connection(self.credentials)
18+
main_resource = "drive/items"
19+
20+
# Create Workbook Application
21+
self.workbook_app = WorkbookApplication(workbook_or_id=self.workbook_id, con=con, protocol=self.protocol, main_resource=main_resource)
22+
23+
def teardown_class(self):
24+
pass
25+
26+
def test_with_workbook(self):
27+
# wb = Workbook()
28+
pass
29+
30+
def test_with_parent(self):
31+
pass
32+
33+
def test_get(self):
34+
res = self.workbook_app.get_workbookapplication()
35+
print(res)
36+
37+
def test_run_calculations(self):
38+
res = self.workbook_app.run_calculations("Recalculate")
39+
print(res)

0 commit comments

Comments
 (0)