Rubyに、条件に基づいた動的な背景色付け(HEXコードを使って色を設定する必要があります)を書くことができる宝石はありますか?Rubyでダイナミックな背景色を持つExcelシートを書く
私は動的条件に基づいて異なるセルに異なる色を記述する必要が(例:レコードの提出状況)
すべてのヘルプは大歓迎です。
Rubyに、条件に基づいた動的な背景色付け(HEXコードを使って色を設定する必要があります)を書くことができる宝石はありますか?Rubyでダイナミックな背景色を持つExcelシートを書く
私は動的条件に基づいて異なるセルに異なる色を記述する必要が(例:レコードの提出状況)
すべてのヘルプは大歓迎です。
をあなたがのConditional Formating機能を使用して動的にセルの背景を設定することができますwrite_xlsx gem。ここで
はgemfileからの例です:
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'write_xlsx'
workbook = WriteXLSX.new('conditional_format.xlsx')
worksheet1 = workbook.add_worksheet
# Light red fill with dark red text.
format1 = workbook.add_format(
:bg_color => '#FFC7CE',
:color => '#9C0006'
)
# Green fill with dark green text.
format2 = workbook.add_format(
:bg_color => '#C6EFCE',
:color => '#006100'
)
# Some sample data to run the conditional formatting against.
data = [
[ 90, 80, 50, 10, 20, 90, 40, 90, 30, 40 ],
[ 20, 10, 90, 100, 30, 60, 70, 60, 50, 90 ],
[ 10, 50, 60, 50, 20, 50, 80, 30, 40, 60 ],
[ 10, 90, 20, 40, 10, 40, 50, 70, 90, 50 ],
[ 70, 100, 10, 90, 10, 10, 20, 100, 100, 40 ],
[ 20, 60, 10, 100, 30, 10, 20, 60, 100, 10 ],
[ 10, 60, 10, 80, 100, 80, 30, 30, 70, 40 ],
[ 30, 90, 60, 10, 10, 100, 40, 40, 30, 40 ],
[ 80, 90, 10, 20, 20, 50, 80, 20, 60, 90 ],
[ 60, 80, 30, 30, 10, 50, 80, 60, 50, 30 ]
]
# This example below highlights cells that have a value greater than or
# equal to 50 in red and cells below that value in green.
caption = 'Cells with values >= 50 are in light red. ' +
'Values < 50 are in light green'
# Write the data.
worksheet1.write('A1', caption)
worksheet1.write_col('B3', data)
# Write a conditional format over a range.
worksheet1.conditional_formatting('B3:K12',
{
:type => 'cell',
:format => format1,
:criteria => '>=',
:value => 50
}
)
# Write another conditional format over the same range.
worksheet1.conditional_formatting('B3:K12',
{
:type => 'cell',
:format => format2,
:criteria => '<',
:value => 50
}
)
workbook.close
出力ファイルは次のようになります。
私は結局 'write_xlsx'に切り替えました。これはあなたの次の質問への答えに続く条件付き書式設定をサポートしています...ありがとう!! – rubyprince
writeexcelがこれを実行できることは確かです(Cell formatting参照)。
writeexcel
がExcel 97-2007形式をサポートする重要な点の1つです。
は、Excelの作成中に定義された色ですか?または、Excelの値が変更されたときに色が変わるべきですか? – knut
@knut ...私は書き込みでこれを行う方法を考え出しましたが、私は値が 'High'、 'Medium'、 'Low'、そしてこれに対して異なる色を表示して、Excelでドロップダウンする必要があります。 – rubyprince