How to get the correct height of Table cell when text inside the cell

前端 未结 1 1325
无人及你
无人及你 2021-01-28 07:40

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

相关标签:
1条回答
  • 2021-01-28 08:20

    How about this modification?

    Modification points:

    • When I checked getSpaceAbove() and getSpaceBelow(), it seems that those are 0.
    • 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.
    0 讨论(0)
提交回复
热议问题