Skip to content

Commit 35faee3

Browse files
committed
worksheet enhancements & example to return grouped items from a list (#806)
1 parent d025c1e commit 35faee3

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

Diff for: examples/onedrive/excel/get_cell.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Gets the range object containing the single cell based on row and column numbers.
3+
4+
https://learn.microsoft.com/en-us/graph/api/worksheet-cell?view=graph-rest-1.0
5+
"""
6+
import sys
7+
8+
from office365.graph_client import GraphClient
9+
from tests import test_client_id, test_password, test_tenant, test_username
10+
11+
client = GraphClient.with_username_and_password(
12+
test_tenant, test_client_id, test_username, test_password
13+
)
14+
drive_item = client.me.drive.root.get_by_path("Financial Sample.xlsx")
15+
worksheets = drive_item.workbook.worksheets.get().execute_query()
16+
if len(worksheets) == 0:
17+
sys.exit("No worksheets found")
18+
19+
20+
result = worksheets["Sheet1"].cell(row=1, column=1).execute_query()
21+
print(result.values)

Diff for: examples/sharepoint/listitems/from_folder.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from office365.sharepoint.client_context import ClientContext
2+
from office365.sharepoint.listitems.caml.query import CamlQuery
3+
from tests import test_client_credentials, test_team_site_url
4+
5+
6+
def create_custom_query():
7+
qry = CamlQuery()
8+
qry.FolderServerRelativeUrl = "Shared Documents/Archive"
9+
return qry
10+
11+
12+
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
13+
lib = ctx.web.lists.get_by_title("Documents")
14+
result = lib.get_items(create_custom_query()).execute_query()
15+
for item in result:
16+
print(item.properties)

Diff for: examples/sharepoint/listitems/get_grouped.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Demonstrates how to return distinct values from a List for the specific column, where:
3+
- render_list_data is used to returns the data for the specified query view
4+
5+
"""
6+
7+
import json
8+
9+
from office365.sharepoint.client_context import ClientContext
10+
from tests import test_client_credentials, test_team_site_url
11+
12+
view_xml = """
13+
<View>
14+
<Query>
15+
<GroupBy Collapse="TRUE" GroupLimit="100">
16+
<FieldRef Name="Author"/>
17+
</GroupBy>
18+
</Query>
19+
<ViewFields>
20+
<FieldRef Name="Author"/>
21+
</ViewFields>
22+
<RowLimit Paged="TRUE">100</RowLimit>
23+
</View>
24+
"""
25+
26+
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
27+
lib = ctx.web.lists.get_by_title("Site Pages")
28+
result = lib.render_list_data(view_xml).execute_query()
29+
data = json.loads(result.value)
30+
for item in data.get("Row"):
31+
print(item.get("Author.title"))

Diff for: office365/onedrive/workbooks/worksheets/worksheet.py

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ def __repr__(self):
2525
def __str__(self):
2626
return self.name or self.entity_type_name
2727

28+
def cell(self, row, column):
29+
"""Gets the range object containing the single cell based on row and column numbers.
30+
The cell can be outside the bounds of its parent range, so long as it's stays within the worksheet grid.
31+
:param int row: Row number of the cell to be retrieved. Zero-indexed.
32+
:param int column: Column number of the cell to be retrieved. Zero-indexed.
33+
"""
34+
return_type = WorkbookRange(
35+
self.context, ResourcePath("range", self.resource_path)
36+
)
37+
params = {"row": row, "column": column}
38+
qry = FunctionQuery(self, "cell", method_params=params, return_type=return_type)
39+
self.context.add_query(qry)
40+
return return_type
41+
2842
def range(self, address=None):
2943
"""Gets the range object specified by the address or name."""
3044
return_type = WorkbookRange(

Diff for: office365/sharepoint/lists/list.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def start_recycle(self):
301301

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

0 commit comments

Comments
 (0)