Skip to content
Snippets Groups Projects
Commit 5d8cb7d5 authored by Sebastian Listl's avatar Sebastian Listl :speech_balloon:
Browse files

1064788 Odt table replacing

parent 8cb82a44
No related branches found
No related tags found
No related merge requests found
......@@ -744,6 +744,17 @@ DocumentTemplate.prototype._getReplacedODT = function (pReplacements, pTableData
pReplacements = [pReplacements];
if (!pTableData)
pTableData = [];
var tablePlaceholders = [];
if (pTableData.length > 0)
{
//pTableData[0] = first document
tablePlaceholders = pTableData[0].map(function (tblData)
{
if (tblData && tblData.length > 0)
return new Set(Object.keys(tblData[0])); //tblData[0] = first row
return new Set();
});
}
if (pReplacements.length !== 0)
{
......@@ -775,31 +786,48 @@ DocumentTemplate.prototype._getReplacedODT = function (pReplacements, pTableData
let tables = pTableData[i] || [];
let tableEnd = 0;
for (let tblIndex = 0; tblIndex < tables.length; tblIndex++) //iterate over all tables in the document
//for (let tblIndex = 0; tblIndex < tables.length; tblIndex++) //iterate over all tables in the document
if (tables.length > 0)
{
let tableData = tables[tblIndex];
if (tableData && tableData.length > 0)
let hasMoreTables = currentBody.includes("</table:table>");
for (let tblI = 0; tblI < 10 && hasMoreTables; tblI++)
{
tableEnd = currentBody.indexOf("</table:table>", tableEnd) + 14;
if (tableEnd === -1) //stop if there is no table
break;
let rowBegin = currentBody.slice(0, tableEnd).lastIndexOf("<table:table-row");
let rowEnd = currentBody.indexOf("</table:table-row>", rowBegin) + 18;
let afterTable = currentBody.slice(rowEnd);
let tableRow = currentBody.slice(rowBegin, rowEnd);
tableEnd -= tableRow.length;
currentBody = currentBody.slice(0, rowBegin);
for (let rowIndex = 0; rowIndex < tableData.length; rowIndex++)
tableEnd = currentBody.indexOf("</table:table>", tableEnd);
if (tableEnd !== -1) //stop if there is no table
{
let tableRowData = tableData[rowIndex];
let replacedRow = that._replaceText(tableRow, tableRowData);
currentBody += replacedRow;
tableEnd += replacedRow.length;
tableEnd += 14;
let rowBegin = currentBody.slice(0, tableEnd).lastIndexOf("<table:table-row");
let rowEnd = currentBody.indexOf("</table:table-row>", rowBegin) + 18;
let tableRow = currentBody.slice(rowBegin, rowEnd);
let rowPlaceholders = tableRow.match(PlaceholderUtils.getRegexpMatchAll());
//find the table data that contains all required placeholders
let tableDataIndex = !rowPlaceholders ? -1 : tablePlaceholders.findIndex(function (placeholderSet)
{
return rowPlaceholders.every(function (placeholderName)
{
return placeholderSet.has(placeholderName);
});
});
if (tableDataIndex !== -1)
{
let afterTable = currentBody.slice(rowEnd);
currentBody = currentBody.slice(0, rowBegin);
tableEnd -= tableRow.length;
let tableData = tables[tableDataIndex];
for (let rowIndex = 0; rowIndex < tableData.length; rowIndex++)
{
let tableRowData = tableData[rowIndex];
let replacedRow = that._replaceText(tableRow, tableRowData);
currentBody += replacedRow;
tableEnd += replacedRow.length;
}
currentBody += afterTable;
}
}
currentBody += afterTable;
else
hasMoreTables = false;
}
}
fullBody += currentBody;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment