How to fill color in each cell

906 Views Asked by At

I'm using Roo and RubyXL to modify an Excel spreadsheet.

When I comment out the code that fills the color in the Excel sheet, then I do not get any error.

My full code:

require 'rubyXL'
require 'roo'

workbook = Roo::Spreadsheet.open(file_name)
workbook1 = RubyXL::Parser.parse(file_name)

workbook.default_sheet = 'Location Table Values'
worksheet1 = workbook1['Location Table Values']

for j in (workbook.first_row..workbook.last_row)
  print "\n"
  d = j

  for k in (workbook.first_column..workbook.last_column)
    if (k == workbook.first_column)
      choose("lookupSearch", :option => "Plan")
      fill_in "searchInput", :with => workbook.cell(j, k)
      find(:xpath, '//*[@id="searchicon"]').click
    elsif (k == workbook.last_column)
      puts " "
    else
      print k
      findXpath = page.find('td', text: workbook.cell(j,k), :match => :prefer_exact).path
      splitXpath = (findXpath.split("/"))
      count = splitXpath.count
      value = splitXpath.at(count - 1)

      if (value == "td[1]")
        finalElementXpath = findXpath.sub("td[1]", "td[2]")
      elsif (value == "td[2]")
        finalElementXpath= findXpath.sub("td[2]", "td[3]")
      end

      if (workbook.cell(j, k + 1)) == (find(:xpath, finalElementXpath).native.text)
        print "#{workbook.cell(j + 1, k)}-value exist \n"
        worksheet1.sheet_data[d][k].change_fill ('008000') # Green color
      else
        print "object #{workbook.cell(j, k + 1)}not exist #{workbook.cell(j, k)}"
        print workbook.cell(j, k + 1)
        worksheet1.sheet_data[d][k].change_fill ('ff0000')
      end
    end
  end
end

workbook1.write(file_name)

But I get Nil[] Nil class error.

0

There are 0 best solutions below