This is a sample script for managing the row height and the column width of a table on Google Slides using Google Apps Script.
In the current stage, Google Slides service (SlidesApp) cannot manage the row height and the column width of the table on Google Slides, while the table width and height can be managed. But, fortunately, when Google Slides API is used, this can be achieved.
In this post, I would like to introduce a sample script for managing the row height and the column width of a table on Google Slides using Google Apps Script.
Sample script
Please copy and paste the following script to the script editor of Google Slides and save the script. When you test this script, please enable Slides API at Advanced Google services. Ref
function myFunction() {
const numRows = 3;
const numCols = 3;
const rowHeights = [50, 100, 200];
const colWidths = [200, 100, 50];
// Create a new table.
const s = SlidesApp.getActivePresentation();
const slide = s.getSlides()[0]; // 1st slide is used.
const table = slide.insertTable(numRows, numCols);
const objectId = table.getObjectId();
s.saveAndClose();
// Create a request body for Slides API.
const requests1 = rowHeights.map((e, i) => ({
updateTableRowProperties: {
objectId,
rowIndices: [i],
tableRowProperties: { minRowHeight: { magnitude: e, unit: "PT" } },
fields: "minRowHeight",
},
}));
const requests2 = colWidths.map((e, i) => ({
updateTableColumnProperties: {
objectId,
columnIndices: [i],
tableColumnProperties: { columnWidth: { magnitude: e, unit: "PT" } },
fields: "columnWidth",
},
}));
const requests = [...requests1, ...requests2];
// Request batchUpdate method of Slides API.
Slides.Presentations.batchUpdate({ requests }, s.getId());
}
Testing
When this script is run, the following result is obtained. You can see that the row heights and column widths of the default table are changed.
Reference
- This method was answered to this thread on Stackoverflow.