How to add dropdown in google sheet using axlsx gem? - ruby-on-rails

I am using axlsx gem for the excel sheets in rails. I have a dropdown for source values. However,
it's working fine in Libre Office, but not in Google sheet. Also, I don't understand why Err 509 is there in the dropdown.
Here's the code:
source = Source.all.pluck(:name)
source = source.join(', ')
sheet.add_data_validation("F2:F10", {
:type => :list,
:formula1 => source,
:showDropDown => false,
:showInputMessage => true,
:promptTitle => 'Select source',
:prompt => 'Please select a valid source'
})
The gems are:
gem 'caxlsx'
gem 'caxlsx_rails'

Related

Issue with opening the generated file in MS Excel using axlsx_rails

I am using axlsx_rails in my Rails project to generate an excel file with validations and dropdowns. After generating the File, I tried to open the file in MS Excel and it said that it needs to repair the file and all the validations and dropdowns are cleared off.
Versions
axlsx_rails - 0.1.5
axlsx - 1.3.6
rubyzip - 1.2.0
rails - 4.1.2
ruby - 2.2.2
Code
#excel = Axlsx::Package.new
#excel.workbook.add_worksheet(name: "Samples") do |ws|
ws.add_row #field_names
sheet.add_data_validation("G2:G1000", {
:type => :list,
:formula1 => '"Jim", "Tom", "Jack"',
:showDropDown => false,
:showErrorMessage => true,
:errorTitle => '',
:error => 'Please use the dropdown selector to choose the value',
:errorStyle => :stop,
:showInputMessage => true,
:prompt => 'Choose the value from the dropdown'})
end
#excel.serialize "tmp/SampleTemplate.xlsx"
Sending the file using
send_file "tmp/SampleTemplate.xlsx"
The generated file is working in LibreOffice but not in MSExcel
Please help
Solved. The issue is in the line,
:formula_1 => '"Jim", "Tom","Jack"'
I've changed it to,
:formula_1 => '"Jim, Tom, Jack"'

gem rich don't apperas in Active_admin

I installed gem rich in my Rails 4.1.5 project. I'm using Active Admin. In my file of admin I have this line that calls rich:
f.input :content, :as => :rich, :config => { :height => '200px' }, :label => 'Conteúdo'
But when I open Active_admin, the rich don't appears (should appear right below the first field):
My gemfile:
gem 'rich', :git => 'https://github.com/kreativgebiet/rich.git'
In my application.rb I have this lines:
config.assets.prefix = '/lince/assets'
config.relative_url_root = '/lince'
config.action_controller.relative_url_root = '/lince'
When I try without this lines, rich works fine.
Anyone can help me? I can't understand why this not working.
Thanks.
rich has not been updated in years. I recommend you look at other WYSIWYG editors

Convert CSV to Array?

Any ideas on how to convert this CSV into a ruby array using vim?
Starting CSV:
Year,Make,Model
1997,Ford,E350
2000,Mercury,Cougar
Desired Array:
car_info = [
{'Year' => '1997', 'Make' => 'Ford', 'Model' => 'E350'},
{'Year' => '2000', 'Make' => 'Mercury', 'Model' => 'Cougar'},
]
I have > 2000 entries like the CSV above, and I'd love a way to quickly re-format it for use in my Rails app. I'd like to use vim, but I'm open to other options too.
FasterCSV.read("path/to/file.csv", :headers => true).map do |row|
{ "Year" => row[0], "Make" => row[1], "Model" => row[2] }
end
PS: Install faster_csv gem
In vim, you can use global search and replace with a regular expression:
:g/\(.*\),\(.*\),\(.*\)/s//{'Year' => '\1', 'Make' => '\2', 'Model' => '\3'}/g
Then edit the first and last lines of the resulting file accordingly.

Ruby-Rails Spreadsheet "corrupted" under Microsoft Excel 2010

I am using Spreadsheet gem to allow users to export data with .xls format. The code I am showing is allowing users to open the file in Open Office and MS Excel for MAC, but not for Windows, which I get a "file is corrupted" error.
I have a method that do the XLS export like this:
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet(:name => "Expired Activity")
# Title
title_format = Spreadsheet::Format.new(:size => 12, :pattern => 1, :pattern_fg_color => :blue, :align => :merge)
for i in 0..5
sheet1.row(0).set_format(i, title_format)
end
sheet1.row(0).push "Activity"
# Header row
header_format = Spreadsheet::Format.new(:size => 11, :pattern => 1, :pattern_fg_color => :yellow, :align => :merge)
index_row = sheet1.row(1)
index_row.push("Date")
index_row.push("First Name")
index_row.push("Last Name")
for i in 0..2
index_row.set_format(i, header_format)
end
# Row values
row_iterator = 2
#expired.each do |expired|
sheet1.row(row_iterator).push(expired.earnedOn, expired.firstName, expired.lastName)
row_iterator = row_iterator + 1
end
file_io = StringIO.new
book.write file_io
file_io.string
And this method is being called like this:
format.xls {#expired = current_user.recent_activity(unlimited_size,params[:start],nil);
send_data(build_excel_expired_activity, :filename => "#{Time.now.strftime('%Y-%m-%d')}_Expired_Activity.xls", :type => "application/xls", :disposition => 'attachment')
}
Any thoughts, or ideas?
You might want to have a look at the axlsx gem and its rails counterpart acts_as_xlsx. It supports full schema validation so you don't have to fight with this kind of thing.
https://www.ruby-toolbox.com/projects/axlsx

Ruby add_item for eBay

I am attempting to write a ruby on rails app that posts an item to eBay. Cody Fauser/Garry Tan have a gem called ebayApi which is built on top of the ebay gem. When I attempt to post an item, I am getting an error back from ebay that says the condition ID is required for this category. I have found a category that does not require the condition, and I can post to that category. Searching through the eBay API documentation, I have found a tag conditionID under the "item" class. However, in the documentation for ebayAPI, there is no such tag. Looking back at the ebay API documentation, there is an older way to specify condition, using lookup_attributes. I have noted that the return xml is coming in API version 745, and Garry Gan's updated of the ruby interface is running version 609. I have tried using the lookup, and seem to get the same error (condition required). I am using the following code to specify the item:
#ebay = Ebay::Api.new :auth_token => #seller.ebay_token
item = Ebay::Types::Item.new( :primary_category => Ebay::Types::Category.new(:category_id => #ebayTemplate.categoryID),
:title => #ebayTemplate.name,
:description => #ebayTemplate.description,
:location => #ebayTemplate.location,
:start_price => Money.new((#ebayTemplate.startPrice*100).to_d, #ebayTemplate.currency),
:quantity => 1,
:listing_duration => #ebayTemplate.listingDuration,
:country => #ebayTemplate.country,
:currency => #ebayTemplate.currency,
:payment_methods => ['VisaMC', 'PayPal'],
:paypal_email_address => '********#gmail.com',
:dispatch_time_max => 3,
:lookup_attributes => [Ebay::Types::LookupAttribute.new( :name => "Condition", :value => "New")],
# :attribute_sets => [
# Ebay::Types::AttributeSet.new(
# :attribute_set_id => 2919,
# :attributes => [
# Ebay::Types::Attribute.new(
# :attribute_id => 10244,
# :values => [ Ebay::Types::Val.new(:value_id => 10425) ]
# )
# ]
# )
# ],
:shipping_details => Ebay::Types::ShippingDetails.new(
:shipping_service_options => [
# ShippingServiceOptions.new(
# :shipping_service_priority => 2, # Display priority in the listing
# :shipping_service => 'UPSNextDay',
# :shipping_service_cost => Money.new(1000, 'USD'),
# :shipping_surcharge => Money.new(299, 'USD')
# ),
Ebay::Types::ShippingServiceOptions.new(
:shipping_service_priority => 1, # Display priority in the listing
:shipping_service => #ebayTemplate.shipSvc,
:shipping_service_cost => Money.new((#ebayTemplate.shipSvcCost*100).to_d, #ebayTemplate.currency),
:shipping_surcharge => Money.new((#ebayTemplate.shipSurcharge*100).to_d, #ebayTemplate.currency)
)
],
:international_shipping_service_options => [
Ebay::Types::InternationalShippingServiceOptions.new(
:shipping_service => 'USPSPriorityMailInternational',
:shipping_service_cost => Money.new((#ebayTemplate.shipSvcCost*100).to_d, #ebayTemplate.currency),
:shipping_service_priority => 2,
:ship_to_location => #ebayTemplate.shipToLocation
)
]
),
:return_policy => Ebay::Types::ReturnPolicy.new (
:description => 'this product for suckers only!',
:returns_accepted_option => 'ReturnsAccepted'
)
#:condition_id => 1000
)
#response = #ebay.add_item(:item => item)
As you can see, it is just a mutation of the example given by Cody Fauser. The condition_id at the bottom will bring up an error as there is no such attribute. It seems to me there is no facility for this in the gem since the requirement came into existence after the gem was created. I have not been able to find any other gems to connect with ebay. I have also noticed, there are very little complaints about this even though people are still downloading the gem (10 people downloaded it today). I think there are quite a number of people writing for ebay. Is there a key word I am missing to specify the condition? A work around that people have been using? Another gem I have missed?
There is an existing item_conditions_codes.rb in the gem's type directory and only has two values New and Used. Guess you could add more values in there. However still needs mapping to ID's per the updating (and changed from Attributes) method
You have to modify in the gem library in .. ruby/1.8/gems/ebayapi-0.12.0/lib/ebay/types/item.rb
and add the following new lines
# added to allow ConditionID to be pushed into XML
numeric_node :condition_id, 'ConditionID', :optional => true
then in your ruby ebay code use the following convention
:condition_id => 1500,
At least that seems to work for me right now.

Resources