Is there a way that I can add a new line to a cell using the Axlsx gem in Rails?
So basically replicating in Excel once you enter a value you can do a Alt + Enter to add additional text to the new line in the cell. I tried
sheet.add_row ["Testing cell row 1" + \r\n + "Testing cell row 2"]
but that throws an error.
I recently had the same problem and I found a solution that works.
I used this to setup:
p = Axlsx::Package.new
p.use_shared_strings = true
And this code adds a wrap style that makes the \r line breaks work correctly:
wrap = p.workbook.styles.add_style alignment: {wrap_text: true}
sheet.add_row "1\r2\r3", style: wrap
Now the new line in cell works, and the output is:
1
2
3
Notes:
The new line in cell doesn't work (#Gary Pinkham)
The "\x0D\x0A" didn't work (#noel)
For a forced line feed use "\x0A" (breaks between paragraphs.)
If you want both carriage return and line feed, use "\x0D\x0A".
I couldn't comment on the "doesn't work in mac excel" comment so adding this as an answer.. use package.use_shared_strings = true.. needed for Mac Excel..
Related
version
Rails 6.0.3
ruby 2.7.2
What I want to do
There is text_area_tag in haml file
text_area_tag("context[]",nil,:wrap=>"virtual",:rows=>"8")
Now I wanna add like this sentence as placeholder
placeholder test
Hi I'm Mike, placeholder
new line
new line
new line
Hi I'm Michelle
new line
new line
Hi I'm Goku
new line new line
(and more... and more...)
・
・
・
Problem
I could declare variable in view haml file like
- var = "placeholder test
Hi I'm Mike, placeholder
new line
new line .........."
but it's too long, super spaghetti code.
If I could use like javascript↓, It would be useful
const var = `
placeholder test
Hi I'm Mike, placeholder
new line
......
`
If somebody knows better way to declare variable with a lot of new line in haml file, let me know please.
To create new line, you can use "\n" direct on haml file, or use a variable like times4 in my way, and note not set the name to 4times, it will not working!
%h4 Content
- var = "placeholder test
Hi I'm Mike, placeholder
new line
new line .........."
- iso1 = "placeholder test \nHi I'm Mike, placeholder \nnew line \n \nnew line .........."
- times4 = "\n" * 4
- iso2 = "placeholder test \nHi I'm Mike, placeholder \nnew line #{times4}new line .........."
= text_area_tag("context[]",nil,:wrap=>"virtual",:rows=>"8", :placeholder=>"#{iso2}")
Happy coding!
Update for multi lines, you can use "|" in variable declaration, example:
- iso3 = "placeholder test \n" + |
"Hi I'm Mike, placeholder \n" + |
"new line #{times4}" + |
"new line .........." |
Note it ends with a "|", if not, you will get a error!
Do the more, door the move! : D
I have a table made up of many cells, and in one cell i want to draw a horizontal line, and if i try horizontal_line it shows table document unsupported type.
and i'm using prawn ruby gem
Ex
cell_1 = make_cell(:content => "this row content comes directly ", height: 62.5.mm, size: 6)
cell_2 = make_cell(:content => "this row content comes directly ", height: 62.5.mm, size: 6)
cell_3 = make_cell(:content => "this row content comes directly ", height: 62.5.mm, size: 6)
t = make_table([[cell_1],[cell2], [cell3])
t.draw
i tried like using
cell4 = horizontal_rule(some values)
make_table shows error unrecognized table content, when i add cell4 to it.
How do i draw line inside the table or make cell
I was trying to do the same thing months a go but finally I decided to forget using prawn cause it make every thing hard! If you have enough time to use another pdf generator I recommend you to use Wicked_PDF gem (here) and draw your table with simple html and css. Wicked pdf is faster easier and makes you be more creative.
You need to override the Prawn::Table::Cell class. Here's how I did it:
class RedTriangleCell < Prawn::Table::Cell
def natural_content_width
0
end
def natural_content_height
0
end
def draw_content
color = #pdf.fill_color
#pdf.fill_color("e93131")
#pdf.fill_polygon([-5,2], [-1,8], [3,2])
#pdf.fill_color(color)
end
end
You can drop an instance of this as the cell content and it will render whatever you have in draw_content.
I'm saving Rails data into an Excel spreadsheet using the gem AXLSX_RAILS.
I have some text fields that are stored as HTML in the database.
This is my attempt to convert the HTML to text:
sheet.add_row ['REVENUE DESCRIPTION', strip_tags(#costproject.revenue).gsub!(" ", "")]
That works to remove the HTML tags.
But, I would like to replace with the Excel new line (code 10 - vbLf).
How can I do that?
I tried this:
sheet.add_row ['DESCRIPTION', strip_tags(#costproject.description).gsub!(" ", vbLf)]
Thanks for the help!
Try "\x0A". Something like:
sheet.add_row ['REVENUE DESCRIPTION', strip_tags(#costproject.revenue).gsub!(" ", "\x0A")]
That should be the Hex equivalent of vbLF. See this reference.
If you want both carriage return and line feed, use "\x0D\x0A".
Incidentally, I do not see any constant within Axlsx.
I need to send a string to my view that includes line breaks. If my controller says:
def answer = "Line 1<br/>Line 2"
render answer
It displays two lines of text in the view. But I need it to work with:
def answer = "Line 1<br/>Line 2"
[answer: answer]
where I have an ${answer} tag in my gsp. When I do the above, I get a single line exactly as it appears in the quotes. How do I get my tag to interpret the HTML?
Return Line 1 and Line 2 as array
For Example
IN Controller
String[] answer = null
answer.add(Line1)
answer.add(Line2)
[answer: answer]
In GSP
${answer.get(1)}<br />${answer.get(2)}
Hope this will help
I have an expression statement to display content for a text box for a reportviewer but went blank when trying to add a carriage return or a new line between the two expression. My expression as follows:
=Format(Fields!LastDateVisited.Value, "d")+ " "+Fields!LastVisitType.Value
Instead of the space i want to make it a new line. I tried "\n" but didn't work, any suggestions would be great. Thanks!
You can also use following :
= Fields!FirstField1.Value + System.Environment.NewLine + Fields!MyField2
I think that a end of line character should work: '\n'
in your placeholder properties (Expr page) change the markup type to html