Adding Macros to Excel multi seelct in rails - ruby

I wanted to use multi select list in the excel. hope the excel should contain vb macros for that. So I am trying to add the macros to the excel dynamically via ruby. But I didt get any idea. Is there any other option to generate the excel with single and multi select list option in ruby on rails.
I am using ubuntu and the libreoffice. I have manually tried to add the macro vb script in the excel in windows and it was working. But I cant do it with ruby. Please suggest if any other solutions are there

require 'writeexcel'
#data_array = ["option","aaa","bbb","ccc","ddd","eee","fff","ggg"]
workbook = WriteExcel.new('single_select_dropdown.xls')
worksheet = workbook.add_worksheet('sheet1')
worksheet.set_column('A:A', 30)
worksheet.set_column('B:B', 30)
worksheet.set_column('C:C', 20)
worksheet.set_column('A:D', 30)
worksheet.write(0, 0, 'Select Your Option')
worksheet.write(0, 1, 'option')
worksheet.data_validation(0, 1,
{
:validate => 'list',
:source => #data_array
})
worksheet.write(10, 2, "Your option is")
worksheet.write(10, 3, "=B1")
workbook.close
and this is for single select dynamic list via ruby, and still am trying multi select list

require "rubygems"
require 'write_xlsx'
#data_array = ["aaa","bbb","ccc","ddd","eee","fff","ggg"]
workbook = WriteXLSX.new('multiple_select_vba_project.xlsm')
workbook.set_vba_name('Workbook')
worksheet = workbook.add_worksheet
worksheet.set_vba_name('Sheet1')
worksheet.activate() #don't forgot to add this
worksheet.set_column('A:A', 30)
worksheet.set_column('C:C', 30)
workbook.add_vba_project('vbaProject.bin')
worksheet.write(1, 0, 'select your option')
worksheet.write(1, 2, 'option')
worksheet.data_validation(1, 2,
{
:validate => 'list',
:source => #data_array
})
workbook.close
also you need vbaProject.bin file..
need to extract vbaProject.bin file from existing xls file use this command
extract_vba filename.xlsm
if suppose got any error please update your libexcel-writer-xlsx-perl gem using this command
sudo apt-get -f install

Related

Add custom entries to acknowledgments.plist generated by Cocoapods

Cocoapods generates a plist which can be copied "post_install" to the settings bundle.
I'd love to add some custom entries in for items which need attribution but aren't Cocoapods.
I've looked through the source for https://github.com/CocoaPods/cocoapods-acknowledgements but I am not familiar with Ruby and I can't figure out where to start.
post_install do | installer |
require'fileutils'
FileUtils.cp_r('Pods/Target Support Files/Pods-xxx/Pods-xxx-acknowledgements.plist', 'xxx/Settings.bundle/Acknowledgements.plist', :remove_destination => true)
end
I don't need someone to write a whole solution for me, just point me in the right direction.
As my not complete answer got deleted, here now the full working version:
Based on this python script: https://gist.github.com/wagyu298/cb11f5d6eabd9acc7425cc106fc03fd2
We came up with a solution in our project:
def add_license_manually
require 'cfpropertylist'
additional_license = {
"FooterText" => "Copyright (c) 2020, Example Dev All rights reserved.
Redistribution......",
"License" => "MIT",
"Title" => "Library",
"Type" => "PSGroupSpecifier"
}
ack_plist_name = 'Pods/Target Support Files/Pods-xxx/Pods-xxx-acknowledgements.plist'
# read plist
plist = CFPropertyList::List.new(:file => ack_plist_name)
# convert to native data
data = CFPropertyList.native_types(plist.value)
# append the license
data["PreferenceSpecifiers"].insert(-1, additional_license)
# convert back
plist.value = CFPropertyList.guess(data)
# write plist to file
plist.save(ack_plist_name, CFPropertyList::List::FORMAT_XML)
end
And in your post_install, you add add_license_manually() before the
FileUtis.cp_r(...)
line

Add CSS minifier to Sprockets

I have a web application which uses rack.
The code:
set :assets, (Sprockets::Environment.new { |env|
env.js_compressor = Uglifier.new({
:output => {
:preserve_line => true,
:bracketize => true,
:beautify => true,
:indent_level => 4,
:semicolons => true,
},
:mangle => false
})
env.append_path(APP_ROOT + "/app/assets/images")
env.append_path(APP_ROOT + "/app/assets/javascripts")
env.append_path(APP_ROOT + "/app/assets/stylesheets")
})
I now want to add a CSS minifier to it.
Can someone explain why only javascript files are taken into the JS compressor above?
Can I add something like env.css_compressor = YUI::CssCompressor.new() after the JS_compressor to get my requirement done
UPDATE: Well the second actually worked. But I have no clue how it worked :)
You hadn't set up the Sprockets::Environment.css_compressor variable, so there was no compressor available to run on text/css assets.
puts Sprockets::Environment.methods.inspect
#=> [...#css_compressor, #css_compressor=, #js_compressor, #js_compressor=,...]
To answer your question about how assets are loaded, yes the default is to point to one load path and you can as well manipulate that to include others.
https://github.com/sstephenson/sprockets
The load path is an ordered list of directories that Sprockets uses to
search for assets. To add a directory to your environment's load path, use the append_path and prepend_path methods.

Create a file descriptor in ruby

I am writing a script will perform various tasks with DSV or positional files. These tasks varies and are like creating an DB table for the file, or creating a shell script for parsing it.
As I have idealized my script would receive a "descriptor" as input to perform its tasks. It then would parse this descriptor and perform its tasks accordingly.
I came up with some ideas on how to specify the descriptor file, but didn't really manage to get something robust - probably due my inexperience in ruby.
It seems though, the best way to parse the descriptor would be using ruby language itself and then somehow catch parsing exceptions to turn into something more relevant to the context.
Example:
The file I will be reading looks like (myfile.dsv):
jhon,12343535,27/04/1984
dave,53245265,30/03/1977
...
Descriptor file myfile.des contains:
FILE_TYPE = "DSV"
DSV_SEPARATOR = ","
FIELDS = [
name => [:pos => 0, :type => "string"],
phone => [:pos => 1, :type => "number"],
birthdate => [:pos => 2, :type => "date", :mask = "dd/mm/yyyy"]
]
And the usage should be:
ruby script.rb myfile.des --task GenerateTable
So the program script.rb should load and parse the descriptor myfile.des and perform whatever tasks accordingly.
Any ideas on how to perform this?
Use YAML
Instead of rolling your own, use YAML from the standard library.
Sample YAML File
Name your file something like descriptor.yml, and fill it with:
---
:file_type: DSV
:dsv_separator: ","
:fields:
:name:
:pos: 0
:type: string
:phone:
:pos: 1
:type: number
:birthdate:
:pos: 2
:type: date
:mask: dd/mm/yyyy
Loading YAML
You can read your configuration back in with:
require 'yaml'
settings = YAML.load_file 'descriptor.yml'
This will return a settings Hash like:
{:file_type=>"DSV",
:dsv_separator=>",",
:fields=>
{:name=>{:pos=>0, :type=>"string"},
:phone=>{:pos=>1, :type=>"number"},
:birthdate=>{:pos=>2, :type=>"date", :mask=>"dd/mm/yyyy"}}}
which you can then access as needed to configure your application.

Add image from URL to Excel with Axlsx

I'm using the (Axlsx gem and it's working great, but I need to add an image to a cell.
I know it can be done with an image file (see Adding image to Excel file generated by Axlsx.?), but I'm having a lot of trouble using our images stored in S3 (through Carrierwave).
Things I've tried:
# image.url = 'http://.../test.jpg'
ws.add_image(:image_src => image.url,:noSelect => true, :noMove => true) do |image|
# ArgumentError: File does not exist
or
ws.add_image(:image_src => image,:noSelect => true, :noMove => true) do |image|
# Invalid Data #<Object ...>
Not sure how to proceed
Try using read to pull the contents into a tempfile and use that location:
t = Tempfile.new('my_image')
t.binmode
t.write image.read
t.close
ws.add_image(:image_src => t.path, ...
To add an alternative answer for Paperclip & S3 as I couldn't find a reference for that besides this answer.
I'm using Rails 5.0.2 and Paperclip 4.3.1.
With image URLs like: http://s3.amazonaws.com/prod/accounts/logos/000/000/001/original/logo.jpg?87879987987987
#logo = #account.company_logo
if #logo.present?
#logo_image = Tempfile.new(['', ".#{#logo.url.split('.').last.split('?').first}"])
#logo_image.binmode # note that our tempfile must be in binary mode
#logo_image.write open(#logo.url).read
#logo_image.rewind
end
In the .xlsx file
sheet.add_image(image_src: #logo_image.path, noSelect: true, noMove: true, hyperlink: "#") do |image|...
Reference link: http://mensfeld.pl/tag/tempfile/ for more reading.
The .split('.').last.split('?').first is to get .jpg from logo.jpg? 87879987987987.

Using Ruby & Github API to filter commits by date

I am using the ruby gem 'octokit' which implements the Github API v3. Mostly works great but I cannot seem to filter by date. I believe I have the syntax and time format correct, but it appears my option is ignored and the API returns the past 35 entries regardless of the since or until dates.
Here's a minimal reproducible example (after installing the octokit gem).
require 'octokit'
require 'time'
#day = "2012-09-27"
#until = DateTime.parse(#date).iso8601
#since = (DateTime.parse(#day) - 60*60*48).iso8601
a = Octokit.commits({:username => "cboettig", :repo => "labnotebook", :since => #since, :until => #until})
see the date of the output of last entry
a.last.commit.author.date
explicit day doesn't work either
b = Octokit.commits({:username => "cboettig", :repo => "labnotebook", :since => "2012-09-27T00:00:00+00:00"})
b.last.commit.author.date
The date I get in both examples is from August, outside the specified range given. What did I miss?
Background: I'm trying to write a little Jekyll plugin that uses the API to return commits made to a specified repo on the day of the post.
joeyw gives a great answer to this question here.
The second argument should be the sha or branch, and options should be the third argument, e.g.
Octokit.commits("cboettig/labnotebook", "master", :since => "2012-09-28T00:00:00+00:00").length
or
Octokit.commits("cboettig/labnotebook", nil, :since => "2012-09-28T00:00:00+00:00").length
works just fine. Here's my corresponding jekyll plugin

Resources