Como obter a altura correta da célula da Tabela quando o texto dentro da célula

Aug 17 2020

Histórico: - O tamanho da forma não é igual ao tamanho da célula da tabela e cabe o texto dentro da forma

A solução estava funcionando perfeitamente, mas encontrei alguns problemas quando mudávamos a altura do texto de qualquer texto Como fiz com " Texto 1 ", mudei a altura do " Texto 1 ", então o resultado da altura da linha será 0,0

Limitação do Google Apps Script: Não podemos obter a altura da célula, não podemos obter o preenchimento da célula.

Após algumas pesquisas tentei extrair o parágrafo da célula como texto.

Script atualizado com minha modificação e 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);

Roteiro completo aqui https://pastebin.com/keXKHbhC

Aqui está minha mesa no GSlide - https://docs.google.com/presentation/d/10nupvk-2BZDSV6-gPn_n2LkrUtkCxot7qr_jcZGBHqw/edit#slide=id.p

O problema ainda é o mesmo com a altura da forma igual à célula da tabela, veja a imagem

Respostas

1 Tanaike Aug 17 2020 at 23:57

Que tal essa modificação?

Pontos de modificação:

  • Quando verifiquei getSpaceAbove()e getSpaceBelow(), parece que são 0.
  • Acho que getLineSpacing()pode ser usado.
  • E, percebi mais um ponto importante. Por exemplo, quando há 3 parágrafos, parece que é necessário usar os espaços de 4 parágrafos.

Quando esses pontos são refletidos em seu script de https://pastebin.com/keXKHbhC, torna-se o seguinte.

Script modificado:

De:

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)

});

Para:

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;
});

E também, modifique como segue.

De:

insertedShape.getText()
            .getParagraphStyle()
            .setLineSpacing(cellParagraph.getLineSpacing()/100)
            .setSpaceAbove(cellParagraph.getSpaceAbove())
            .setSpaceBelow(cellParagraph.getSpaceBelow())
            .setSpacingMode(cellParagraph.getSpacingMode())
            .setParagraphAlignment(cellParagraph.getParagraphAlignment())

Para:

insertedShape.getText()
            .getParagraphStyle()
            .setParagraphAlignment(cellParagraph.getParagraphAlignment())

Resultado:

Quando seu exemplo de Apresentações Google é usado com o script modificado acima, ele se torna o seguinte.

Nota:

  • Este é um script de amostra simples para sua amostra. Portanto, ao usar outra amostra, pode ser necessário modificar o script. Por favor, tome cuidado com isso.
  • Nesse caso, a partir de sua amostra, supõe-se que cada parágrafo tenha o mesmo tamanho de fonte. Por favor, tome cuidado com isso.