Cómo obtener la altura correcta de la celda de la tabla cuando el texto está dentro de la celda
Historial: - El tamaño de la forma no es igual al tamaño de la celda de la tabla y se ajusta al texto dentro de la forma.
La solución funcionaba perfectamente, pero encontré algunos problemas cuando cambiábamos la altura del texto de cualquier texto. Como hice con " Texto 1 ", cambié la altura de " Texto 1 ", entonces el resultado de la altura de la fila será 0.0

Limitación de Google Apps Script: no podemos obtener la altura de la celda, no podemos obtener el relleno de la celda.
Después de algunas investigaciones, traté de extraer el párrafo de la celda como texto.
Script actualizado con mi modificación y la de 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);
Guión completo aquí https://pastebin.com/keXKHbhC
Aquí está mi tabla en GSlide: https://docs.google.com/presentation/d/10nupvk-2BZDSV6-gPn_n2LkrUtkCxot7qr_jcZGBHqw/edit#slide=id.p
Aún así, el problema es el mismo con la altura de la forma igual a la celda de la tabla, ver imagen

Respuestas
¿Qué tal esta modificación?
Puntos de modificación:
- Cuando revisé
getSpaceAbove()
ygetSpaceBelow()
, parece que esos son0
. - Creo que
getLineSpacing()
podría usarse. - Y noté un punto más importante. Por ejemplo, cuando hay 3 párrafos, parece que se requiere usar los espacios de 4 párrafos.
Cuando estos puntos se reflejan en su guión de https://pastebin.com/keXKHbhC, se convierte en el siguiente.
Guión modificado:
Desde:
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)
});
A:
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;
});
Y también, modifique de la siguiente manera.
Desde:
insertedShape.getText()
.getParagraphStyle()
.setLineSpacing(cellParagraph.getLineSpacing()/100)
.setSpaceAbove(cellParagraph.getSpaceAbove())
.setSpaceBelow(cellParagraph.getSpaceBelow())
.setSpacingMode(cellParagraph.getSpacingMode())
.setParagraphAlignment(cellParagraph.getParagraphAlignment())
A:
insertedShape.getText()
.getParagraphStyle()
.setParagraphAlignment(cellParagraph.getParagraphAlignment())
Resultado:
Cuando su muestra de Presentaciones de Google se utiliza con la secuencia de comandos modificada anterior, se convierte en lo siguiente.

Nota:
- Este es un script de muestra simple para su muestra. Por lo tanto, cuando utilice otra muestra, es posible que deba modificar el script. Tenga cuidado con esto.
- En este caso, de su muestra, se supone que cada párrafo tiene el mismo tamaño de fuente. Tenga cuidado con esto.