I have a script that converts XML into "JSON". The data available is able to be accessed using styles.Default.FontName
(for example). However, instead of manually inserting 'Default' in that line, I need to be able to use a variable. I need to be able to use many different combinations of 'styles.XXXX.XXXX'.
Here is my script function convertData(xml) {
var styles = {}
$(xml).find('Style').each(function() {
var id = $(this).attr('ss:ID');
var name = $(this).attr('ss:Name');
var type = $(this).children();
var style = {};
styles[id] = style;
style['Name'] = name;
type.each(function() {
$.each(this.attributes, function() {
if (this.specified) {
style[stripPrefix(this.name)] = this.value;
}
});
});;
// You can now use 'styles.s57.FontName' and the like to return values :)
});
$(xml).find('Worksheet').each(function() {
var name = $(this).attr('ss:Name');
var data = $(this).find('Data');
data.each(function() {
var cell = $(this).parent('Cell');
var value = $(this).text();
var styleId = cell.attr('ss:StyleID');
$(window).load(function() {
// Here is my issue (see below for working calls...)
// This guy doesn't work (because of the 'styleId' variable not being an actual style id like 's57')
$('.testing').append('<span class="color:#f60;">' + styles.styleId.Color + '</span>');
});
});
});
$(document).ready(function() {
$('body').append('<div class="testing"/>');
// Works fine
$('.testing').html(styles.Default.FontName);
$('.testing').append(styles.s59.Bold);
$('body').append('<div class="json-output"/>');
$('.json-output').append(JSON.stringify(styles));
for (var i in styles) {
console.log(i);
}
});
}
So, how can I use a variable here instead of specifying 'Default' or 's59'? Any help is greatly appreciated!
To clarify what I am asking...
The use of **styles.Default.FontName**
is working fine. I just need to be able to dynamically swap 'Default' with a variable. Why? Because I will be using this inside loops that are iterating through "Cells" that each have a different "StyleID". This loop is shown above via data.each(). So each iteration through, there could be a different "StyleID" being referenced.
But when I use styles.SomeVariable.SomeStyle
, instead of inserting what value I have set for 'SomeVariable', it references the JSON object for "SomeVariable"...
UPDATE:
For reference- the generated JSON
{
"Default":{
"Name":"Normal",
"Vertical":"Bottom",
"FontName":"Calibri",
"Family":"Swiss",
"Size":"11",
"Color":"#000000"
},
"s57":{
"Name":"Hyperlink",
"FontName":"Calibri",
"Family":"Swiss",
"Size":"11",
"Color":"#0066CC",
"Underline":"Single"
},
"s58":{
"Horizontal":"Left",
"Vertical":"Center",
"Indent":"1"
},
"s59":{
"Vertical":"Center",
"WrapText":"1",
"FontName":"Calibri",
"Family":"Swiss",
"Size":"11",
"Color":"#000000",
"Bold":"1"
}
}