I'm using the Slides API in my appsScript to change a tables column size, when later I query the tables column width I still get the old values.
When the script ends I can see in slides the table width has changed & next time I'll run the script it will return the updated width value.
Is there a way to wait for the request to finish? Something else?
To reproduce, open an empty google slides and place a table in the first (and only slide).
/**
* @OnlyCurrentDoc Limits the script to only accessing the current presentation.
*/
function getColumnsWidth(table) {
colsWidth = []
for (var c=0; c<table.getNumColumns(); c++) {
col = table.getColumn(c)
colsWidth.push(col.getWidth());
}
return colsWidth
}
function changeColumnWidth(table, columnsInd, W){
Logger.log('Called changeColumnWidth with columnsInd=%s, W=%s', columnsInd, W)
var presentation = SlidesApp.getActivePresentation();
var tableObjectId = table.getObjectId();
var resource = {"requests": [
{"updateTableColumnProperties": {
"objectId": tableObjectId,
"columnIndices": columnsInd,
"tableColumnProperties": {"columnWidth": {"unit": "PT", "magnitude": W}}, "fields": "*"}
}
]};
Slides.Presentations.batchUpdate(resource, presentation.getId());
}
function getTableInSlide(selection) {
var currentPage = selection.getCurrentPage();
pageElements = currentPage.getPageElements();
for (var i=0; i<pageElements.length; i++){
var elementType = pageElements[i].getPageElementType()
if (elementType == 'TABLE'){
return pageElements[i].asTable()
}
}
return Null
}
function myFunction() {
var presentation = SlidesApp.getActivePresentation();
var selection = presentation.getSelection();
table = getTableInSlide(selection)
var colsW = getColumnsWidth(table)
Logger.log('columns width (before update): %s', colsW);
changeColumnWidth(table, [0], colsW[0]*2)
colsW = getColumnsWidth(table)
Logger.log('columns width (after update): %s', colsW);
}
Output:
columns width (before update): [43.303149606299215, 181.63385826771653, 149.73228346456693]
Called changeColumnWidth with columnsInd=[0.0], W=86.60629921259843
columns width (after update): [**43.303149606299215**, 181.63385826771653, 149.73228346456693]
Next run output:
columns width (before update): [**86.60629921259843**, 181.63385826771653, 149.73228346456693]
Called changeColumnWidth with columnsInd=[0.0], W=173.21259842519686
columns width (after update): [86.60629921259843, 181.63385826771653, 149.73228346456693]
P.S I'm using the slides API as I didn't find any better way to change a tables column width (if there is - I'd love to learn that too :)