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:
对于我,罗ws 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
-
Lucas Rayala ✭✭✭✭✭
Got it! I was trying to assign the parent ID at the cell level, not the row level. Did this instead and it worked:
对于我,罗ws 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
-
Lucas Rayala ✭✭✭✭✭
Got it! I was trying to assign the parent ID at the cell level, not the row level. Did this instead and it worked:
对于我,罗ws 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
=IFERROR(INDEX(DISTINCT(LOWER(<\/strong>COLLECT(.....................))<\/strong>), 1), \"//www.santa-greenland.com/community/discussion/101417/\")<\/p> I haven't tested it yet, but I feel like it should work.<\/p>"},{"commentID":386435,"body":" Right. There are already a number of posts here in the community detailing how to pull a distinct or unique list from another sheet via formula. It usually involves the formula above without the LOWER function. The number 1 would output the first distinct value. Changing that to a 2 will output the second distinct value, so on and so forth. I usually use a helper column with the numbers manually entered so I can apply it as a column formula.<\/p>"}]}},"status":{"statusID":3,"name":"Accepted","state":"closed","recordType":"discussion","recordSubType":"question"},"bookmarked":false,"unread":false,"category":{"categoryID":320,"name":"API & Developers","url":"https:\/\/community.smartsheet.com\/categories\/api-developers","allowedDiscussionTypes":[]},"reactions":[{"tagID":3,"urlcode":"Promote","name":"Promote","class":"Positive","hasReacted":false,"reactionValue":5,"count":0},{"tagID":5,"urlcode":"Insightful","name":"Insightful","class":"Positive","hasReacted":false,"reactionValue":1,"count":0},{"tagID":11,"urlcode":"Up","name":"Vote Up","class":"Positive","hasReacted":false,"reactionValue":1,"count":0},{"tagID":13,"urlcode":"Awesome","name":"Awesome","class":"Positive","hasReacted":false,"reactionValue":1,"count":0}],"tags":[{"tagID":369,"urlcode":"bridge-by-smartsheet","name":"Bridge"}]},{"discussionID":107674,"type":"question","name":"API PUT and Body help for Cell link from a cell in sheet \"a\" to sheet \"b\"","excerpt":"I am having trouble linking a cell from sheet a to b. I am new to using API and possibly just getting the call wrong? or the body. Any help would be appreciated. PUT: https:\/\/api.smartsheet.com\/2.0\/sheets\/4098048888098692\/rows\/1370574824361860\/cells\/259016080 Body: { \"cells\": [ { \"linkinFromCell\": {…","snippet":"I am having trouble linking a cell from sheet a to b. I am new to using API and possibly just getting the call wrong? or the body. Any help would be appreciated. PUT:…","categoryID":320,"dateInserted":"2023-07-17T16:23:24+00:00","dateUpdated":"2023-07-18T10:24:44+00:00","dateLastComment":"2023-07-18T16:45:05+00:00","insertUserID":135530,"insertUser":{"userID":135530,"name":"Carlo Rodriguez","url":"https:\/\/community.smartsheet.com\/profile\/Carlo%20Rodriguez","photoUrl":"https:\/\/us.v-cdn.net\/6031209\/uploads\/defaultavatar\/nWRMFRX6I99I6.jpg","dateLastActive":"2023-07-21T15:50:31+00:00","banned":0,"punished":0,"private":false,"label":"✭✭"},"updateUserID":91566,"lastUserID":135530,"lastUser":{"userID":135530,"name":"Carlo Rodriguez","url":"https:\/\/community.smartsheet.com\/profile\/Carlo%20Rodriguez","photoUrl":"https:\/\/us.v-cdn.net\/6031209\/uploads\/defaultavatar\/nWRMFRX6I99I6.jpg","dateLastActive":"2023-07-21T15:50:31+00:00","banned":0,"punished":0,"private":false,"label":"✭✭"},"pinned":false,"pinLocation":null,"closed":false,"sink":false,"countComments":2,"countViews":51,"score":null,"hot":3379310909,"url":"https:\/\/community.smartsheet.com\/discussion\/107674\/api-put-and-body-help-for-cell-link-from-a-cell-in-sheet-a-to-sheet-b","canonicalUrl":"https:\/\/community.smartsheet.com\/discussion\/107674\/api-put-and-body-help-for-cell-link-from-a-cell-in-sheet-a-to-sheet-b","format":"Rich","tagIDs":[472,563],"lastPost":{"discussionID":107674,"commentID":385785,"name":"Re: API PUT and Body help for Cell link from a cell in sheet \"a\" to sheet \"b\"","url":"https:\/\/community.smartsheet.com\/discussion\/comment\/385785#Comment_385785","dateInserted":"2023-07-18T16:45:05+00:00","insertUserID":135530,"insertUser":{"userID":135530,"name":"Carlo Rodriguez","url":"https:\/\/community.smartsheet.com\/profile\/Carlo%20Rodriguez","photoUrl":"https:\/\/us.v-cdn.net\/6031209\/uploads\/defaultavatar\/nWRMFRX6I99I6.jpg","dateLastActive":"2023-07-21T15:50:31+00:00","banned":0,"punished":0,"private":false,"label":"✭✭"}},"breadcrumbs":[{"name":"Home","url":"https:\/\/community.smartsheet.com\/"},{"name":"Get Help","url":"https:\/\/community.smartsheet.com\/categories\/get-help"},{"name":"API & Developers","url":"https:\/\/community.smartsheet.com\/categories\/api-developers"}],"groupID":null,"statusID":3,"attributes":{"question":{"status":"accepted","dateAccepted":"2023-07-18T16:45:08+00:00","dateAnswered":"2023-07-18T11:38:00+00:00","acceptedAnswers":[{"commentID":385713,"body":"
<\/p>