var { normalizePosition, normalizeColorInOptions, validateEnum } = require('./index'); function renderTable(slide, tableData) { var opts = {}; if (tableData.options) { for (var k in tableData.options) { if (tableData.options.hasOwnProperty(k)) opts[k] = tableData.options[k]; } } normalizePosition(opts); if (opts.fill && typeof opts.fill === 'object') { normalizeColorInOptions(opts.fill, 'color'); validateEnum(opts.fill.type, ['none', 'solid'], 'fill.type'); } if (opts.border) { if (Array.isArray(opts.border)) { if (opts.border.length !== 4) { console.warn('Invalid table border array: expected 4 sides, got ' + opts.border.length); } } else if (typeof opts.border === 'object') { normalizeColorInOptions(opts.border, 'color'); if (opts.border.type !== undefined) { validateEnum(opts.border.type, ['none', 'dash', 'solid'], 'border.type'); } } } var rows = []; if (tableData.rows) { for (var r = 0; r < tableData.rows.length; r++) { var row = tableData.rows[r]; var newRow = []; for (var c = 0; c < row.length; c++) { var cell = row[c]; var newCell = { text: cell.text }; if (cell.colspan !== undefined) { if (typeof cell.colspan !== 'number' || !isFinite(cell.colspan) || cell.colspan < 1 || cell.colspan !== Math.floor(cell.colspan)) { console.warn('Invalid colspan: got ' + JSON.stringify(cell.colspan) + ', expected integer >= 1'); } newCell.colspan = cell.colspan; } if (cell.rowspan !== undefined) { if (typeof cell.rowspan !== 'number' || !isFinite(cell.rowspan) || cell.rowspan < 1 || cell.rowspan !== Math.floor(cell.rowspan)) { console.warn('Invalid rowspan: got ' + JSON.stringify(cell.rowspan) + ', expected integer >= 1'); } newCell.rowspan = cell.rowspan; } if (cell.options) { newCell.options = {}; for (var k in cell.options) { if (cell.options.hasOwnProperty(k)) newCell.options[k] = cell.options[k]; } normalizeColorInOptions(newCell.options, 'color'); normalizeColorInOptions(newCell.options, 'highlight'); if (newCell.options.fill && typeof newCell.options.fill === 'object') { normalizeColorInOptions(newCell.options.fill, 'color'); } if (newCell.options.border) { if (Array.isArray(newCell.options.border)) { if (newCell.options.border.length !== 4) { console.warn('Invalid cell border array: expected 4 sides, got ' + newCell.options.border.length); } } else if (typeof newCell.options.border === 'object') { normalizeColorInOptions(newCell.options.border, 'color'); } } validateEnum(newCell.options.align, ['left', 'center', 'right', 'justify'], 'cell.align'); validateEnum(newCell.options.valign, ['top', 'middle', 'bottom'], 'cell.valign'); } newRow.push(newCell); } rows.push(newRow); } } slide.addTable(rows, opts); } module.exports = { renderTable };