Is there any way to Automate to upload files from local to the specific row in smartsheet in bulk?

Options
rye
rye
edited 03/15/24 in API & Developers

Hi,

I tried to write a python code to upload files from local to rows in smartsheet in bulk. The is executed without any output.

Could someone please help me on it to.

Python Code:

import requests
import os


base_url = "https://api.smartsheet.com/2.0"
access_token = "Your_api_token"
sheet_id = "1234567"  # Replace with your sheet ID
attach_url = base_url + '/sheets/{sheet_id}/rows/{row_id}

row_attachments={'file_path':'row_id',
      'file_path_1':'row_id_1',
      'file_path_2':'row_id_2',
.......
}


def upload_attachment(row_id, file_path):
    url = attach_url.format(sheet_id=sheet_id,row_id=row_id)
    headers = {
        "Authorization": f"Bearer {access_token}",
    "Content-Type":"application/octet-stream",
        "Content-Disposition": f"attachment; filename=\'os.path.basename(file_path)\'"
    }

    files ={'file': open(file_path,'rb')}

    response= requests.post(url, headers=headers, files=files)

    if response.status_code == 200:
        print("Attachment uploaded successfully to row", row_id)
    else:
        print(f"Failed to upload attachment to row", row_id)
        print(response.text)


Answers

  • Lee Joramo
    Lee Joramo ✭✭✭✭✭✭
    Options

    I don't have time to dig into this and provide a detailed answer. I will suggest that instead of python's requests module, you should use the Smartsheet Python SDK, it takes care of many issues for you, including dealing with rate-limits and retries.

    There are times you may need to go beyond the SDK, and directly use requests , but I have found this to be rare

  • rye
    rye
    edited 03/18/24
    Options

    Hi @Lee Joramo

    I have tried using Smartsheet Python SDK as well . But I could see same that code is executed without any output.


    Python Code:

    import os
    from smartsheet import Smartsheet
    from smartsheet.models import Sheet, Row, Column, Cell
    
    access_token = 'Your_Token_Access'
    
    # Initialize smartsheet
    ss_client= Smartsheet(access_token)
    
    sheet_id = 'Your_Sheet_ID'
    column_id = 'Your Column ID'
    
    files_and_rows={'file_path':'row_id',
          'File path 1':'Row_1',
          'File path 2':'Row_2'
    }
    
    #Function to upload files to smartsheet
    def upload_files_to_smartsheet(files_and_rows, sheet_id, column_id):
        #Open the sheet
        sheet = ss_client.sheets.get_sheet(sheet_id)
        
       #Iterate over files and rows
        for file_path, row_id in files_and_rows:
            #Create a new row
            new_row = Row()
            new_row.cells.append(Cell(column_id, value = os.path.basename(file_path)))
    
            #Attach the file to row
            response = ss_client.Attachments.attach_file_to_row(sheet_id, row_id,file_path,new_row.cells[0])
    
            if response and response.message:
                print(f"File {file_path} uploaded successfully to row {row_id}")
            else:
                print(f"Failed to upload files")
    
  • Lee Joramo
    Lee Joramo ✭✭✭✭✭✭
    Options

    @rye I will try to give this a review later today. However, I will note that you included what appears to be a real API token. You will want to reset that ASAP. (We have all done this sort of thing.)