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}")