Writing an excel sheet with dynamic background colors in Ruby - ruby-on-rails

Is there a gem in Ruby which can write to an Excel file, with dynamic background coloring based on condition (need to set the color using HEX code)?
I need to write different colors to different cells dynamically based on condition (ex: status filed of a record)
Any help is greatly appreciated.

You may take a look to rexcel
After installation you may check examples/example_color.rb
It creates:

You can set the cell backgrounds dynamically using the Conditional Formating feature of the write_xlsx gem.
Here is an example from the 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
The output file will look like this:

I know for sure that writeexcel can do this (see Cell formatting).
One important thing that writeexcel supports Excel 97-2007 formats.

Related

Does jsPdf have a concept of a div that I'm just not seeing?

I need to create a pdf with a number of images with titles and descriptions.
I would be mapping an array of objects and then inserting them into the document as two columns.
With my understanding of how to do that right now, I would have to figure out a way to position each element dynamically.
const doc = new jsPDF();
doc.text("Some title", 10, 10);
doc.addImage(imageData, "JPEG, 10, 15, 100, 50)
doc.text("Some Desc", 10, 50);
doc.text("Some title", 300, 10);
doc.addImage(imageData, "JPEG, 300, 15, 100, 50)
doc.text("Some Desc", 300, 50);
doc.text("Some title", 10, 150);
doc.addImage(imageData, "JPEG, 10, 135, 100, 50)
doc.text("Some Desc", 10, 200);
// All of this x and y coordinates would have be calculated and not static as they are above
...and so on
//Something like that
If there was a wrapper element that I could relatively position the child elements, and then position the wrapper element in the document that would be a lot better.
However, I don't see a way this can be done. Am I wrong?

Can't use .len of a bidimensional array

I have this simple code that doesn't compile.
const s = [_][_]int {
[_]int{08, 02, 22, 97, 38, 15, 00},
[_]int{49, 49, 99, 40, 17, 81, 18},
[_]int{81, 49, 31, 73, 55, 79, 14},
[_]int{52, 70, 95, 23, 04, 60, 11},
[_]int{22, 31, 16, 71, 51, 67, 63},
[_]int{24, 47, 32, 60, 99, 03, 45},
[_]int{32, 98, 81, 28, 64, 23, 67},
[_]int{67, 26, 20, 68, 02, 62, 12},
[_]int{24, 55, 58, 05, 66, 73, 99},
[_]int{21, 36, 23, 09, 75, 00, 76}
};
pub fn main() void
{
const w = s[0].len;
const h = s.len;
}
The compiler says:
./a.zig:1:14: error: inferred array size invalid here
const s = [_][_]int {
^
./a.zig:16:15: note: referenced here
const w = s[0].len;
What is the problem?
I'd be interested to know there's a deeper reason, but my simple understanding is that the current syntax [N]T allows for the array size to be elided using _, but not for more than one dimension.
So you can fix your problem using the following (N.B. I've used u8 because I'm unsure what your int is):
const s = [_][7]u8{
// Your items
}
I suspect this is because of the way the parsing rules are applied, so [7]u8 would be the type your nested array would hold, and will be used by the compiler to check contents are all of type [7]u8; you can confirm this by modifying one of your rows to have 6 elements and examining the resulting error.
If you want a variable number of items, you could start to look into an array of slices: [_][]u8, but I don't think that's what you're currently after.

How to read byte array data in Dart?

connecting TCP Socket server and sending Request. and also Server sends the response in Byte array. How to read byte array data in dart.
Socket.connect('localhost', 8081)
.then((socket) {
//Establish the onData, and onDone callbacks
socket.listen((data) {
print(new String.fromCharCodes(data).trim()); //Here data is byte[]
//How to read byte array data
},
onDone: () {
print("Done");
// socket.destroy();
},
onError: (e) {
print('Server error: $e');
});
socket.add([255, 12, 0, 11, 0, 9, 34, 82, 69, 70, 84, 65, 72, 73, 76]);
});
}
It depends on with data type was encoded to bytes. Let's suppose it's String
Then you can do it with dart:convert library.
import 'dart:convert' show utf8;
final decoded = utf8.decode(data);
It's pretty clear that there's a message structure in those bytes. You give two examples of messages:
[255, 12, 0, 11, 0, 9, 34, 82, 69, 70, 84, 65, 72, 73, 76]
and
[255, 20, 0, 11, 0, 0, 0, 15, 80, 82, 69, 77, 84, 65, 72, 73, 76, 45, 53, 53, 57, 55, 48]
Both start with 255, followed by what looks like two or three little endian 16 bit words (12 and 11) and (20, 11 and 0) followed by a string, who's length is encoded in a leading byte. If you are expected to inter-operate with another system, you really need the protocol spec.
Assuming I've guessed the structure correctly, this code
main() {
Uint8List input = Uint8List.fromList([
255,
20,
0,
11,
0,
0,
0,
15,
80,
82,
69,
77,
84,
65,
72,
73,
76,
45,
53,
53,
57,
55,
48
]);
ByteData bd = input.buffer.asByteData();
print(bd.getUint16(1, Endian.little)); // print the first short
print(bd.getUint16(3, Endian.little)); // and the second
print(bd.getUint16(5, Endian.little)); // and the third
int stringLength = input[7]; // get the length of the string
print(utf8.decode(input.sublist(8, 8 + stringLength))); // decode the string
}
produces
20
11
0
PREMTAHIL-55970
as expected

AS2 Final Fantasy type Inventory

Edit: since I wasn't clear originally, I'm having trouble working out an AS2 inventory with stacking consumables and equippable weapons/armor, my code below is the arrays holding the values I've been working on for years but haven't figured any viable way to make work. I'm looking for help/ideas of how I can make an array hold these values and make them stack
Since about 2008, I've been working on a flash RPG. While I'm making headway, my biggest roadblock is the lack of item system which is becoming increasingly hard to create in AS2 as all of the great coders are slowly refusing to touch flash anymore. It's far too late to start over in a different language, my main file is a well over 500mb .fla
Honestly, I'd pay whoever can figure this out cause this game has a few other issues I'd like help with after this is solved. I'll throw down $50 usd to whoever can make a good item system with these reqs
Has to be able to draw from a database of different item types (consumable, weapon, armor, accessory)
Consumables must be able to "stack" (weapons, armor, accessories dont need to stack)
Consumables must be able to directly affect hp/mp values (heal and the like)
Weapons Armour acc. must have values attached to them so they can affect the character's stats when equipped (ex.strength +10 or something)
Items must be able to be bought and sold
Items must be able to be displayed in text (movieclips of each item is not really necessary)
AS2
Pretty much this below : (Final Fantasy item/inventory example)
File size is not an issue whatsoever, these items must be able to be rendered in game
Literally if possible, use Final Fantasy 7 (or any FF game) as a basis for how the item system should work. If you can get it close to that, I'll pay you immediately. This is the code I was working on...
//ITEM SYSTEM
//this array will hold all items, inside of it, hopefully to be called inside of itself
//0.......1............2........3......4.... array element values
//name, description, effect, price, item type
_global.itemlist = [
//CONSUMABLES
["Sweetroot", "Restores 50 HP", 50, 100, "consumable"],
["Sugarroot", "Restores 100 HP", 100, 500, "consumable"],
["Candyroot", "Fully restores HP", 9999, 5000, "consumable"],
["Bitterroot", "Restores 50 MP", 50, 200, "consumable"],
["Sourroot", "Restores 100 MP", 100, 600, "consumable"],
["Tartroot", "Fully restores MP", 9999, 10000, "consumable"]
];
//0.......1............2....3....4...5....6.....7. array element values
//name, description, user, str, sta, def, mst, item type
_global.weaponlist = [
//WEAPONS
["Secred", "A mysterious zanbato with seals on the blade", "azul", 50, 0, 25, 10, "weapon"],
["Illuminite", "A thin weapon with a diamond rod for focusing light", "aseru", 50, 10, 10, 50, "weapon"],
["Fists", "A bare handed attack", "malforn", 50, 0, 55, 0, "weapon"],
["Kazeshini", "An oddly shaped blade for quick attacks ", "vayle", 20, 10, 25, 10, "weapon"],
["Yamato", "A katana with a blue hilt and a lightweight blade", "aoizi", 50, 20, 35, 50, "weapon"],
["Saleria", "A heavy scythe made from chenre absorbing crystal", "laava", 50, 20, 35, 50, "weapon"]
];
//0.......1............2....3....4...5....6...7 array element values
//name, description, str, sta, def, mst, price, item type
_global.armorlist = [
["Clothes", "light clothing", 0, 10, 10, 0, 500, "armor"],
["Armor", "plated armor", 0, 20, 20, 0, 5000,"armor"],
["Chain", "a thick chain for attack or defense", 20, 10, 10, 0, 500,"armor"],
["Shawl", "thin fabric worn by women", 0, 0, 5, 10, 7000,"armor"],
["Bodysuit", "a thick full body suit", 10, 20, 20, 0, 7500,"armor"],
["Spiked Araments", "armor covered in spikes, increases attack strength", 30, 0, 20, 0, 8000,"armor"]
];
//0.......1............2....3....4...5....6...7 array element values
//name, description, str, sta, def, mst, price, item type
_global.acclist = [
["Blade Ring", "raises attack", 30, 0, 0, 0, 500,"accesory"],
["Vigor Ring", "raises stamina", 0, 30, 0, 0, 500,"accesory"],
["Shield Ring", "raises defense", 0, 0, 30, 0, 500,"accesory"],
["Chenre Ring", "raises mental", 0, 0, 0, 30, 500,"accesory"],
["Knife Ring", "greatly raises attack", 120, 0, 0, 0, 50000,"accesory"],
["Vehemence Ring", "greatly raises stamina", 0, 120, 0, 0, 50000,"accesory"],
["Aegis Ring", "greatly raises defense", 0, 0, 120, 0, 50000,"accesory"],
["Aura Ring", "greatly raises mental", 0, 0, 0, 120, 50000,"accesory"]
];
get item Name with item[0][0];
get item Description with item[0][1];
get item Effect with item[0][2];
Example : If item[0] is being addressed then... (item[0] [0] =
Secred (as Name). Then item[0] [1] = Fists (as Description), and so on. trace(item[0][4]); should trace the name Sweetroot.

Parsing paragraphs in applescript not working as expected

Posting the code is losing the formatting that is causing the issue, copying what I in the post will actually do what it should. To bad that isn't an option in using the script, so I uploaded the script file here with an example of the text built in that is causing the issue. I will try to convey what the issue is still.
I am pulling text from mail.app. The emails I am parsing have within them a list of dates (amongst other things):
5/27/2012
5/28/2012
5/29/2012
5/30/2012
5/31/2012
6/1/2012
6/3/2012
6/4/2012
6/5/2012
6/6/2012
Now I'm trying to get the dates into a list. No problem I thought...
The following did NOT work:
Using paragraphs did NOT work, returned the entire thing as a paragraph
set AppleScript's text item delimiters to (ASCII character 13) -- (Carriage Return)
set AppleScript's text item delimiters to (ASCII character 10) -- (LF)
Neither of the delimiters worked. I wondered what exactly the ASCII code of the 'return' was so I made the following:
set rundates to "5/27/2012
   5/28/2012
   5/29/2012
   5/30/2012
   5/31/2012
   6/1/2012
   6/3/2012
   6/4/2012
   6/5/2012
   6/6/2012
   6/7/2012
   6/8/2012
   6/10/2012
   6/11/2012"
set mylist to {}
repeat with z from 1 to count of characters of rundates
copy (ASCII number (character z of rundates)) to end of mylist
end repeat
--return mylist ---{53, 47, 50, 55, 47, 50, 48, 49, 50, 13, 53, 47, 50, 56, 47, 50, 48, 49, 50, 13, 53, 47, 50, 57, 47, 50, 48, 49, 50, 13, 53, 47, 51, 48, 47, 50, 48, 49, 50, 13, 53, 47, 51, 49, 47, 50, 48, 49, 50, 13, 54, 47, 49, 47, 50, 48, 49, 50, 13, 54, 47, 51, 47, 50, 48, 49, 50, 13, 54, 47, 52, 47, 50, 48, 49, 50, 13, 54, 47, 53, 47, 50, 48, 49, 50, 13, 54, 47, 54, 47, 50, 48, 49, 50, 13, 54, 47, 55, 47, 50, 48, 49, 50, 13, 54, 47, 56, 47, 50, 48, 49, 50, 13, 54, 47, 49, 48, 47, 50, 48, 49, 50, 13, 54, 47, 49, 49, 47, 50, 48, 49, 50}
---===== Notice the 13s? So this should work right? ====---
So my delimiter using 13 should have worked, but it doesn't.
Anyone have any ideas?
I get different results from your post of the ascii numbers. Actually now that applescript is unicode we use "id" now instead of ascii number. It seems your character is "8232". So use this in your code before you get the text items...
set AppleScript's text item delimiters to character id 8232

Resources