I’d like to save an Excel file to dropbox using Rails and randym / axlsx.
I can create a Axlsx::Package object, I can even serialize it, so save it to my disk in development env.
But I’d like to upload it to dropbox using the futuresimple/dropbox-api.
How hard could it be?
def save_in_dropbox(project)
Dropbox::API::Config.app_key = ENV['DROPBOX_APP_KEY']
Dropbox::API::Config.app_secret = ENV['DROPBOX_APP_SECRET']
Dropbox::API::Config.mode = ENV['DROPBOX_ACCESS_TYPE']
dropbox_client = Dropbox::API::Client.new(:token => ENV['DROPBOX_CLIENT_TOKEN'], :secret => ENV['DROPBOX_CLIENT_SECRET'])
logger.info "#{dropbox_client.account}"
# create Excel file
p = Axlsx::Package.new
p.workbook.add_worksheet(:name => "Basic Worksheet") do |sheet|
sheet.add_row ["First Column", "Second", "Third"]
sheet.add_row [1, 2, 3]
end
p.use_shared_strings = true
# Next line causes Error: undefined method `length' for true:TrueClass
# because p.serialize('file.xlsx')) is true
# but I don't want to save to my disk, on heroku I couldn't
# I want to save to my dropbox
dropbox_client.upload("#{ENV['DROPBOX_ROOT_PATH']}/file.xlsx", p.serialize('file.xlsx'))
#p.serialize('c:\file.xlsx') # that saves it to my development env disk :-(
#uploaded_file = dropbox_client.upload "#{ENV['DROPBOX_ROOT_PATH']}/file.txt", "#{1.second.ago.to_s}\n\r#{project.title}" # Upload to Dropbox works fine
end
I would recommend that you use Package#to_stream in this case. It will give you the IO object that you can then read into the upload call.
The alternative is to write it to disk with Package#serialize, and then read it from the file system.
I am not familiar with the dropbox_client, but I would be surprised if it did not handle an IO stream correctly.
Best
-randym