diff --git a/process/DocumentTemplate_lib/process.js b/process/DocumentTemplate_lib/process.js index 3579096b94ccbc30fc17b3c2f1c748d8a1a1d5fd..3c074ffd23098632fde158cbebaa1bfad78e50f1 100644 --- a/process/DocumentTemplate_lib/process.js +++ b/process/DocumentTemplate_lib/process.js @@ -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;