How I get the row number in a iteration using RubyXL? - ruby-on-rails

Using RubyXL I want to know what row number my iteration is.
workbook = RubyXL::Parser.parse("./file.xlsx")
worksheet = workbook[0]
worksheet.each do |row|
test0 = row[0].value
line = ????
puts "Line number #{line} - Value = #{test0}"
end

You can use #each_with_index and write it like this:
workbook = RubyXL::Parser.parse("./file.xlsx")
workbook.first.each_with_index do |row, index|
puts "Line number #{index} - Value = #{row[0].value}"
end

You can use each_with_index method while looping to get the current row number of the iteration
worksheet.each_with_index do |row, index|
test0 = row[0].value
line = index
puts "Line number #{line} - Value = #{test0}"
end

Related

Secure random unique number in loop

I have a loop that adds numbers in groups of a certain number which can be inputted by the user.
no_reps = #trial.number_of_repetitions
I'm looking to input a random number between one and the no_reps variable in groups of no_reps variable.
Current the r.treatment_index = SecureRandom.random_number(1..no_reps) isn't putting in unique numbers. The values match the range, but aren't unique per in_groups_of.
#trial.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
a.each do |r|
r.repetition_index = i + 1
r.treatment_index = SecureRandom.random_number(1..no_reps)
end
end
Try to #shuffle prepopulated array:
#trial.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
treatment_indexes = (1..no_reps).to_a.shuffle
a.each_with_index do |r, j|
r.repetition_index = i + 1
r.treatment_index = treatment_indexes[j]
end
end
UPD: If you take care about speed:
treatment_indexes = (1..no_reps).to_a
#trial.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
treatment_indexes.shuffle!
...

Write to csv is omitting first row

I am writing my Hashmap into a csv file with headers using the below piece of code. However, what I notice is that the first data row is not available in the file. I can the headers and all other rows accurately
def self.execute_auto_pg_debtors pg_debtors_dt_list
partition_key = Date.today.prev_day.prev_day.strftime "%Y%m%d"
csvfilename = "PG_Debtors_" + partition_key + ".CSV"
pg_debtors_dt_batch = Array.new
rowid = 0
pg_debtors_dt_list.each { |x|
pg_debtors_details = Hash.new
pg_debtors_details["Store_Order_Id"] = x['Store_Order_Id']
pg_debtors_details["Transaction_Id"] = x['Transaction_Id']
pg_debtors_details["Gateway_Payment_Id"] = x['Gateway_Payment_Id']
pg_debtors_details["PPS_Id"] = x['PPS_Id']
pg_debtors_details["Event_Type"] = x['Event_Type']
pg_debtors_details["Event_Date"] = x['Event_Date']
pg_debtors_details["Gateway_Name"] = x['Gateway_Name']
pg_debtors_details["Open_Amount"] = "%f" % x['Open_Amount']
pg_debtors_details["Invoice_No"] = x['Invoice_No']
pg_debtors_dt_batch << pg_debtors_details
rowid += 1
if rowid == 1
CSV.open(csvfilename, "w") do |csv|
csv << pg_debtors_details.keys# adding header row (column labels)
end
else
CSV.open(csvfilename, "a") do |csv|
csv << pg_debtors_details.values# of if/else inside hsh
end# of hsh's (rows)
end# of csv open
}
return pg_debtors_dt_batch
end
Please help.
You are writing the headers instead of the first row!
I recommend, that you open the file and iterate through your hash inside the CSV.open do ... end
AND do not use a else after your if rowid == 1. Just execute that for EVERY values, so you do not skip data row 1
Even if you check for rowid, the .each loop is still not aware of it. So, for rowid == 1, it will write the headers, but in the next iteration, x will point to the second item in pg_debtors_dt_list.
To solve it, write your code in the following order:
Open the file, and write the headers first.
Loop through pg_debtors_dt_list, and write subsequent data to the file.
Hope it helps.

AXLSX merge cells inside a style

I'm using ruby gem axlsx and want to know if there's a way to set merge columns inside a style? Today i doing like this:
sheet.merge_cells "A1:E1"
sheet.add_row [I18n.t('foo.some_label').upcase], style: [title]
sheet.merge_cells "B2:E2"
...
I want to avoid to increment the cells manually (B2:E2...B5:E5), there's a way to do this?
Yes give this a try (*Disclaimer I did not actually test these methods but I have used similar functionality in the past)
def merge_last_row(sheet,options ={})
last_row = sheet.rows.last.index + 1
first_col,last_col = options[:columns]
if first_col && last_col
sheet.merge_cells "#{first_col}#{last_row}:#{last_col}#{last_row}"
else
sheet.merge_cells sheet.rows.last
end
sheet.rows.last.style = style if options[:style]
end
so to do what you want it would be
merge_last_row sheet, columns:["A","E"]
sheet.add_row [I18n.t('foo.some_label').upcase]
merge_last_row sheet, columns:["B","E"], style:title
If the last row contains data in A-E then the columns can be left empty and it will merge the whole row. If it does not you could add an option for filling the columns like so
def fill_columns(sheet,column_count,options={})
row = options[:row_data] || []
(column_count - row.count).times do
row << nil
end
sheet.add_row row
end
calls as
my_row = ["Hello","World"]
fill_columns sheet, 5,row_data: my_row
# this will add a row like["Hello","World",nil,nil,nil]
# so that it will merge properly across the columns A-E
merge_last_row sheet
If you are going to use these consistently then patching these functions into Worksheet might make more sense so you don't have to pass the sheet object.
module Axlsx
class Worksheet
def merge_last_row(options={})
last_row = rows.last.index + 1
first_col,last_col = options[:columns]
if first_col && last_col
merge_cells "#{first_col}#{last_row}:#{last_col}#{last_row}"
else
merge_cells rows.last
end
rows.last.style = style if options[:style]
end
def fill_columns(column_count,options={})
row_data = options[:row_data] || []
(column_count - row.count).times do
row_data << nil
end
add_row row_data
end
end
end
Call
sheet.merge_last_row columns:["A","E"]
sheet.add_row [I18n.t('foo.some_label').upcase]
sheet.merge_last_row columns:["B","E"], style:title

How to get the cell position in spreadsheet in ruby on rails?

Is there any way to get the position(Co-ordinates) of spreadsheet cell in ruby on rails using gem like roo, spreadsheet etc. ?
Suppose I have value "TOTAL" in Cth column and 2nd row. How to get this position using ruby on rails?
Any hints?
#http://spreadsheet.rubyforge.org/GUIDE_txt.html
require 'spreadsheet'
book = Spreadsheet.open 'sample.xls'
sheet1 = book.worksheet 0
sheet1.each_with_index do |row, index|
for column in 0..(row.length-1)
puts "Row:- #{index}, Column: #{column}, Value: #{row[column]}"
end
end
Here is an example from roo official tutorials http://roo.rubyforge.org/
Hope it would help
HOURLY_RATE = 123.45
oo = Openoffice.new("simple_spreadsheet.ods")
oo.default_sheet = oo.sheets.first
4.upto(12) do |line|
date = oo.cell(line,'A')
start_time = oo.cell(line,'B')
end_time = oo.cell(line,'C')
pause = oo.cell(line,'D')
sum = (end_time - start_time) - pause
comment = oo.cell(line,'F')
amount = sum * HOURLY_RATE
if date
puts "#{date}\t#{sum}\t#{amount}\t#{comment}"
end
end

Using Column headers to parse excel sheets using roo - Ruby

Can we use column headers to specify the column number from which we are parsing the excel sheet using roo gem? My current code is like this now:
oo = Openoffice.new("simple_spreadsheet.ods")
oo.default_sheet = oo.sheets.first
(2..oo.last_row).each do |line|
date = oo.cell(line,'A')
start_time = oo.cell(line,'B')
end_time = oo.cell(line,'C')
pause = oo.cell(line,'D')
...
end
I would like to parse from column headers instead of specifying columns as 'A' 'B' 'C' ... Can I acheive this using Roo?
You can grab the entire header row as an array and hash the entire row key'd on the header row.
oo = Openoffice.new("simple_spreadsheet.ods")
oo.default_sheet = oo.sheets.first
header = oo.row(1)
2.upto(oo.last_row) do |line|
row_data = Hash[header.zip oo.row(line)]
...
end
You could also use row_data[line] to nest the hashes for later use.
A cleaner/clearer version of the above is
oo = Openoffice.new("simple_spreadsheet.ods")
oo.default_sheet = file.sheets.first
header = oo.first_row
2.upto(oo.last_row) do |line|
row_data = Hash[*header.zip(row).flatten]
...
end
the original took me a bit to understand because especially as i thought hash was a local variable named hash instead of the class Hash
This will use the header row as the keys. The helpful parts are transpose and strip.
def self.excel_to_hash(folder_name, file_name, tab_name)
# Takes an excel file name and a tab name, and returns an array of stripped, transposed rows
# Sample call: my_data = excel_to_hash File.join(Rails.root,'db/data/data_to_import.xlsx'), 'models'
rows = []
file = File.open(File.join(folder_name, file_name), mode = 'r')
excel = Excelx.new(file.path, nil, :ignore)
excel.default_sheet = excel.sheets.index(tab_name) + 1
header = excel.row(1)
(2..excel.last_row).each do |i|
next unless excel.row(i)[0]
row = Hash[[header, excel.row(i)].transpose]
row.each_key{|x| row[x] = row[x].to_s.strip if row[x]}
rows << row
end
return rows
end
valid through Roo gem 1.10.2
This works for me
require 'roo'
# open excel file
excel_file = Roo::Spreadsheet.open(file_path)
# iterate on each sheet
excel_file.each_with_pagename do |name, sheet|
# iterate on each sheet
sheet.parse(headers: true, pad_cells: true) do |row|
# data should be access by column header if we have column header Name we can access like this
row['Name']
end
end
end

Resources