|
3 | 3 | Note: Support for workbooks stored in OneDrive Consumer platform is still not available. |
4 | 4 | At this time, only the files stored in business platform is supported by Excel REST APIs. |
5 | 5 | """ |
6 | | - |
| 6 | +import json |
7 | 7 | import logging |
8 | 8 | import datetime as dt |
9 | 9 | from urllib.parse import quote |
@@ -1833,3 +1833,72 @@ def add_named_range(self, name, reference, comment='', is_formula=False): |
1833 | 1833 | if not response: |
1834 | 1834 | return None |
1835 | 1835 | 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 |
0 commit comments