unable to move a row in Python

Options
Bryan Langley
Bryan Langley โœญโœญโœญ

I use Smartsheet to govern a project that does ~10k tasks per year so automating as much of the project is extremely helpful. I have multiple python scripts that are updating sheets and grabbing data form sheets but this is the first time i have tried to move data in a sheet.

I am trying to move a row below another row when the text in the primary cell of one row is completely in another. this way that card will always be directly below the other. this way the "Vent" card is always next to the task card that requires the vent. the code is pulling all the right data and rows and then assigning the proper rows to move but the move fails with the following error

{"response": {"statusCode": 500, "reason": "Internal Server Error", "content": {"errorCode": 4000, "message": "An unexpected error has occurred. Please contact the Support team at https://help.smartsheet.com/contact for assistance.", "refId": "cs5d1q"}}}

this is the response return

response = <smartsheet.models.error.Error object at 0x0000025AC047CA50>,

sheet_id = 2763833039867780,

directive = <smartsheet.models.copy_or_move_row_directive.CopyOrMoveRowDirective object at 0x0000025ABFEBB0E0>

this is my code:

smartsheet_client = smartsheet.Smartsheet(access_token)

sheet_id = 2763833039867780

sheet = smartsheet_client.Sheets.get_sheet(sheet_id)

tool_col_id = next((col.id for col in sheet.columns if col.title == "Tool"), None)
task_col_id = next((col.id for col in sheet.columns if col.title == "Task"), None)
if not tool_col_id or not task_col_id:
raise Exception("Columns 'Tool' and 'Task' not found.")

tool_task_map = {}
for row in sheet.rows:
tool_value = next((cell.value for cell in row.cells if cell.column_id == tool_col_id), None)
task_value = next((cell.value for cell in row.cells if cell.column_id == task_col_id), None)
if tool_value and task_value:
tool_task_map[row.id] = (tool_value, task_value)

rows_to_move = []
for row_id, (tool_value, task_value) in tool_task_map.items():
for other_id, (other_tool_value, other_task_value) in tool_task_map.items():
if (
row_id != other_id and
tool_value == other_tool_value and
task_value in other_task_value
):
rows_to_move.append((row_id, other_id))
break

for row_id, target_id in rows_to_move:
directive = smartsheet.models.CopyOrMoveRowDirective()
directive.row_ids = [row_id]
directive.to = smartsheet.models.CopyOrMoveRowDestination()
directive.to.row_id = target_id
directive.to.position = 'Below'

response = smartsheet_client.Sheets.move_rows(sheet_id, directive)
print(f"Moved row {row_id} below {target_id}: {response.message}")

print("All matching rows processed.")

any help would be much appreciated

Best Answer

  • Bryan Langley
    Bryan Langley โœญโœญโœญ
    Answer โœ“

    i have found a solution to the problem, if i move the desired rows to a different sheet and then move them back to the original sheet in the desired order then it works.

Answers

  • Isaac A.
    Isaac A. Employee
    edited 06/09/25

    Hi @Bryan Langley!

    Currently, Smartsheet Support does not handle SDK-related inquiries. If youโ€™ve tested and found an issue with the SDK, please report it on the relevant GitHub page. You can access the links to these pages in our API documentation here: Smartsheet API Documentation.

    Should you not get a response from the Community, consider checking out StackOverflow to connect with other developers to get more help.


    Cheers,
    Isaac.

    Need more information? ๐Ÿ‘€ |Help and Learning Center
    ใ“ใ‚“ใซใกใฏ (Konnichiwa), Hallo, Hola, Bonjour, Olรก, Ciao! ๐Ÿ‘‹ |Global Discussions

  • Bryan Langley
    Bryan Langley โœญโœญโœญ

    the error

    {"response": {"statusCode": 500, "reason": "Internal Server Error", "content": {"errorCode": 4000, "message": "An unexpected error has occurred. Please contact the Support team at https://help.smartsheet.com/contact for assistance.", "refId": "cs5d1q"}}}

    told me to contact smartsheet support so i have a case open as well. seems to be a row problem. i will continue to mess around with it to understand why it cant be moved

  • Bryan Langley
    Bryan Langley โœญโœญโœญ
    Answer โœ“

    i have found a solution to the problem, if i move the desired rows to a different sheet and then move them back to the original sheet in the desired order then it works.

  • Isaac A.
    Isaac A. Employee

    That's awesome! I'm glad you figured it out. Thank you for sharing, @Bryan Langley !

    Need more information? ๐Ÿ‘€ |Help and Learning Center
    ใ“ใ‚“ใซใกใฏ (Konnichiwa), Hallo, Hola, Bonjour, Olรก, Ciao! ๐Ÿ‘‹ |Global Discussions

NEW Smartsheet API Documentation - bookmark the updated link! https://developers.smartsheet.com