0

我有以下循环

我有所有数据@clients

[1,2,3,4,5].each { |rows|
    if rows % 2 == 0
        sheet1.row(rows).default_format = bg_color1
    else
        sheet1.row(rows).default_format = bg_color2
    end
}

但我有超过 100 行。@clients如上所述,我如何在每个循环中计算和使用它

4

2 回答 2

1

这应该工作

@clients.each_with_index { |rows,i|
    if i % 2 == 0
        sheet1.row(i).default_format = bg_color1
    else
        sheet1.row(i).default_format = bg_color2
    end
}
于 2013-09-04T12:36:40.083 回答
0
@clients.each_with_index do |client, index|
  if index % 2 == 0
      sheet1.row(index+1).default_format = bg_color1
  else
      sheet1.row(index+1).default_format = bg_color2
  end
end

each_with_index遍历一个数组并提供一个变量来跟踪该数组中的当前位置。

于 2013-09-02T10:37:16.427 回答