33Note: Support for workbooks stored in OneDrive Consumer platform is still not available.
44At this time, only the files stored in business platform is supported by Excel REST APIs.
55"""
6- import json
76import logging
87import datetime as dt
98from urllib .parse import quote
@@ -1663,6 +1662,76 @@ def get_named_range(self, name):
16631662 return self .named_range_constructor (parent = self , ** {self ._cloud_data_key : response .json ()})
16641663
16651664
1665+ class WorkbookApplication (ApiComponent ):
1666+ _endpoints = {
1667+ 'get_details' : '/application' ,
1668+ 'post_calculation' : '/application/calculate'
1669+ }
1670+
1671+ def __init__ (self , workbook_or_id , * , con = None , ** kwargs ):
1672+ """
1673+ Create A WorkbookApplication representation
1674+
1675+ :param workbook_or_id: Either a workbook object, or the id of the workbook you want to interact with
1676+ :param parent: parent for this operation
1677+ :param Connection con: connection to use if no parent specified
1678+ """
1679+
1680+ if isinstance (workbook_or_id , WorkBook ) and con :
1681+ raise ValueError ('Need a workbook or a connection but not both' )
1682+
1683+ if isinstance (workbook_or_id , WorkBook ):
1684+ self .con = workbook_or_id .session .con
1685+ self .workbook_id = workbook_or_id .object_id .split (":" ).pop ()
1686+ main_resource = getattr (workbook_or_id , 'main_resource' , None )
1687+
1688+ elif isinstance (workbook_or_id , str ):
1689+ self .con = con
1690+ self .workbook_id = workbook_or_id
1691+ main_resource = kwargs .pop ('main_resource' , None )
1692+
1693+ else :
1694+ raise ValueError ("workbook_or_id was not an accepted type: Workbook or string" )
1695+
1696+ # Choose the main_resource passed in kwargs over parent main_resource
1697+
1698+ super ().__init__ (
1699+ protocol = workbook_or_id .protocol if isinstance (workbook_or_id , WorkBook ) else kwargs .get ('protocol' ),
1700+ main_resource = main_resource )
1701+
1702+ def __str__ (self ):
1703+ return self .__repr__ ()
1704+
1705+ def __repr__ (self ):
1706+ return 'WorkbookApplication for Workbook: {}' .format (self .workbook_id or 'Not set' )
1707+
1708+ def __bool__ (self ):
1709+ return self .workbook_id is not None
1710+
1711+ def get_details (self ):
1712+ """ Gets workbookApplication """
1713+ url = self .build_url (self ._endpoints .get ('get_details' )).format (id = quote (self .workbook_id ))
1714+ response = self .con .get (url )
1715+
1716+ if not response :
1717+ return None
1718+ return response .json ()
1719+
1720+ def run_calculations (self , calculation_type ):
1721+ if calculation_type not in ["Recalculate" , "Full" , "FullRebuild" ]:
1722+ raise ValueError ("calculation type must be one of: Recalculate, Full, FullRebuild" )
1723+
1724+ url = self .build_url (self ._endpoints .get ('post_calculation' ).format (id = quote (self .workbook_id )))
1725+ data = {"calculationType" : calculation_type }
1726+ headers = {"Content-type" : "application/json" }
1727+
1728+ response = self .con .post (url , headers = headers , data = data )
1729+ if not response :
1730+ return False
1731+
1732+ return response .ok
1733+
1734+
16661735class WorkBook (ApiComponent ):
16671736 _endpoints = {
16681737 'get_worksheets' : '/worksheets' ,
@@ -1675,6 +1744,8 @@ class WorkBook(ApiComponent):
16751744 'add_named_range' : '/names/add' ,
16761745 'add_named_range_f' : '/names/addFormulaLocal' ,
16771746 }
1747+
1748+ application_constructor = WorkbookApplication
16781749 worksheet_constructor = WorkSheet
16791750 table_constructor = Table
16801751 named_range_constructor = NamedRange
@@ -1742,6 +1813,9 @@ def get_table(self, id_or_name):
17421813 return None
17431814 return self .table_constructor (parent = self , ** {self ._cloud_data_key : response .json ()})
17441815
1816+ def get_workbookapplication (self ):
1817+ return self .application_constructor (self )
1818+
17451819 def get_worksheets (self ):
17461820 """ Returns a collection of this workbook worksheets"""
17471821
@@ -1834,71 +1908,3 @@ def add_named_range(self, name, reference, comment='', is_formula=False):
18341908 return None
18351909 return self .named_range_constructor (parent = self , ** {self ._cloud_data_key : response .json ()})
18361910
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