How to Merge 2 Table cells in 'Google Slide' using Google Apps Script

Viewed 2131

I tried to merge the 2 table cells in Google Slide, It's throwing the Error of TypeError: Cannot find function merge in object TableCell. (line 47, file "Tester")

My sample function is:

function googleSlideCellsMerge(){
    var table=SlidesApp.getActivePresentation().getSlides()[0].getTables()[0];

    var row = table.getRow(1);
    var cell1 = row.getCell(0);
    var cell2 = row.getCell(1);

    var mergeCell = cell2.merge();

    Logger.log(mergeCell);
} 

In the above example, I tried to merge the 2 cells of Row 1.

I think the above code works with the Google Docs, but It's not for me with Google Slide.

2 Answers
  • You want to merge cells of a table on the Slides using Google Apps Script.

If my understanding is correct, how about this sample script? Unfortunately, in the current stage, merging cells of a table cannot be achieved using Slides Service, yet. But when Slides API is used, it can be achieved.

Before you use this script, please enable Slides API at Advanced Google services.

Sample script:

var slide = SlidesApp.getActivePresentation();
var table = slide.getSlides()[0].getTables()[0];
var resource = {requests:[{mergeTableCells: {
  objectId: table.getObjectId(),
  tableRange: {
    location: {rowIndex: 0, columnIndex: 0},
    rowSpan: 2,
    columnSpan: 2
  }
}}]};
Slides.Presentations.batchUpdate(resource, slide.getId());
  • In this sample script, the cells of a table of the 1st slide are merged.
    • When A1Notation is used, there is a table of "A1:C3", and the cells of "A1:B2" are merged.
  • When you want to merge the 2 cells of Row 1, please modify rowSpan from 2 to 1.

Result:

Before:

enter image description here

After:

enter image description here

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Unfortunately right now Google does not offer the possibility to merge cells in Google Slides tables through Apps Script. You can see here that a corresponding feature request has been submitted already:

https://issuetracker.google.com/issues/114200118

If you give the request a star, indicating your interest in the feature - this will increase the visibility of the impact of the feature. In the meantime you can try a workaround, e.g. copying the table to Google Sheets or Google Docs, merging the cells there and copy the table back.

Related