Skip to content

Commit

Permalink
Added additional google sheets functions (#15210)
Browse files Browse the repository at this point in the history
<!--
Thanks for taking the time to open a pull request! Please make sure
you've read the "Opening Pull Requests" section of our Contributing
Guide:


https://github.com/Opentrons/opentrons/blob/edge/CONTRIBUTING.md#opening-pull-requests

To ensure your code is reviewed quickly and thoroughly, please fill out
the sections below to the best of your ability!
-->

# Overview

Added batch cell updates to allow for faster data processing

# Test Plan

<!--
Use this section to describe the steps that you took to test your Pull
Request.
If you did not perform any testing provide justification why.

OT-3 Developers: You should default to testing on actual physical
hardware.
Once again, if you did not perform testing against hardware, justify
why.

Note: It can be helpful to write a test plan before doing development

Example Test Plan (HTTP API Change)

- Verified that new optional argument `dance-party` causes the robot to
flash its lights, move the pipettes,
then home.
- Verified that when you omit the `dance-party` option the robot homes
normally
- Added protocol that uses `dance-party` argument to G-Code Testing
Suite
- Ran protocol that did not use `dance-party` argument and everything
was successful
- Added unit tests to validate that changes to pydantic model are
correct

-->

# Changelog

<!--
List out the changes to the code in this PR. Please try your best to
categorize your changes and describe what has changed and why.

Example changelog:
- Fixed app crash when trying to calibrate an illegal pipette
- Added state to API to track pipette usage
- Updated API docs to mention only two pipettes are supported

IMPORTANT: MAKE SURE ANY BREAKING CHANGES ARE PROPERLY COMMUNICATED
-->

# Review requests

<!--
Describe any requests for your reviewers here.
-->

# Risk assessment

<!--
Carefully go over your pull request and look at the other parts of the
codebase it may affect. Look for the possibility, even if you think it's
small, that your change may affect some other part of the system - for
instance, changing return tip behavior in protocol may also change the
behavior of labware calibration.

Identify the other parts of the system your codebase may affect, so that
in addition to your own review and testing, other people who may not
have the system internalized as much as you can focus their attention
and testing there.
-->
  • Loading branch information
rclarke0 authored and Carlos-fernandez committed Jun 3, 2024
1 parent dd3e2d6 commit f4f6367
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions abr-testing/abr_testing/automation/google_sheets_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ def open_worksheet(self, tab_number: int) -> Any:
"""Open individual worksheet within a googlesheet."""
return self.spread_sheet.get_worksheet(tab_number)

def create_worksheet(self, tab_name: int) -> None:
def create_worksheet(self, title: str) -> None:
"""Create a worksheet with tab name. Existing spreadsheet needed."""
try:
self.spread_sheet.add_worksheet(tab_name, rows="1000", cols="26")
new_sheet = self.spread_sheet.add_worksheet(title, rows="2000", cols="26")
return new_sheet.id
except gspread.exceptions.APIError:
print("Work Sheet already exists")
print("Sheet already exists.")

def write_header(self, header: List) -> None:
"""Write Header to first row if not present."""
header_list = self.worksheet.row_values(1)
if header_list != header:
self.worksheet.insert_row(header, self.row_index)

def write_to_row(self, data: List) -> None:
def write_to_row(self, data: List, title: str = "Sheet1") -> None:
"""Write data into a row in a List[] format."""
try:
self.row_index += 1
Expand Down Expand Up @@ -106,11 +107,24 @@ def batch_delete_rows(self, row_indices: List[int]) -> None:
}
self.spread_sheet.batch_update(body=delete_body)

def batch_update_cells(
self, sheet_title: str, data: List[List[str]], start_column: str, start_row: int
) -> None:
"""Writes to multiple cells at once in a specific sheet."""
sheet = self.spread_sheet.worksheet(sheet_title)
for idx, values in enumerate(data):
column = chr(ord(start_column) + idx) # Convert index to column letter
location = f"{column}{start_row}:{column}{start_row + len(values) - 1}"
cells_to_update = sheet.range(location)
for cell, value in zip(cells_to_update, values):
cell.value = value
sheet.update_cells(cells_to_update)

def update_cell(
self, row: int, column: int, single_data: Any
self, sheet_title: str, row: int, column: int, single_data: Any
) -> Tuple[int, int, Any]:
"""Update ONE individual cell according to a row and column."""
self.worksheet.update_cell(row, column, single_data)
self.spread_sheet.worksheet(sheet_title).update_cell(row, column, single_data)
return row, column, single_data

def get_all_data(self) -> List[Dict[str, Any]]:
Expand All @@ -121,6 +135,15 @@ def get_column(self, column_number: int) -> Set[str]:
"""Get all values in column."""
return self.worksheet.col_values(column_number)

def get_cell(self, cell: str) -> Any:
"""Get cell value with location ex: 'A1'."""
return self.worksheet.acell(cell).value

def get_single_col_range(self, range: str) -> List:
"""Get cell values from one column range."""
values_range = self.worksheet.range(range)
return [cell.value for cell in values_range]

def get_index_row(self) -> int:
"""Check for the next available row to write too."""
row_index = len(self.get_column(1))
Expand Down Expand Up @@ -166,6 +189,9 @@ def create_line_chart(
titles: List[str],
series: List[Dict[str, Any]],
domains: List[Dict[str, Any]],
axis: Dict[str, Any],
col_position: int = 0,
sheet_id: str = "0",
) -> None:
"""Create chart of data on google sheet."""
request_body = {
Expand All @@ -178,10 +204,7 @@ def create_line_chart(
"basicChart": {
"chartType": "LINE",
"legendPosition": "RIGHT_LEGEND",
"axis": [
{"position": "BOTTOM_AXIS", "title": titles[1]},
{"position": "LEFT_AXIS", "title": titles[2]},
],
"axis": axis,
"domains": domains,
"series": series,
"headerCount": 1,
Expand All @@ -190,9 +213,9 @@ def create_line_chart(
"position": {
"overlayPosition": {
"anchorCell": {
"sheetId": 0,
"sheetId": sheet_id,
"rowIndex": 1,
"columnIndex": 1,
"columnIndex": col_position,
}
}
},
Expand Down
File renamed without changes.

0 comments on commit f4f6367

Please sign in to comment.