Search code examples
google-apps-scriptgoogle-slides-apigoogle-slides

Setting width and height of tables with Google Slides Apps Script


I'm using Google Apps Script to manipulate a Slides file. I want to create a table on a slide and set the height/width of a row/column, of a particular cell, or of the table overall. The documentation is mysteriously silent on this, except for this page which doesn't seem very promising.

Anybody know a workaround for this, or do I have to resort to the Slides API?


Solution

    • You want to modify the width and height of a table on Google Slides using Google Apps Script.
    • You want to modify the width and height of a specific cell.

    I could understand like above.

    You can set the height and width of a table using Slides Service, when new table is created. But in the current stage, the height and width of created table and the the specific cell cannot be modified using using Slides Service, yet. I think that this might be achieved in the future update.

    As a current workaround, when Slides API is used, this can be achieved.

    Sample script 1:

    In this sample script, Slides Service is used. New table is created with 300 point and 100 point in the width and height on the 1st page of Slides, respectively.

    var slides = SlidesApp.getActivePresentation();
    var slide = slides.getSlides()[0];
    var table = slide.insertTable(3, 3, 0, 0, 300, 100);
    

    Sample script 2:

    In this sample script, Slides API is used. The the width and height of the cell "B2" of table on the 1st page are modified to 300 point and 100 point, respectively.

    var slides = SlidesApp.getActivePresentation();
    var slide = slides.getSlides()[0];
    var objectId = slide.getTables()[0].getObjectId();
    SlidesApp.getActivePresentation().saveAndClose();
    var resource = {requests: [
      {updateTableColumnProperties: {tableColumnProperties: 
        {columnWidth: {magnitude: 300, unit: "PT"}},
        columnIndices: [1],
        objectId: objectId,
        fields: "columnWidth"
      }},
      {updateTableRowProperties: {tableRowProperties: 
        {minRowHeight: {magnitude: 100, unit: "PT"}},
        rowIndices: [1],
        objectId: objectId,
        fields: "minRowHeight"
      }}
    ]};
    Slides.Presentations.batchUpdate(resource, slides.getId());
    

    Sample script 3:

    In this sample script, using only Slides API, the result of "Sample script 1" and "Sample script 2" can be obtained.

    var slides = SlidesApp.getActivePresentation();
    var slide = slides.getSlides()[0];
    var objectId = "sampleTable" + (new Date().getTime());
    var resource = {requests: [
      {createTable: {
        rows: 3,
        columns: 3,
        elementProperties: {pageObjectId: slide.getObjectId(), size: {width: {magnitude: 300, unit: "PT"}, height: {magnitude: 100, unit: "PT"}}},
        objectId: objectId
      }},
      {updateTableColumnProperties: {
        tableColumnProperties: {columnWidth: {magnitude: 300, unit: "PT"}},
        columnIndices: [1],
        objectId: objectId,
        fields: "columnWidth"
      }},
      {updateTableRowProperties: {
        tableRowProperties: {minRowHeight: {magnitude: 100, unit: "PT"}},
        rowIndices: [1],
        objectId: objectId,
        fields: "minRowHeight"
      }}
    ]};
    Slides.Presentations.batchUpdate(resource, slides.getId());
    

    Note:

    • When you use Slides API, please enable it at Advanced Google Services.
      • From April 8, 2019, when new script project (standalone script type and container-bound script type) is created and the APIs are enabled at Advanced Google Services, the APIs got to be able to be used. Because the APIs are automatically enabled in the default GCP project when the script project is saved.

    References: