Created
November 16, 2018 01:49
-
-
Save stmcallister/ddf4e9405daa7e4bd97c8013b60f08e8 to your computer and use it in GitHub Desktop.
List sheets from a workspace in Smartsheet and gather the rowIds and primaryColumnId in the sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import smartsheet | |
import os | |
import json | |
import logging | |
logging.basicConfig(filename='mylog.log', level=logging.DEBUG) | |
ss_client = smartsheet.Smartsheet(os.environ['SMARTSHEET_ACCESS_TOKEN']) | |
ss_client.errors_as_exceptions() | |
def get_primary_column_id(columns): | |
for column in columns: | |
if (column.primary): | |
return column.id | |
try: | |
workplace_id = 111111111111111 | |
workplace = ss_client.Workspaces.get_workspace(workplace_id) | |
wp_sheets = workplace.sheets | |
info_array = [] | |
# loop through sheets | |
for sheet in wp_sheets: | |
# get sheet | |
full_sheet = ss_client.Sheets.get_sheet(sheet.id) | |
# get the primary column | |
primary_column_id = get_primary_column_id(full_sheet.columns) | |
# get row ids | |
for row in full_sheet.rows: | |
info_array.append({'sheet_id': sheet.id, 'row_id': row.id, 'primary_column_id': primary_column_id}) | |
except smartsheet.exceptions.SmartsheetException as e: | |
# SmartsheetException is the base class for all of the exceptions raised by the SDK. | |
# The two most common types of SmartsheetException are ApiError and HttpError. After trapping the exception, first | |
# determine the exception type using an 'isinstance' test. | |
# | |
# In this example, the ApiError exception has an Error class object accessible through the 'error' property, and | |
# then that in turn points to an ErrorResult class accessible through the result property. | |
# | |
# The details of the API error are stored in that ErrorResult. | |
if isinstance(e, smartsheet.exceptions.ApiError): | |
print(e.error.result.error_code) | |
print(e.error.result.message) | |
print(e.error.result.name) | |
print(e.error.result.recommendation) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment