0

I have an array of arrays like:

arr_all = [arr_1, arr_2, arr_3, arr_r]

where:

arr_1 = [2015-08-19 17:30:24 -0700, 2015-08-19 17:30:34 -0700, 2015-08-19 17:30:55 -0700]
arr_2 = ...
arr_3 = ...

I have a file to modify. I know how to add an array as a row, but I need help to insert each of the arrays in @@ar_data as columns. I find the Row to insert the data, and then I want to insert arr_1 in the cell (next_empty_row, B), then arr_2 at (next_empty_row, C), etc. Please advice. The Number of rows to fill the data is the size of each array. arr_1, arr_2, arr_3 are of size 3.

def performance_report
  Spreadsheet.client_encoding = 'UTF-8'
  f = "PerformanceTest_S.xls"
  if File.exist? (f)
    # Open the previously created Workbook
    book = Spreadsheet.open(f)
    sheet_1_row_index = book.worksheet(0).last_row_index + 1
    sheet_2_row_index = book.worksheet(1).last_row_index + 1
    # Indicate the row index to the user
    print "Inserting new row at index: #{sheet_2_row_index}\n"
    # Insert array as column - I need help with the below code to insert data in arr_data which is array of arrays. 
    column = 1
    row
    @@ar_data.each do |time|
      len = time.size
      book.worksheet(0).cell(sheet_1_row_index, )
      book.worksheet(0).Column.size
    end
    # This insert row is for worksheet 2 and works fine.
    book.worksheet(1).insert_row(sheet_2_row_index, @@ar_calc)
    # Delete the file so that it can be re-written
    File.delete(f)
    # puts @@ar_calc
    # Write out the Workbook again
    book.write(f)
sawa
  • 165,429
  • 45
  • 277
  • 381
napsterdsilva
  • 163
  • 1
  • 2
  • 14

2 Answers2

0

I don't even know what you're talking about, but what you should probably do is convert that xls to csvs (one for each sheet), and parse it something like this. I'll use a hash to make it platform-independent (but normally I just directly add spreadsheet data to a database using rails):

require 'csv' #not necessary in newer versions of ruby
rows = CSV.open("filename.csv").read
column_names = rows.shift
records = []
rows.each do |row|
  this_record = {}
  column_names.each_with_index do |col, i|
    this_record[col] = row[i]
  end
  records << this_record
end

If you don't want to manually convert each sheet into CSV, what you could do is use the Spreadsheet gem or something like it to convert each sheet into an array of arrays and that's basically a CSV file right there.

In ruby, Hashes inherit from the Enumerable class just like Arrays do. So to convert your hash into an array of tuples (two-element arrays with key and value for each), you'd just have to do this:

records = records.map(&:to_a)

But that's not even necessary, you can directly iterate on and simultaneously assign on hashes just like you can with an array of arrays

records.each_with_index do |hsh, i|
  hsh.each do |k,v|
    puts "record #{i}: #{k}='#{v}'"
  end
end
boulder_ruby
  • 38,457
  • 9
  • 79
  • 100
0

Am not really sure what you tried to accomplish. This is an example of how you can write your arrays to the end of your file.

require 'spreadsheet'

arrays = [
['Text 1','Text 2','Text 3'],
['Text 4','Text 5','Text 6'],
['Text 7','Text 8','Text 9']]

f = '/your/file/path.xls'
Spreadsheet.client_encoding = 'UTF-8'
if File.exist? f
    book = Spreadsheet.open(f)
    sheet = book.worksheet(0)

    lastRow = sheet.last_row_index + 1
    arrays.each_with_index do |row, rowNum|
          row.each_with_index do |cell, cellNum|
              sheet[ rowNum + lastRow, cellNum ] = cell
          end
    end
    File.delete f
    book.write f
end
Lasse Sviland
  • 1,479
  • 11
  • 23
  • Hi @lassvi, thanks for the solution. That looks like what i am trying to achieve. but still i am not completely there. Please help me understand what row, rowNum, cellNum and cell. I did a little variation of your solution but it doesn't work ... i hope it gives you a better idea of what i am trying to achieve here. `@@ar_data.each do |i| l = 1 i.each do |j| k = 0 book.worksheet(0)[sheet_1_row_index + k, l] = j.at(k) k +=1 end l +=1 end` – napsterdsilva Aug 21 '15 at 20:56
  • The problem with your code(the one in the comment) is that you are setting the l and k variables inside the loops, so they will never count up, l will always stay at 1 and k at 0...... each_with_index will run through your array and create a index for you(the same as you tried to do with the l and k variable), so in the code above the row variable will be a variable storing the secondary array, rowNum will be the index of the array. cell is the value of the array element('Text 1', 'Text 2' etc) and cellNum will be the index of that text in its array. – Lasse Sviland Aug 22 '15 at 01:30
  • You can change the code in your comment to `sheet_1_row_index = sheet.last_row_index + 1 @@ar_data.each_with_index do |i, l| i.each_with_index do |j, k| book.worksheet(0)[sheet_1_row_index + k, l] = j end ` end Am not sure what you wanted to j.at(k) to do, but i believe you ment to use i.at(k), however that will be the same as what is stored in your j variable.( the j variable will also be the same as i had in the cell variable from my original awnser. – Lasse Sviland Aug 22 '15 at 01:41
  • Your stubborness to insist on parsing tabular data in xml format shall be your downfall young diva!!! – boulder_ruby Aug 24 '15 at 02:52