Skip to content

Instantly share code, notes, and snippets.

@RCura
Created September 18, 2012 16:39
Show Gist options
  • Save RCura/3744189 to your computer and use it in GitHub Desktop.
Save RCura/3744189 to your computer and use it in GitHub Desktop.
function concatenateTables(table1, table2, columnsNameList)
{
gNewTable = new google.visualization.DataTable();
//alert(columnsNameList);
for (i in columnsNameList)
{
gNewTable.addColumn('string', columnsNameList[i], columnsNameList[i]);
};
// alert(gNewTable.toJSON());
t1Length = table1.getNumberOfRows();
t2Length = table2.getNumberOfRows();
gNewTable.addRows(t1Length + t2Length);
// Remplissage des premières cellules depuis table1
for (iRow=0; iRow < (t1Length + t2Length); iRow++)
{
for (iCol in columnsNameList)
{
var colName = columnsNameList[iCol];
var newTableColIndex = gNewTable.getColumnIndex(colName);
// On veut savoir si on est ds le premier ou le second tableau
if (iRow < t1Length) // On est dans le premier (table1)
{
var isPresent = table1.getColumnIndex(colName) != -1;
if (isPresent)
{
var oldTableColIndex = table1.getColumnIndex(colName);
gNewTable.setValue(iRow, newTableColIndex, table1.getValue(iRow, oldTableColIndex));
}
else
{
gNewTable.setValue(iRow, Number(iCol), "nop");
};
}
else // on est dans le second (table2)
{
var isPresent = table2.getColumnIndex(colName) != -1;
if (isPresent)
{
var oldTableColIndex = table2.getColumnIndex(colName);
gNewTable.setValue(iRow, newTableColIndex, table2.getValue((iRow - t1Length), oldTableColIndex));
}
else
{
gNewTable.setValue(iRow, Number(iCol), "nop");
};
};
};
};
return gNewTable;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment