Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active April 26, 2021 13:37
Show Gist options
  • Save tanaikech/3143be7e7df8cc595d73427d22ae2e0e to your computer and use it in GitHub Desktop.
Save tanaikech/3143be7e7df8cc595d73427d22ae2e0e to your computer and use it in GitHub Desktop.
Retrieving Size of Tables in Google Slides using Google Apps Script

Retrieving Size of Tables in Google Slides using Google Apps Script

This sample script is for retrieving the size (width and height) of a table in Google Slides using Google Apps Script.

There are no methods for directly retrieving the table size using SlidesApp yet. So I thought of a workaround using Slides API.

  • When the slide information is retrieved using Slides.Presentations.Pages.get() of Slides API, the information of tables is also included. In the information, the height and width of table are also included.
  • But the unit is EMU (English Metric Unit), and the height and width is separated by each cell. So it is required to sum each height and width while modifying the unit.

The modified script reflected this is as follows.

In order to use this script, please enable Slides API at Advanced Google Services and API console.

var s = SlidesApp.getActivePresentation();
var presentationId = s.getId();
var pageObjectId = s.getSlides()[0].getObjectId(); // In this sample, I use 1st page. But if you want to use other page, please get the "getObjectId".

var pageElements = Slides.Presentations.Pages.get(presentationId, pageObjectId).pageElements;
var result = [];
for (var i in pageElements) {
  var rowHeight = 0;
  var rowWidth = 0;
  if (pageElements[i].table) {
    for (var j in pageElements[i].table.tableRows) {
      rowHeight += pageElements[i].table.tableRows[j].rowHeight.magnitude / 12700;
    }
    for (var k in pageElements[i].table.tableColumns) {
      rowWidth += pageElements[i].table.tableColumns[k].columnWidth.magnitude / 12700;
    }
    result.push({Height: rowHeight, Width: rowWidth});
  }
}
Logger.log(result)

Result :

[
    {
        "Height": 200,
        "Width": 300
    }
]

Note :

  • Unit of output values is point.
  • Because 12,700 EMUs per point, each values retrieved by pageElements[i].table.tableRows[j].rowHeight.magnitude and pageElements[i].table.tableColumns[k].columnWidth.magnitude are required to be divided by 12,700.
  • When there are several tables in the slide, this sample script retrieves the width and height for each table.

Reference :

@joeybronner
Copy link

"Slides" on line 5 in your code doesn't work...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment