Skip to content

Commit

Permalink
worksheet enhancements & example to return grouped items from a list (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Jan 21, 2024
1 parent d025c1e commit 35faee3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
21 changes: 21 additions & 0 deletions examples/onedrive/excel/get_cell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Gets the range object containing the single cell based on row and column numbers.
https://learn.microsoft.com/en-us/graph/api/worksheet-cell?view=graph-rest-1.0
"""
import sys

from office365.graph_client import GraphClient
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
drive_item = client.me.drive.root.get_by_path("Financial Sample.xlsx")
worksheets = drive_item.workbook.worksheets.get().execute_query()
if len(worksheets) == 0:
sys.exit("No worksheets found")


result = worksheets["Sheet1"].cell(row=1, column=1).execute_query()
print(result.values)
16 changes: 16 additions & 0 deletions examples/sharepoint/listitems/from_folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.listitems.caml.query import CamlQuery
from tests import test_client_credentials, test_team_site_url


def create_custom_query():
qry = CamlQuery()
qry.FolderServerRelativeUrl = "Shared Documents/Archive"
return qry


ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
lib = ctx.web.lists.get_by_title("Documents")
result = lib.get_items(create_custom_query()).execute_query()
for item in result:
print(item.properties)
31 changes: 31 additions & 0 deletions examples/sharepoint/listitems/get_grouped.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Demonstrates how to return distinct values from a List for the specific column, where:
- render_list_data is used to returns the data for the specified query view
"""

import json

from office365.sharepoint.client_context import ClientContext
from tests import test_client_credentials, test_team_site_url

view_xml = """
<View>
<Query>
<GroupBy Collapse="TRUE" GroupLimit="100">
<FieldRef Name="Author"/>
</GroupBy>
</Query>
<ViewFields>
<FieldRef Name="Author"/>
</ViewFields>
<RowLimit Paged="TRUE">100</RowLimit>
</View>
"""

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
lib = ctx.web.lists.get_by_title("Site Pages")
result = lib.render_list_data(view_xml).execute_query()
data = json.loads(result.value)
for item in data.get("Row"):
print(item.get("Author.title"))
14 changes: 14 additions & 0 deletions office365/onedrive/workbooks/worksheets/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ def __repr__(self):
def __str__(self):
return self.name or self.entity_type_name

def cell(self, row, column):
"""Gets the range object containing the single cell based on row and column numbers.
The cell can be outside the bounds of its parent range, so long as it's stays within the worksheet grid.
:param int row: Row number of the cell to be retrieved. Zero-indexed.
:param int column: Column number of the cell to be retrieved. Zero-indexed.
"""
return_type = WorkbookRange(
self.context, ResourcePath("range", self.resource_path)
)
params = {"row": row, "column": column}
qry = FunctionQuery(self, "cell", method_params=params, return_type=return_type)
self.context.add_query(qry)
return return_type

def range(self, address=None):
"""Gets the range object specified by the address or name."""
return_type = WorkbookRange(
Expand Down
2 changes: 1 addition & 1 deletion office365/sharepoint/lists/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def start_recycle(self):

def render_list_data(self, view_xml):
"""
Returns the data for the specified query view.<56> The result is implementation-specific, used for
Returns the data for the specified query view. The result is implementation-specific, used for
providing data to a user interface.
:param str view_xml: Specifies the query as XML that conforms to the ViewDefinition type as specified in
Expand Down

0 comments on commit 35faee3

Please sign in to comment.