Shape size not equal to the table cell size and fit the text inside the shape

Viewed 523

Trying to insert a shape equal to the table cell size.

I created the shape using InsertShape() and copied the height of row (possibly height of cell), width of column (possibly width of cell) to the shape.

Problem :

  1. size of shape is not equal to cell size in height and width.
  2. The text inside the shape is going out of shape, text of shape should be auto fit, while the text of the cell fits inside the cell.

I can't say how .getRow().getMinimumHeight() works.

Text length not fixed. It could be like a long paragraph.

Limitation of Google Apps Script :

Table Cell - Google Apps Script can't get the width and height of each cell

Google Apps Script:

let activePresentation = SlidesApp.getActivePresentation()
let selection = activePresentation.getSelection()
let pageElements = selection.getPageElementRange().getPageElements()

//get table Left, table Top of each table under the pageElement object
var tableLeft = pageElements[0].asTable().getLeft(),
    tableTop = pageElements[0].asTable().getTop();

var cellTop = tableTop;       //cell top possible equal to table top   
var cellLeft = tableLeft;    //cell top possible equal to table Left 

//get the row height and column width
var rowHeight = pageElements[0].asTable().getRow(0).getMinimumHeight();
var columnWidth = pageElements[0].asTable().getColumn(0).getWidth();

//get the internal text of cell
var cellText = pageElements[0].asTable().getCell(0, 0).getText().asString()

//insert shape equal to the table cell size
var shape_t = selection.getCurrentPage()
                   .insertShape(SlidesApp.ShapeType.RECTANGLE, 
                                         cellLeft+200, 
                                         cellTop, 
                                         columnWidth, 
                                         rowHeight);

//set the cell text insde the Shape 
shape_t.getText().setText(cellText);

Here you can see the demo https://docs.google.com/presentation/d/10nupvk-2BZDSV6-gPn_n2LkrUtkCxot7qr_jcZGBHqw/edit?usp=sharing

Full Script : https://pastebin.com/CrV3qHwG

Replicate Issue Paste above mentioned https://pastebin.com/CrV3qHwG script in Google Script Editor, Add a table with 1 row and 1 column, obviously it will be only one cell. Select the table and then run the script by clicking on "Create Shape" from the "Test So" menu.

Image shows problem and expected result in quick view

Image shows problem and expected result in quick view

1 Answers

From your additional information, I could confirm your situation. When I saw it, I could notice that the table has one cell, and the table size is the default size. In this case, the table uses the default row height of the cell. And the height of cell is automatically set by the inputted texts. By this, the row height is only one row which is the height of 381000 EMU (30 pixels). About this situation, I confirmed that the same results were obtained from both Slides service and Slides API. So it seems that this is the current specification.

When you want to retrieve the actual height, in the current stage, I think that it is required to set the height by manual or script and retrieve the height. By this, the table is the actual row height.

Or, although this is not the completely same with the row height of the default table including the texts, I think that the row height can be also calculated using the line breaks and the font size. In this answer, I introduce the sample script for this by modifying your script.

Sample modification:

In order to give the height, as a sample modification, I would like to propose the following script. When your script is modified, it becomes as follows.

From:
var rowHeight = pageElements[0].asTable().getRow(0).getMinimumHeight();
To:
var t = pageElements[0].asTable().getRow(0).getCell(0).getText();
var rowHeight = (t.asString().split("\n").length - 1) * (t.getTextStyle().getFontSize() / 72 * 96);
  • In this modification, the row height is calculated using the line breaks and the font size.
    • t.asString().split("\n").length - 1 is the number of line breaks.
    • t.getTextStyle().getFontSize() / 72 * 96 is the pixel value of the font size. It seems that in Google Slides, 96 DPI is used.
    • But, unfortunately, in the current stage, the result is not the completely same with the default table row height.
    • In this case, when rowHeight is multiplied by 1.08, the same height is obtained. But this factor is unfounded. Please be careful this.
Related