在下拉列表中为不同的值使用不同的颜色创建的Excel文件

问题描述:

我需要创建一个具有字段状态的Excel表格。值为高,中和低。当他们选择不同的值时,我需要显示不同的颜色。我已经使用 writeexcel 宝石实施了下拉列表。这是我的代码:

I need to create an excel sheet with a field status. It is a drop down with values 'High', 'Medium', and 'Low'. I need to show different colors when they select different values. I have implemented the drop down using writeexcel gem. Here is my code:

worksheet.data_validation(count, 5,
          {
            :validate        => 'list',
            :source          => ['High', 'Medium', 'Low'],
          })

下拉菜单正常工作。但是我想为每个选择指定一个颜色。我可以根据下拉菜单的选择对单元格进行着色,但是我需要的是不同颜色的不同的下拉菜单。任何其他具有此实现的宝石也很好。

The drop down is working fine. But I want to specify a color for each selection. I can color the cell based on the selection of the dropdown, but what I need is different colors for different selections of the drop down. Any other gem having this implementation is also fine.

write_xlsx gem是 writeexcel 的更新版本,支持较新的Excel 2007+ XLSX格式。它是同一个作者,具有相同的界面,但具有其他功能。

The write_xlsx gem is an updated version of writeexcel that supports the newer Excel 2007+ XLSX format. It is by the same author and has the same interface but has additional features.

其中一个新功能是条件形成

您可以使用它将条件格式应用于与下列下拉验证相同的单元格:

You can use it to apply conditional formats to the same cell as the drop-down validation like this:

worksheet.conditional_formatting(count, 5,
    {
        :type     => 'cell',
        :format   => format1,
        :criteria => '=',
        :value    => '"High"'
    }
)

worksheet.conditional_formatting(count, 5,
    {
        :type     => 'cell',
        :format   => format2,
        :criteria => '=',
        :value    => '"Medium"'
    }
)
...

您将需要使用标准界面定义格式。

You will need to define the formats using the standard interface.

注意,write_xlsx是Perl模块的一个端口 Excel :: Writer :: XLSX 。该模块包含有关使用的其他文档条件格式。您应该可以很容易地将示例转换为Ruby。

Note, write_xlsx is a port of the Perl module Excel::Writer::XLSX. That module contains additional documentation on the use of conditional formats. You should be able to convert the examples to Ruby easily enough.

另请参阅我对上一个问题的回答。

See also my answer to your previous question.