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

  • Lee Joramo
    Lee Joramo ✭✭✭✭✭✭
    edited 03/15/24

    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:

    1. Get ID of attachment
    2. Download the attachment
    3. Attach a new version of the file with the New File name and the file data
    4. 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.