[英]How to set height of empty div inside table cell to height of that cell
[英]How to get the correct height of Table cell when text inside the cell
历史: - 形状大小不等于表格单元格大小并且适合形状内的文本
解决方案工作得很好,但是当我们改变任何文本的文本高度时我发现了一些问题就像我对“ Text 1 ”所做的那样,我改变了“ Text 1 ”的高度,那么行高的结果将为 0.0
Google Apps 脚本的限制:我们无法获取单元格的高度,无法获取单元格的内边距。
经过一些研究尝试将单元格的段落提取为文本。
用我和Tanaike修改更新了脚本:
var rowHeight = 0.0;
var tableCell = pageElements[index].asTable().getCell(ir, ic);
//Get cell color
let cellFill = tableCell.getFill().getSolidFill();
var cellParaText = tableCell.getText().getParagraphs();
cellParaText.forEach(function(item, index) {
var t = cellParaText[index].getRange();
//I tried to applied Tanaike's solution here
rowHeight = rowHeight + ( t.asString().split("\n").length - 1 ) * ( t.getTextStyle().getFontSize() / 72 * 96 )
//rowHeight = rowHeight + cellParaText[index].getRange().getTextStyle().getFontSize();
rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceAbove()
rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceBelow()
rowHeight = rowHeight + (cellParaText[index].getRange().getParagraphStyle().getLineSpacing()/100)
});
//Get cell text, text color and text background color
let cellText = tableCell.getText().asString()
//insert the RECTANGLE shape
let insertedShape = selection.getCurrentPage()
.insertShape(SlidesApp.ShapeType.RECTANGLE, cellLeft+5, cellTop+5, columnWidth, rowHeight);
完整的脚本在这里https://pastebin.com/keXKHbhC
这是我在 GSlide 中的表格 - https://docs.google.com/presentation/d/10nupvk-2BZDSV6-gPn_n2LkrUtkCxot7qr_jcZGBHqw/edit#slide=id.p
形状高度等于表格单元格的问题仍然相同,参见图片
这个改装怎么样?
getSpaceAbove()
和getSpaceBelow()
时,它们似乎是0
。getLineSpacing()
。当这一点反映到您的https://pastebin.com/keXKHbhC脚本中时,它变为如下。
cellParaText.forEach(function(item, index) {
var t = cellParaText[index].getRange();
//i tried to applied Tanike's solution here
rowHeight = rowHeight + ( t.asString().split("\n").length - 1 ) * ( t.getTextStyle().getFontSize() / 72 * 96 )
//rowHeight = rowHeight + cellParaText[index].getRange().getTextStyle().getFontSize();
rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceAbove()
rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceBelow()
rowHeight = rowHeight + (cellParaText[index].getRange().getParagraphStyle().getLineSpacing()/100)
});
cellParaText.forEach(function(item, index, a) {
var t = cellParaText[index].getRange();
var lineSpace = cellParaText[index].getRange().getParagraphStyle().getLineSpacing() / 100;
var fontSize = t.getTextStyle().getFontSize() / 72 * 96;
rowHeight += fontSize * (a.length > 1 ? lineSpace : 1);
if (index == a.length - 1) rowHeight += fontSize;
});
另外,请进行如下修改。
insertedShape.getText()
.getParagraphStyle()
.setLineSpacing(cellParagraph.getLineSpacing()/100)
.setSpaceAbove(cellParagraph.getSpaceAbove())
.setSpaceBelow(cellParagraph.getSpaceBelow())
.setSpacingMode(cellParagraph.getSpacingMode())
.setParagraphAlignment(cellParagraph.getParagraphAlignment())
insertedShape.getText()
.getParagraphStyle()
.setParagraphAlignment(cellParagraph.getParagraphAlignment())
当您的示例 Google 幻灯片与上述修改后的脚本一起使用时,它变为如下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.