forked from smartsheet-samples/python-read-write-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-read-write-sheet.py
More file actions
90 lines (63 loc) · 2.76 KB
/
python-read-write-sheet.py
File metadata and controls
90 lines (63 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Install the smartsheet sdk with the command: pip install smartsheet-python-sdk
import smartsheet
import logging
import os.path
# TODO: Set your API access token here, or leave as None and set as environment variable "SMARTSHEET_ACCESS_TOKEN"
access_token = None
_dir = os.path.dirname(os.path.abspath(__file__))
# The API identifies columns by Id, but it's more convenient to refer to column names. Store a map here
column_map = {}
# Helper function to find cell in a row
def get_cell_by_column_name(row, column_name):
column_id = column_map[column_name]
return row.get_column(column_id)
# TODO: Replace the body of this function with your code
# This *example* looks for rows with a "Status" column marked "Complete" and sets the "Remaining" column to zero
#
# Return a new Row with updated cell values, else None to leave unchanged
def evaluate_row_and_build_updates(source_row):
# Find the cell and value we want to evaluate
status_cell = get_cell_by_column_name(source_row, "Status")
status_value = status_cell.display_value
if (status_value == "Complete"):
remaining_cell = get_cell_by_column_name(source_row, "Remaining")
if (remaining_cell.display_value != "0"): # Skip if already 0
print("Need to update row #" + str(source_row.row_number))
# Build new cell value
newCell = ss.models.Cell()
newCell.column_id = column_map["Remaining"]
newCell.value = 0
# Build the row to update
newRow = ss.models.Row()
newRow.id = source_row.id
newRow.cells.append(newCell)
return newRow
return None
print("Starting ...")
# Initialize client
ss = smartsheet.Smartsheet(access_token)
# Make sure we don't miss any error
ss.errors_as_exceptions(True)
# Log all calls
logging.basicConfig(filename='rwsheet.log', level=logging.INFO)
# Import the sheet
result = ss.Sheets.import_xlsx_sheet(_dir + '/Sample Sheet.xlsx', header_row_index=0)
# Load entire sheet
sheet = ss.Sheets.get_sheet(result.data.id)
print ("Loaded " + str(len(sheet.rows)) + " rows from sheet: " + sheet.name)
# Build column map for later reference - translates column names to column id
for column in sheet.columns:
column_map[column.title] = column.id
# Accumulate rows needing update here
rowsToUpdate = []
for row in sheet.rows:
rowToUpdate = evaluate_row_and_build_updates(row)
if (rowToUpdate != None):
rowsToUpdate.append(rowToUpdate)
# Finally, write updated cells back to Smartsheet
if rowsToUpdate:
print("Writing " + str(len(rowsToUpdate)) + " rows back to sheet id " + str(sheet.id))
result = ss.Sheets.update_rows(result.data.id, rowsToUpdate)
else:
print("No updates required")
print ("Done")