-1

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 :)

Yoni
  • 62
  • 1
  • 1
  • 6
  • Please share your code. – Neven Subotic Feb 23 '22 at 19:11
  • Please provide [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Century Tuna Feb 23 '22 at 21:20
  • I've added my code to the question (notice I know nothing in gs\js and improvising as I go from examples) – Yoni Feb 24 '22 at 14:07
  • @NevenSubotic - I've updated the question with code example... – Yoni Feb 24 '22 at 18:44
  • I was able to replicate and test your current code, but for further clarification, the script was working as intended. Can you further elaborate on this statement? "Is there a way to wait for the request to finish? Something else?" because the reason why the updated values are already appearing on the logs is because the request is already finished – Century Tuna Feb 24 '22 at 23:35

2 Answers2

2

I recommend giving it some time before checking for the updated value. The function to try is Utilities.sleep(numberOfSeconds), see docs.

Basically, before you prepend it to this section as shown here:

Utilities.sleep(1000)
colsW = getColumnsWidth(table)
Logger.log('columns width (after update): %s', colsW);

If that does not work then I suggest you explicitly load the updated file again, e.g.

 
 const originalTable = getTableInSlide()
 changeColumnWidth(originalTable, 1, 2,)
 const updatedTable =  getTableInSlide()
 const newWidth = getColumnsWidth(updatedTable)
 

Neven Subotic
  • 1,399
  • 1
  • 6
  • 18
  • Thanks for the help, I was looking how to interduce a sleep and couldn't find it. Sadly this didn't help, I enter a 10,000 milisec sleep (the parameter into sleep is actually in milliseconds not seconds), I can see the affect of the resize ~immediately on the table but I still get the same result – Yoni Feb 25 '22 at 10:37
  • 1
    I updated my suggestions above – Neven Subotic Feb 26 '22 at 09:45
0

Not sure I fully got the example, but I had a similar problem. I solved it by using the following command: saveAndClose() Then reopening the Presentation. It forces execution completion.

FuCo
  • 15
  • 4