Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you set table cell padding properties using the Google Slides rest API?

Is there a way to change table cell padding properties of Google Slides tables via the rest API?

updateTableCellProperties does not seem to have any cell padding properties

like image 303
David Bayon Avatar asked Nov 07 '22 17:11

David Bayon


1 Answers

Setting Slide Table Padding can be done!!!, but the only way is to set up a template slides document with template table(s) with your desired padding. Then in the code that follows, you can copy the template table (and associated padding) to instances of tables in your target slides document, and then modify the table content accordingly. This really does work as at 26/09/2021. I use this all over the place! I hope you find this useful.

// open template slide document where you have manually create one or more tables with ONE cell ONLY with your desired padding
var lTemplatePresentation = SlidesApp.openById("1hd4b_EqLNmUEBQYkYTDrdeGEQ0E4d2lXXG2ClFBKkuo");
var lTemplateSlide = lTemplatePresentation.getSlides()[0];
var lTemplateTable = lTemplateSlide.getTables()[0];

// use the templates tables alt text title or description if you need multiple tables with different cell padding
Logger.log("Alt Text Title/Description: "+lTemplateTable.getTitle()+"/"+lTemplateTable.getDescription());

// open target presentation
var lMyPresentation = SlidesApp.openById("1-KWFQXtIsUW6Oo2dmpF54PqJHbVl258ph9bGMN6ifhM");
var lMySlide = lMyPresentation.getSlides()[0];

// this is the magic bit..   
var lMyTable = lMySlide.insertTable(lTemplateTable);

// modify table as required using normal methods
lMyTable.insertColumn(0); // yes cell padding is propogated from adjacent cells
lMyTable.insertColumn(0);
lMyTable.insertRow(0);
lMyTable.insertRow(0);
lMyTable.insertRow(0);
lMyTable.setTop(10).setLeft(10);

// NB: there seems to be a restriction on setting table width/height after the fact, so I suggest your template table
// has more columns which are sizes as needed. You can then always add more rows to your target tables programmatically.
// lMyTable.setHeight(100); // Note: 26/09/2021 Exception: Updating the size of this page element type is not supported.
// lMyTable.setWidth(300);  // Note: 26/09/2021 Exception: Updating the size of this page element type is not supported.

// Add text and modify paragraph indents to truely test cell padding has copied from the template slides document
var lMyCellA1 = lMyTable.getCell(0, 0);
var lMyCellTextRange = lMyCellA1.getText();
lMyCellTextRange.setText("HELLO WORLD ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !"
+"! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !");
var lMyParagraphStyle = lMyCellTextRange.getParagraphStyle();
lMyParagraphStyle.setLineSpacing(100 /*percent*/).setIndentFirstLine(0).setIndentStart(0);
lMyParagraphStyle.setIndentEnd(0).setSpaceBelow(0).setSpaceAbove(0);  

// that's it - job done!
lMyPresentation.saveAndClose();
lTemplatePresentation.saveAndClose();
like image 164
Jason Brown Avatar answered Nov 30 '22 08:11

Jason Brown