Creating rows as children using the API
I've successfully created a Python script to update a sheet in Smartsheets based off a table in postgres. The postgres table updates every hour, so I have the code running every hour after the update. It works great. I have the code set up to create my dataframe, delete all the rows in the sheet, then append all the new rows from postgres, basically:
create df clear sheet append new rows
My table is about 5,000 rows, so it takes about 1.5 minutes to run. Mostly for practice (and a slight nudge by the database admin), I'm trying to reduce that time. One idea I was working on is to create all the new rows as children to a single parent row -- then, instead of deleting all the rows, I would just delete the parent.
What I'm trying to do now is this:
create df clear sheet by deleting single parent row create new header row and get ID for that row append new rows as children to new header row
To do this, I create a single row at the top of the page with the content "Header" in the primary column. I retrieve the row ID for that row, which I assign to the variable parent_row. I thought I could just set parent_id to parent_row when I was uploading all the new rows as follows:
for i, rows in df.iterrows(): temp_row = ss.models.Row() temp_row.to_bottom = True for j, columns in enumerate(df): temp_row.cells.append({ 'column_id': list_of_columns[j], 'value': str(df.iat[i,j]), 'parent_id': parent_row }) for x in range(0, len(list_of_rows), chunk_interval): ss.Sheets.add_rows(sheet_id, list_of_rows[x:x + chunk_interval])
The code doesn't error out, but the newly created rows are not indented under the header row. What do I need to do to get them to indent as children to that parent row? Thanks!
Best Answer
-
Got it! I was trying to assign the parent ID at the cell level, not the row level. Did this instead and it worked:
for i, rows in df.iterrows(): temp_row = ss.models.Row() temp_row.to_bottom = True temp_row.parent_id = parent_row for j, columns in enumerate(df): temp_row.cells.append({ 'column_id': list_of_columns[j], 'value': str(df.iat[i,j]) }) for x in range(0, len(list_of_rows), chunk_interval): ss.Sheets.add_rows(sheet_id, list_of_rows[x:x + chunk_interval])
Answers
-
Got it! I was trying to assign the parent ID at the cell level, not the row level. Did this instead and it worked:
for i, rows in df.iterrows(): temp_row = ss.models.Row() temp_row.to_bottom = True temp_row.parent_id = parent_row for j, columns in enumerate(df): temp_row.cells.append({ 'column_id': list_of_columns[j], 'value': str(df.iat[i,j]) }) for x in range(0, len(list_of_rows), chunk_interval): ss.Sheets.add_rows(sheet_id, list_of_rows[x:x + chunk_interval])
Categories
- All Categories
- 14 Welcome to the Community
- Customer Resources
- 64.9K Get Help
- 441 Global Discussions
- 139 Industry Talk
- 471 Announcements
- 4.9K Ideas & Feature Requests
- 129 Brandfolder
- 148 Just for fun
- 68 Community Job Board
- 495 Show & Tell
- 33 Member Spotlight
- 2 SmartStories
- 300 Events
- 36 Webinars
- 7.3K Forum Archives