I am using Axlsx for generating Excel file.
I need to add image to the Excel File. I have used this code :
ws.add_image(:image_src => '../something',:noSelect => true, :noMove => true) do |image|
image.width=1000
image.height=200
image.start_at 0,0
end
where 'ws' is the worksheet.
It adds the required image, but i am not able to set the 'width' & 'height' of the image with this code.
Even if i give width=2000 and height=1000, it does not affect the image in Excel file.
Can anybody tell , what i doing wrong.?
This looks correct to me as well, and is inline with the example in the gem.
wb.add_worksheet(:name => "Image with Hyperlink") do |sheet|
img = File.expand_path('../image1.jpeg', __FILE__)
# specifying the :hyperlink option will add a hyper link to your image.
# #note - Numbers does not support this part of the specification.
sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image|
image.width = 7
image.height = 6
image.hyperlink.tooltip = "Labeled Link"
image.start_at 2, 2
end
end
There is a possibility that a bug was introduced in the version you are using.
As we discussed on #axlsx, lets try this against master on github and if it does prove to be a bug in the version you are using, I'll push out a new release.
Best,
randym
Related
I'm using the axslx ruby gem to create a spreadsheet with a pie chart. For the most part it is very straightforward and I've had no problems with one exception: I can't figure out how to set the font size for the title in the pie chart. I've read through the documentation and examples and found no mention of it. I'm sure I have overlooked it but if anyone knows how to do that, I would appreciate any help.
thanks!
adding code ... sorry, should have provided this initially
issue_sheet.add_chart(Axlsx::Pie3DChart, start_at: 'I3', end_at: 'S32') do |chart|
chart.add_series data: issue_sheet['G3:G7'], labels: issue_sheet['F3:F7'], colors: ['9467BD','D62828','EF7E12','82BBDD','3AA02B']
chart.title = "Distribution by Severity"
chart.d_lbls.show_val = false
chart.d_lbls.show_percent = true
chart.d_lbls.d_lbl_pos = :outEnd
chart.d_lbls.show_leader_lines = true
end
I did notice something interesting but I'm not sure what it means. If I use a cell reference rather than a string for the chart.title, the font size changes.
chart.title = "Distribution by Severity" # this sets the font size to 16
chart.title = issue_chart['F2'] # this sets the font size to 10
You can make use of sz property provided in this link
item_style = s.add_style :b => false, :sz => 9, :font_name => 'courier'
row = sheet.add_row [item.name, item.price], :style => item_style
Trying to add a Paned window in Ruby/Tk and I'm getting the following error:
C:/Users/user/Ruby193/lib/ruby/1.9.1/tk.rb:3016:in `_invoke': Attempt to change read-only option (RuntimeError)
whenever I add the orient option to my code like this:
p = Tk::Tile::Paned.new(parent) { orient 'horizontal' }
It seems that 'orient' is read-only (and defaults to 'vertical') for some reason? I noticed a ruby/tk tutorial on the web with a Paned window example and it avoided using the orient option, perhaps because they ran into the same error?
If you paste the following tutorial code into a .rb file and run it (no orient option) it works. Add the orient option similar to the above and it fails.
require 'tk'
require 'tkextlib/tile'
$resultsVar = TkVariable.new
root = TkRoot.new
root.title = "Window"
p = Tk::Tile::Paned.new(root)do
height 110
place('height' => 100, 'width' => 200, 'x' => 10, 'y' => 10)
#orient 'horizontal' # <== uncomment this line to see error
end
f1 = TkFrame.new(p) {
relief 'groove'
borderwidth 3
background "red"
padx 30
pady 30
pack('side' => 'left', 'pady' => 100)
}
f2 = TkFrame.new (p){
relief 'groove'
borderwidth 3
background "yellow"
padx 30
pady 30
pack('side' => 'right', 'pady' => 100)
}
p.add f1 #, nil <== had to remove nil option here because this also caused an error
p.add f2 #, nil
Tk.mainloop
Has anyone else been able to get the 'orient' option to work? I need it to be horizontal, not the default vertical value. I tried looking at tk.rb and following the error trace and it seems to indicate a 'method_missing' issue.
I think the problem is that the property 'orient' can be set but can't be changed. You can create a PanedWindow 'horizontal' if you pass the option at creation time. Like
p = Tk::Tile::Paned.new(root, 'orient' => 'horizontal' )
Forget the 'method_missing' call. Is a trick for dinamically creating the properties of the widgets.
I'm using the Axlsx gem to create an excel file and I'd like to add an image. I am currently able to add an image but it seems to add the image in a way that makes it float on top of other content.
I'd like it to exist within a cell. Is this possible?
It is an old question but we never know.
There is an answer in this post : Adding image to Excel file generated by Axlsx.?
You can specify the cell from which you want to put the image by using image.start_at
sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image|
image.width = 7
image.height = 6
image.hyperlink.tooltip = "Labeled Link"
image.start_at 2, 2
end
HTH
Is it possible to insert images into an Excel spreadsheet automatically given a link to the image?
Here's how you do it it in Ruby:
The Worksheet object's Shapes collection includes an AddPicture() method that creates a picture from an existing file and returns a Shape object that represents the new picture. The syntax is:
.AddPicture(Filename, LinkToFile, SaveWithDocument, Left, Top, Width, Height)
All seven arguments are required, but this allows you to specify the position and size of the picture in the method call.
The following code inserts an image into the range of cells from C3 to F5 in the active worksheet:
require 'win32ole'
xl = WIN32OLE.connect('Excel.Application')
ws = xl.ActiveSheet
range = ws.Range('C3:F5')
pic = ws.Shapes.AddPicture( {
'FileName' => 'C:\Pictures\Image1.jpg',
'LinkToFile' => false,
'SaveWithDocument' => true,
'Left' => range.Left,
'Top' => range.Top,
'Width' => range.Width,
'Height' => range.Height
} )
I make a dozen of plots with Gnuplot on Mac via ruby-gnuplot. If I re-run my ruby script, then the number of open windows with the plots doubles. If I could just output all these plots in a PDF opened in Preview, then the file would be automatically updated after every re-run and I don't need to bother closing the numerous windows.
Currently I can achieve this only with one plot per PDF-file:
Gnuplot.open do |gp|
Gnuplot::Plot.new(gp) do |plot|
plot.arbitrary_lines << "set terminal pdf \n set output 'figures.pdf'"
# ...
end
end
How can I make a single PDF with all my figures by Gnuplot?
Hmm, at least on gnuplot for UN*x, multipage output for postscript and PDF always was the default - as long as you don't either change the terminal type nor reassign the output file, everything you plot ends up on a new page.
I.e. you do:
set terminal pdf
set output "multipageplot.pdf"
plot x, x*x
plot sin(x), cos(x)
set output ""
and you end up with two pages in the PDF file, one containing line/parabola, the other sine/cosine.
To clarify: The important thing is to issue all the plot commands in sequence, without changing the output file nor changing the terminal type. Gnuplot won't append to an existing PDF file.
I make thousands of plots with ruby-gnuplot and use a gem called prawn to compile them into a pdf. The following is a code snippet using prawn, that includes some useful features:
require 'prawn'
def create_pdf
toy_catalogue = #toy_catalogue
full_output_filename ||= "#{output_path}/#{pre-specified_filename_string}"
Prawn::Document.generate(full_output_filename, :page_layout => :portrait, :margin => 5, :skip_page_creation => false, :page_size => [595, 1000]) do
toy_catalogue.each do |toy|
start_new_page
image toy[:plan_view], :at => [0,900], :width => 580
image toy[:front_view], :at => [0, 500], :width => 585
font_size(20) { draw_text toy[:name], :at => [5, 920] }
draw_text "production_date = #{toy[:date]}", :at => [420, 930]
end
end
end
That should be easy enough to adapt to your purposes.