Can we add data dynamically into the table.
Hi, I am new to PptxGenJS, I have an API that gives me response as JSON. I would like to add that data into the tables of the PPT. Maybe, like an AJAX call to the API, and I could use the JSON data I receive as my source into the tables. Is it possible? Please let me know.
Hi @phavanagi
Any presentation.PPT file is a binary, zipped collection of XML files, so there's no interfacing with that. In addition, the PptxGenJS library cannot open a file and manipulate it, so the only other option is to use the library to generate a table using the data you receive - this is the "normal" usage scenario.
Example:
// [Get JSON via REST or whatever...]
// Create new Array() and fill with data (eg: arrTabRows)
var pptx = new PptxGenJS();
var slide = pptx.addNewSlide();
slide.addText('Here is that JSON data:', { x:0.5, y:0.7, font_size:18, font_face:'Arial', color:'0088CC' });
slide.addTable(
arrTabRows, { x:0.5, y:1.0, w:'90%' },
{ rowH:0.75, fill:'F7F7F7', font_size:14, color:'363636', border:{pt:'1', color:'BBCCDD'} }
);
pptx.save('json-data-demo');
Hi @gitbrent , Thanks for the reply :) I have the following code : It doesn't work and the PPT generated is corrupt. Can you let me know where I am going wrong? ` function insertCharacteristicsTable(slide) { var date = []; var yield = []; var avgQualMood = []; var portCde = [];
$.ajax({
type: 'GET',
url: "http://route"
dataType: 'json',
success: function(data){
//console.log(data.list[0].main.temp);
//trying to parse through data for graph
$.each(data,function(index, current){
date.push(current.as_of_date);
yield.push(current.yield);
avgQualMood.push(current.average_quality_moody);
portCde.push(current.port_code);
console.log(date + " " + yield + " " + avgQualMood + " " + portCde);
});
}
});
var rows = [
[
{ text: 'Characteristics', opts: { valign:'b', align:'l', font_face:'Arial', font_size:8, fill:'4C86A1', color:'ffffff', bold:true} },
{ text: 'Univ. of Illinois Short Portfolio (12/31/16)', opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, fill:'4C86A1', color:'ffffff', bold:true} },
{ text: 'Univ. of Illlinois Short Portfolio (3/31/17)', opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, fill:'4C86A1', color:'ffffff', bold:true} },
{ text: 'Bloomberg Barclays 1-3 Yr G/C Index (3/31/17)', opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, fill:'4C86A1', color:'ffffff', bold:true} },
],
[
{ text: date, opts: { valign:'b', align:'l', font_face:'Arial', font_size:8, color:'000000', margin:[1,1,1,10]} },
{ text: yield, opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, color:'000000', margin:[1,1,1,1]} },
{ text: avgQualMood, opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, color:'000000', margin:[1,1,1,1]} },
{ text: portCde, opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, color:'000000', margin:[1,1,1,1]} },
],
]
// Sets the position and size of the table, as well as the width of the columns. The position should stay the same,
// but the widths need to be dynamic in case there are fewer than 4 columns.
var tabOpts = { x:0.25, y:1.13, w:4.6, colW:[1.6,1,1,1]};
var celOpts = {
border:{ pt:'1', color:'FFFFFF' }
};
slide.addTable( rows, tabOpts, celOpts );
}
`
Hi @phavanagi,
the variables date, yield, avgQualMood and portCde are undefined at the moment of creating rows array. It is outside of the success callback.
Hi @kajda90 ,
I put it in success, but still doesn't work. It gives me a blank table. Am I using the arrays correctly? will it loop through on itd own? Shud we do something like date[0]. Kindly let me know. Attached is the modified code( Just putting it under success)
``
function insertCharacteristicsTable(slide)
{
var date = [];
var yield = [];
var avgQualMood = [];
var portCde = [];
$.ajax({
type: 'GET',
url: "http://reportingsystemapidev.incomeresearch.com/api/characteristics/04-30-2017/ALAS%20INC",
dataType: 'json',
success: function(data){
//console.log(data.list[0].main.temp);
//trying to parse through data for graph
$.each(data,function(index, current){
date.push(current.as_of_date);
yield.push(current.yield);
avgQualMood.push(current.average_quality_moody);
portCde.push(current.port_code);
});
console.log(date + " " + yield + " " + avgQualMood + " " + portCde);
var rows = [
[
{ text: 'Characteristics', opts: { valign:'b', align:'l', font_face:'Arial', font_size:8, fill:'4C86A1', color:'ffffff', bold:true} },
{ text: 'Univ. of Illinois Short Portfolio (12/31/16)', opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, fill:'4C86A1', color:'ffffff', bold:true} },
{ text: 'Univ. of Illlinois Short Portfolio (3/31/17)', opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, fill:'4C86A1', color:'ffffff', bold:true} },
{ text: 'Bloomberg Barclays 1-3 Yr G/C Index (3/31/17)', opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, fill:'4C86A1', color:'ffffff', bold:true} },
],
[
{ text: date, opts: { valign:'b', align:'l', font_face:'Arial', font_size:8, color:'000000', margin:[1,1,1,10]} },
{ text: yield, opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, color:'000000', margin:[1,1,1,1]} },
{ text: avgQualMood, opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, color:'000000', margin:[1,1,1,1]} },
{ text: portCde, opts: { valign:'t', align:'c', font_face:'Arial', font_size:8, color:'000000', margin:[1,1,1,1]} },
],
]
// Sets the position and size of the table, as well as the width of the columns. The position should stay the same,
// but the widths need to be dynamic in case there are fewer than 4 columns.
var tabOpts = { x:0.25, y:1.13, w:4.6, colW:[1.6,1,1,1]};
var celOpts = {
border:{ pt:'1', color:'FFFFFF' }
};
slide.addTable( rows, tabOpts, celOpts );
}
});
}