Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active August 5, 2021 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/a6b6cf2227953fdbf27f468b4574cd1e to your computer and use it in GitHub Desktop.
Save bjoerntx/a6b6cf2227953fdbf27f468b4574cd1e to your computer and use it in GitHub Desktop.
async function adaptTable()
{
var maxWidth = await getMaxWidth();
var tableInfo = await getTableInfo();
for (let row = 1; row <= tableInfo.rows ; row++) {
var rowWidth = await getRowWidth(row,
tableInfo.cols,
tableInfo.table);
var destinationWidth = rowWidth - maxWidth;
var tableCheck = await setRowWidth(row,
tableInfo.cols,
tableInfo.table,
destinationWidth,
rowWidth);
};
function getMaxWidth() {
var maxWidth = 0;
var pageWidth = 0;
var marginLeft = 0;
var marginRight = 0;
return new Promise(resolve => {
TXTextControl.setPageUnit(TXTextControl.MeasuringUnit.Twips, function() {
TXTextControl.sections.getItem( function (section) {
section.format.pageSize.getWidth(function (width) {
pageWidth = width;
section.format.pageMargins.getLeft(function (left) {
marginLeft = left;
section.format.pageMargins.getRight(function (right) {
marginRight = right;
resolve(pageWidth - marginLeft - marginRight);
});});});});});});
}
function getTableInfo() {
var cols = 0;
var rows = 0;
return new Promise(resolve => {
TXTextControl.tables.getItem( function (table) {
if (table === null)
return;
table.rows.getCount(function(rowCount) {
table.columns.getCount(function(colCount) {
var tableInfo = {
rows: rowCount,
cols: colCount,
table: table
};
resolve(tableInfo);
});});});});
}
function getRowWidth(row, colCount, table) {
return new Promise(resolve => {
var currentWidth = 0;
for (let col = 1; col <= colCount; col++) {
table.cells.getItem(function (cell) {
cell.getWidth(function (cellWidth) {
currentWidth = currentWidth + cellWidth;
if (col === colCount)
resolve(currentWidth);
});
}, null, row, col);};});
}
function setRowWidth(row, colCount, table, destinationWidth, currentWidth) {
return new Promise(resolve => {
for (let col = 1; col <= colCount; col++) {
// calculate the ratio percentage for each cell
table.cells.getItem(function (cell) {
cell.getWidth(function (cellWidth) {
var percentage = cellWidth / currentWidth;
cell.setWidth(parseInt(cellWidth - (destinationWidth * percentage)), function() {
if (col === colCount)
resolve(true);
});
});
}, null, row, col);};});}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment