Can axlsx convert html format to work in excel? - ruby-on-rails

My DB stores text from a WYSIWYG editor that looks something like this:
<p><s>Hi!</s></p>
<p>My Name is Bob's.</p>
<p> </p>
<p>I like to eat these things:</p>
<ul>
<li>Candy</li>
<li>Veggies</li>
<li>Everything</li>
</ul>
<p>Enjoy<sup>2</sup></p>
In my view I have something like:
sheet.add_row [#event.text], style: font_format
where #event.text is the above html
Is there a way to make this formatting work in excel using axlsx?

I don't think there is any automatic conversion of html to styles. You'd have to write one yourself. I would use the rich text example as a guide.
I believe it handles any normal Axlsx style on a chunk of text. It at least handles bold, italic, and strikethrough.
For a forced line feed use "\x0A" (breaks between paragraphs.)
But, that means you'll have to parse the html.

Yes, to_spreadsheet is just for you. I have just finish a Rails app using it to generate xlsx file for download. I just follow the instruction and create a view 'show.xlsx.erb' in view directory. And it's done!

Related

ruby on rails: text filed formatting

I tried to copy and past from Word document to text field using Ruby on Rails.
But all formatting( spaces, bold and other) are disappeared on text filed.
I've just got the simple lines of text without any formatting.
I've read that need to use Simple format tool... but I want that a user be able just to copy and past a text to text field without doing any adjustments.
I mean, I want make all adjustment in advance and the user could just copy and past the text and got all formatting, the same as in Word doc.
The link to file with text field as below.
https://gist.github.com/tatyana12/2f9d39c2f6e4f8fabea5e70e11eaf310
Also I have Application.html.erb file:
https://gist.github.com/tatyana12/15c27d542091b04f3c3adfdfd252b7f4
How to initialize editor if I don't have id = "edit" right now?
How to put some code extra style to this file?
Have a go at wrapping your field in your show.html.erb or wherever you want to display it with simple_format, for example:
<%= simple_format(#object.description) %>
See http://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format for more info.
I've solved this problem by implementing SKEditor.
There is a lot of tutorial how to implement this editor.
I (as a lot of other users) have problem that my text wasn't formatted because this editor is not compatible with turbo links.
So, I disable turbo links in some files, and as result I have the text formatted.

HtmlPurifier - Codeblock

I was looking in HtmlPurifier documentation, but I can't see nothing about that.
Let's say I have
<div class="codebox">
All html tags here - Even <div class="codebox">another code box</div>
</div>
I want to parse the content of the first <div class="codebox"> so it can be readable as plaintext.
Can htmlpurifier do that ?
Out of the box HTMLPurifier can't do that and there is no config setting, that I know of, that can convert only the first <div> tag to plain text without converting the entire document. And even for converting the entire document to text the HTMLPurifier is neither needed nor recommended.
You can extend functionality of HTMLPurifier but unless you are an expert coder, I wouldn't recommend doing that.
However if you want to convert a part of the HTML document to text then break it into parts and run the part which you want to convert to text through
strip_tags()
PHP Manual page on strip_tags
You could convert all the div tags in your document to plain text with this configuration directive:
$config->set(HTML.ForbiddenElements, 'div'); //This will black list 'div' tag
And if you absolutely insist on converting your entire document to text using HTMLPurifier then here is the config directive that will do that.
$config->set('HTML.Allowed', ''); //This will white list NO tags ''

Redactor-Rails html tags showing

I'm trying to implement redactor as a WYSIWYG editor with ruby on rails. Everything seems to be working fine except that when I edit text in the editor the html tags show up. This happens even when I use the html button on the toolbar.
So on the webpage the text appears something like this:
<p>Edited text here</p>
I haven't included any code because I'm not really sure where to begin looking with this so any help at all will be appreciated :)
when using a text editor you have to tell your rails app that the area is html safe.
This is (by default) not the case as people could attack your site by using a text box you have put into your app.
by declaring an area as html safe you should be able to use the html tags as you like.
be aware of the security risk for using this.
e.g.
<div class="description">
<%= #foo.foo_desc.html_safe%>
</div>
Hope this clears it up for you.
in your view try using raw before the text you are trying to show. For example
<%= raw #post.body %>
this will work out with the html tags and show the processed text only without the tags.

Use of html in textarea not working correct

My cms is actievadmin and just installed tinymce for editing in the textarea. When i make changes (bold, paragraph tags ect) the page showing raw html. In the DB is stored with the html but is not rendering the html.
Does anyone know what this problem is?
Try replacing the output on your page to something like this:
<%=raw #model.content %>

CSV header styling in RoR

I'm using this to export records to CSV format. But i want to give styles to headers.
Here is my .erb
<%= CSV.generate_line(["X"+"\t"+"Y"+"\t"+"Y"+"\t"+"T"]).html_safe %>
<%#coupons.each do |coupon|%>
<%= CSV.generate_line([coupon.x+"\t"+coupon.y+"\t"+coupon.z+"\t"+coupon.t]).html_safe %>
<%end%>
How can i give styles to headers or columns? At least i want to make them bold:)
Thnaks.
CSV is a plain text format so you cannot add styles to it.
To use styles, formatting, charts etc you will need to generate an ecma-376 (eg excel) or odf file. If you are on RoR, you might have a look at this:
http://rubygems.org/gems/axlsx

Resources