我正在编写一个脚本,使用Google Apps脚本在Google文档中生成一些表格。我正在寻找均匀分布这些表的行。基本上,要完成与在Google文档中右键单击所述表并选择Distribute Rows相同的操作。这可能吗?
现在我有一个功能完备的脚本来生成这些表。在里面,我有:
var tableData = [['1', '2'],
['3', '4']];
table = body.appendTable(tableData);
///Now I want something like
// table.distributeRows(true);
答案 0 :(得分:0)
没有内置方法可以均匀地分配列。但是,您可以编写自己的函数来计算页面宽度,然后将每列的宽度设置为相等。
function colWidth() {
var doc = DocumentApp.getActiveDocument().getBody();
// Get the table. If you have multiple, you'll need another loop.
var table = doc.getTables();
// Calculate the width of the document
var docWidth = (doc.getPageWidth() - doc.getMarginLeft() - doc.getMarginRight());
// Rows are children of tables. Loop through each row.
for(var i=0; i<table[0].getNumChildren(); i++) {
// Columns are children of rows. Loop through each column.
for(var j=0; j<table[0].getRow(i).getNumChildren(); j++) {
// Get the cell and set the calculated width. You probably only need to do this for the first row.
var cols = table[0].getRow(i).getCell(j);
cols.setWidth((docWidth / table[0].getRow(i).getNumChildren()));
}
}
}
设置行高相似。没有方法可以获得表格的高度,因此您需要从各个行高度计算它。
function rowHeight() {
var doc = DocumentApp.getActiveDocument().getBody();
var table = doc.getTables();
// set an integer to hold the current table height
var tableHeight = 0;
var numRows = table[0].getNumChildren();
// get the height of the table
for(var i=0; i<numRows; i++) {
tableHeight += table[0].getRow(i).getMinimumHeight();
}
// loop again to set the height for each row evenly
for(var j=0; j<numRows; j++) {
table[0].getRow(j).setMinimumHeight(tableHeight / numRows);
}
}