Is there any way to automate to change the attachment name by prepending with column name?

I wrote a python code to automate to change the name of attachment in a row by prepending with column name. I was able to run the code but was unable to get any output.
Please suggest if any changes need to be done or any other recommendations.
Python Code:
import requests access_token = "Your_token_number" sheet_id = '' ย # Replace with your sheet ID column_name = '' ย # Replace with your column name #Function to get attachment details from smartsheet def get_attachment_details(access_token, sheet_id): ย ย headers = { ย ย ย ย "Authorization": f"Bearer {access_token}" ย ย } ย ย url = f"https://api.smartsheet.com/2.0/sheets/{sheet_id}/attachments" ย ย response = requests.get(url, headers=headers) ย ย print("Get attachment details request:" , response.request.url) ย ย if response.status_code == 200: ย ย ย ย print("Attachment details retrieved successfully") ย ย ย ย return response.json()["data"] ย ย else: ย ย ย ย print("Failed to retrieve attachment details") ย ย ย ย print(f"Status Code: {response.status_code}") ย ย ย ย print(f"Error msg: {response.text}") ย ย ย ย return[] ย ย #Function to update attachment names by prepending with column_name def update_attachment_name(access_token, sheet_id, attachment_details): ย ย headers = { ย ย ย ย ย "Authorization": f"Bearer {access_token}", ย ย ย ย ย "Content-Type":"application/octet-stream" ย ย } ย ย for attachment in attachment_details: ย ย ย ย column_name = attachment["primaryColumn"]["title"] ย ย ย ย attachment_id = attachment["id"] ย ย ย ย attachament_name = attachment["name"] ย ย ย ย new_attachment_name = f"{column_name}_{attachament_name}" ย ย ย ย payload={ ย ย ย ย ย ย "name" : new_attachment_name ย ย ย ย } ย ย ย ย url = f"https://api.smartsheet.com/2.0/sheets/{sheet_id}/attachments/{attachment_id}" ย ย ย ย response = requests.put(url, headers=headers, json=payload) ย ย ย ย if response.status_code==200: ย ย ย ย ย ย print(f"Attachment '{attachament_name}' renamed to '{new_attachment_name}'") ย ย ย ย else: ย ย ย ย ย ย print(f"Failed to rename attachment '{attachament_name}'") ย ย ย ย ย ย print(f"Status Code: {response.status_code}") ย ย ย ย ย ย print(f"Error msg: {response.text}")
Answers
-
Have used the API to attach files to Rows and Comments, and to download attachments. I have never tried to modify an existing Attachment. From my Reading of the API, I don't think you can do this. Hopefully, someone else can point to a way to do this.
However, you should be able to achieve this via "Attach a New Version". This would effectively accomplish your goal, but adds a few more steps than a simple rename. It looks like the process would be something like this:
- Get ID of attachment
- Download the attachment
- Attach a new version of the file with the New File name and the file data
- optionally: delete the old version
See the API documentation to Attach a New Version
Hint: when editing a message in this forum click the paragraph symbol ยถ to the left to format your text. Use
code block
to format program code with syntax highlighting and appropriate word wrapping. -
@rye Did you ever finalize a solution to this question? I have the exact same need and was excited to copy your code, until I read that it wasn't working :)
-Neil