单元格中的文本时如何获取表格单元格的正确高度 [英] How to get the correct height of Table cell when text inside the cell
问题描述
历史记录:-
对Google Apps脚本的限制:我们无法获得单元格的高度,无法获得单元格的填充.
经过一些研究,试图将单元格的段落提取为文本.
使用我和
此修改如何?
修改点:
- 当我检查
getSpaceAbove()
和getSpaceBelow()
时,似乎它们是0
. - 我认为可以使用
getLineSpacing()
. - 而且,我注意到了另一个重要的观点.例如,当有3个段落时,似乎需要使用4个段落的空格.
当这一点反映到您的
注意:
- 这是您的示例的简单示例脚本.因此,当您使用其他示例时,可能需要修改脚本.请注意这一点.
- 在这种情况下,从您的示例中,假设每个段落的字体大小都相同.请注意这一点.
History : - Shape size not equal to the table cell size and fit the text inside the shape
Solution was working perfectly, but i found some issues when we would change the text height of any text Like I did with "Text 1", I changed the height of "Text 1", then the result of row height will be 0.0
Limitation of Google Apps Script: We can't get the height of cell, can't get the padding of cell.
After some research tried to extract the cell's paragraph as text.
Updated script with my and Tanaike modification :
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);
Full script here https://pastebin.com/keXKHbhC
Here is my Table in GSlide - https://docs.google.com/presentation/d/10nupvk-2BZDSV6-gPn_n2LkrUtkCxot7qr_jcZGBHqw/edit#slide=id.p
Still issue is same with height of shape equal to table cell , See image
How about this modification?
Modification points:
- When I checked
getSpaceAbove()
andgetSpaceBelow()
, it seems that those are0
. - I think that
getLineSpacing()
might be used. - And, I noticed one more important point. For example, when there are 3 paragraphs, it seems that it is required to use the spaces of 4 paragraphs.
When this points are reflected to your script of https://pastebin.com/keXKHbhC, it becomes as follows.
Modified script:
From:
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)
});
To:
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;
});
And also, please modify as follows.
From:
insertedShape.getText()
.getParagraphStyle()
.setLineSpacing(cellParagraph.getLineSpacing()/100)
.setSpaceAbove(cellParagraph.getSpaceAbove())
.setSpaceBelow(cellParagraph.getSpaceBelow())
.setSpacingMode(cellParagraph.getSpacingMode())
.setParagraphAlignment(cellParagraph.getParagraphAlignment())
To:
insertedShape.getText()
.getParagraphStyle()
.setParagraphAlignment(cellParagraph.getParagraphAlignment())
Result:
When your sample Google Slides is used with above modified script, it becomes as follows.
Note:
- This is a simple sample script for your sample. So when you use other sample, the script might be required to be modified. Please be careful this.
- In this case, from your sample, it supposes that each paragraph is the same font size. Please be careful this.
这篇关于单元格中的文本时如何获取表格单元格的正确高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!