如何在每个单元格中填充颜色

时间:2016-02-10 17:15:21

标签: ruby excel roo-gem rubyxl

我使用Roo和RubyXL修改Excel电子表格。

当我注释掉填写Excel表格中颜色的代码时,我没有收到任何错误。

我的完整代码:

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)

但我得到Nil [] Nil类错误。

0 个答案:

没有答案
相关问题