Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

How to upload google doc file or drive file in drive collection using google drive ruby gem?

Ruby • Asked by TOSSIM Brice

In my app, I want to upload google doc file or google drive file in my drive collection using google drive ruby gem. I saw function like upload_from_file(path) but it works when I'm using local file but not when I want to use google doc file or drive file. Somebody to help me please ?

#When I post my form
def create
  folder_url = "https://drive.google.com/drive/folders/"
  spreadsheet_path = "https://docs.google.com/XXX"

  #Connect to the Drive API with credentials json file
  session = GoogleDrive::Session.from_config("#{ENV['CONFIG_PATH']}/config.json")

  test_dev = session.collection_by_url("#{folder_url}XXX")

  folder = test_dev.create_subcollection(params[:title])

  #Get match between doc title and spreadsheet data
  worksheets = retrieve_spreadsheet_datas(session, spreadsheet_path).drop(1)
  worksheets.each do |row|
    name = row[1] + " " + row[2]
    if name == params[:title]
      #row[3] and row[4] contain link like "https://drive.google.com/open?id=XXX"
      if row[3] != nil
        create_folder(session, folder.web_view_link, 1, row[3])
      end
      if row[4] != nil
        create_folder(session, folder.web_view_link, 2, row[4])
      end
    end
  end

  redirect_to("/drive/index")
end

def retrieve_spreadsheet_datas(session, spreadsheet_path)
  spreadsheet = session.spreadsheet_by_url(spreadsheet_path)
  worksheet = spreadsheet.worksheets.first
  worksheet.rows
end

def create_folder(session, url, i, file)
  collection = session.collection_by_url(url) #Open collection
  col_folder = collection.create_subcollection("R#{i}") #Create subcollection in it
  col_folder_content = session.collection_by_url(col_folder.web_view_link)  #Open the new collection

  #And here I want to upload "file (row[3] and row[4])" in "col_folder_content"
end

XXX represent a key. Thanks :)


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 31,152+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.