I have over 100 sheets in a smartsheet workspace. I'd like to use the python api to loop through each sheet and append the row id, sheet id, and the primary column into an array or pandas data frame.
import requests
import pandas as pd
import io
import warnings
warnings.filterwarnings(action='once')
import smartsheet
import os.path
from time import gmtime, strftime
import pandas as pd
import numpy as np
import json
import requests
import hashlib
fullList = pd.DataFrame()
for sheet in sheetArray:
r = requests.get(baseURL + "/" + str(sheet), headers=headers)
rows = json.loads(r.text)
rows = rows['rows']
rowsDF = pd.DataFrame.from_dict(rows)
dropCols = ["cells","createdAt", "expanded", "modifiedAt","siblingId"]
rowsDF = rowsDF.drop(dropCols, axis=1)
fullList.append(rowsDF)
I'm not sure about pandas, but I can help you get the information into a python array.
Using the Smartsheet Python SDK you'll want to first install the SDK, then
import smartsheet
.Next, initialize a Smartsheet object with your access token like so
Grab your Workspace
Grab the sheets from the Workspace
Initialize the array you're creating
Loop over the sheets from the Workspace object. These sheet objects only have a few fields to identify the sheet, so you'll need to use the
sheet.id
to get the full sheet from the Smartsheet API.Grab the primary column for the sheet
The
get_primary_column_id()
function would look like this. The column objects have a boolean field forprimary
. Find the column withprimary
set to true.Grab the row Ids and append all the info to the
info_array
.Here's the Gist.