How do I merge cells using the Ruby Spreadsheet gem. I would like to merge the first 6 cells on the first row of a worksheet. When I try the following it does not work:
merge_format = Spreadsheet::Format.new :align => :merge
6.times do |j|
sheet.row(0).set_format(j,merge_format)
end
What am I doing wrong?
You can simply do
sheet.merge_cells(start_row, start_col, end_row, end_col)
If you want to go with set_format, I'd advise trying :vertical_align => :merge, although I didn't use it since merge_cells always worked for me.
Related
I have this table like in the image showed below:
I am using Ruby Watir to get every row of the first column.
https://dominiumestate.com/wp-content/uploads/2019/08/Capture.jpg
I have tried this but I can't get every row of the table only the first one with
puts t.tr(:index => 2, class: ['spy1xx']).td(:index => 0).font(class: ['spy14']).text
t = browser.table(:index => 1).tbody()
puts t.trs(:index => 2, class: ['spy1xx']).each do |s|
puts s.td(:index => 0).font(class: ['spy14']).text
end
You will want to:
Get a collection of the table rows
Iterate through each row to get the specific cell text.
Try:
browser.trs(class: /spy1x/, onmouseover: true).map { |tr| tr.font(class: 'spy14').text }
Here is my code:
<%= DimensionVersion.where(:dimension_id => 1).select("name") %>
I expect to get a list of dimension version names where :dimension_id => 1. There are four in the database.
Instead I get this:
#<ActiveRecord::Relation:0x3d351c8>
EDIT:
I figured out how to return what I wanted (sort of) with this:
<%= DimensionVersion.select("name").where(:dimension_id => 1).all %>
Which returns:
[#<DimensionVersion name: "Default">, #<DimensionVersion name: "Test1">, #<DimensionVersion name: "Test2">, #<DimensionVersion name: "Test3">]
However, I don't want it returned with #<DimensionVersion Name: ... >. I tried removing = from the leading tag, but then nothing returned.
DimensionVersion.where(:dimension_id => 1).select("name")
I think you need the pluck method.
Rewrite the above as:
DimensionVersion.where(:dimension_id => 1).pluck(:name)
Similarly even a higher level construct like collect can be used as:
DimensionVersion.where(:dimension_id => 1).collect(&:name)
Hope this helps.
AR returns Relation so that you can chain conditions etc. If you want the actual results, call #all, #first, #each,... on it:
DimensionVersion.where(:dimension_id => 1).select("name").all
Querying with rails is such a pain in the butt I'm about to abandon the whole framework and go back to php.
You might want to read the guides: Active Record Query Interface.
I was able to get rid of the column names by using the collect method like so:
DimensionVersion.select("name").where(:dimension_id => 1).all.collect { |d| [d.name]}
I want to perform an if condition where, if linkedpub.LPU_ID is found in an array of values(#associated_linked_pub), do some action.
I tried the following but the syntax is not correct.
Any suggestion is most welcomed..Thanks a lot
<% for linkedpub in Linkedpub.find(:all) %>
<% if linkedpub.LPU_ID IN #associated_linked_pub %>
# do action
<%end%>
<%end%>
You can use Array#include?
So...
if #associated_linked_pub.include? linkedpub.LPU_ID
...
Edit:
If #associated_linked_pub is a list of ActiveRecord objects then try this instead:
if #associated_linked_pub.map{|a| a.id}.include? linkedpub.LPU_ID
...
Edit:
Looking at your question in more detail, it looks like what you are doing is VERY inefficient and unscalable. Instead you could do...
For Rails 3.0:
Linkedpub.where(:id => #associated_linked_pub)
For Rails 2.x:
LinkedPub.find(:all, :conditions => { :id => #associated_linked_pub })
Rails will automatically create a SQL IN query such as:
SELECT * FROM linkedpubs WHERE id IN (34, 6, 2, 67, 8)
linkedpub.LPU_ID.in?(#associated_linked_pub.collect(&:id))
Using in? in these cases has always felt more natural to me.
if #associated_linked_pub is an array, try
if #associated_linked_pub.include?(linkedpub.LPU_ID)
#associated_linked_pub.collect(&:id).include?(linkedpub.LPU_ID)
Can you recommend good data grid class/gem for Ruby on Rails? Like http://code.google.com/p/zend-framework-datagrid/ for ZF
You can also try datagrid gem. That is focused not only grids with columns but also filters.
class SimpleReport
include Datagrid
scope do
User.includes(:group)
end
filter(:category, :enum, :select => ["first", "second"])
filter(:disabled, :eboolean)
filter(:confirmed, :boolean)
filter(:group_id, :integer, :multiple => true)
integer_range_filter(:logins_count, :integer)
filter(:group_name, :string, :header => "Group") do |value|
self.joins(:group).where(:groups => {:name => value})
end
column(:name)
column(:group, :order => "groups.name") do |user|
user.name
end
column(:active, :header => "Activated") do |user|
!user.disabled
end
end
Not sure if this is what you are looking for but checkout https://github.com/wice/wice_grid
If you are looking for a powerful client-side grid, supporting pagination, sorting, grouping, editing, export to Excel, PDF, etc, you can check Shield UI's Grid component.
Here's a tutorial on how to integrate it in Rails.
If you are looking for things like pagination, ordering, sorting etc, then rails does all this automatically.
So, for example if you wanted to sort all row by a particular column, then the title of that column could simply be a link that sorted the results by that column and then re rendered the grid.
So if you want to build a data grid that isn't AJAXy then this is pretty simple. If you are looking for a way to do it with XHR requests then you can use jQuery to make requests in the background.
As fas as a gem that does all this automatically, I couldn't find one, but I can't see why you couldn't do it yourself easily with the foundations that rails provides.
I"m having no luck formatting a table in a PDF served from a Rails action using Prawn.
Here's the code, but it doesn't apply any of the formatting:
p = Prawn::Document.new(:page_size => "A4")
data = []
data << ["alpha", "brava", "charlie"]
data << ["delta", "echo", "foxtrot"]
p.table(data) do
row(0).style(:background_color => 'dddddd', :size => 9, :align => :center, :font_style => :bold)
cells[0,0].background_color = '999999'
end
p.render
Any clues?
Which version of prawn are you using? I had all sorts of issues with things not working as expected, then I upgraded to prawn 0.11.1.pre and it sorted it all.
See Prawn Tables: Block is not executing which is pretty much the same question.