Google Slides API insert text in existing table

446 views
Skip to first unread message

Lis Kl

unread,
Jul 31, 2021, 12:04:09 PM7/31/21
to Google Apps Script Community
I have an existing presentation with a mockup table with all the dimensions and colors as wanted. It does not contain any text. This is the code I ran to try to populate the table. 
See the code in Stackoverflow https://stackoverflow.com/questions/68444131/new-information-google-slides-api-insert-text-in-existing-table

the error for every request in the batchUpdate looks like:

GoogleJsonResponseException: API call to slides.presentations.batchUpdate failed with error: 
Invalid JSON payload received. Unknown name "pageElements" at 'requests[0]': Cannot find field. 
Invalid JSON payload received. Unknown name "pageType" at 'requests[0]': Cannot find field. 
Invalid JSON payload received. Unknown name "objectId" at 'requests[0]': Cannot find field. 
Invalid JSON payload received. Unknown name "pageElements" at 'requests[1]': Cannot find field. 
Invalid JSON payload received. Unknown name "pageType" at 'requests[1]': Cannot find field. 
...

This is a typical request displayed by console.log:

{ "objectId": "SLIDES_API142383606_0" , "pageType": "SLIDE" , "pageElements": [ { "objectId": "SLIDES_API142383606_1" , "table": { "rows": 8 , "columns": 8 , "tableRows": [ { "tableCells": [ { "location": { "rowIndex": 3, "columnIndex": 2 } ,"text": { "nextVal": "silf" } } ] } ] } } ] }

I used the documentation but I find it very difficult. Any help appreciated.

Clark Lind

unread,
Aug 1, 2021, 11:43:29 AM8/1/21
to Google Apps Script Community
I may be wrong, but it seems to me that you are declaring nextVal, then inserting it into the objects before it has been updated. So it is "undefined" the first iteration.
You are doing this: 
    theTableCellObj = { "location": { "rowIndex": r ,"columnIndex": c }, "text": { nextVal } };   //but nextVal = undefined.

Then when you call this:     nextVal = inArr[r][c].toString();

No where has inArr been declared (at least in your visible code), and no where is inArr being populated with anything that I can see. Unless there is more code, what you've shared seems incomplete to me. (I'm not the best coder, sorry!)

Lis Kl

unread,
Aug 8, 2021, 8:03:49 PM8/8/21
to Google Apps Script Community
cwl  - Thanks for your reply. The array is populated in other code and passed in. All the entries in the array are populated. Put a space in the 'empty' ones so I would not have to detect empty / undefined / null / confusion. This is the display:
tblArr: [ [ ' ', ' ', 'A', 'B', 'C', 'D', 'E', 'F', ' ' ], [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ], [ '1', ' ', 'gub', 'ras', 'mef', 'crulp', 'buct', 'zub', ' ' ], [ '2', ' ', 'clack', 'bilp', 'qualt', 'posk', 'gelt', 'taft', ' ' ], [ '3', ' ', 'julm', 'crift', 'kep', 'sit', 'glap', 'trin', ' ' ], [ '4', ' ', 'jip', 'dolp', 'hush', 'sos', 'fresh', 'rop', ' ' ], [ '5', ' ', 'crit', 'bel', 'frel', 'hes', 'rift', 'lisp', ' ' ], [ '6', ' ', 'trus', 'drelf', 'memp', 'clas', 'belm', 'drulf', ' ' ], [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ] ]
I looks different in the log but the spaces have all been squashed out here. At your suggestion I have added:
      if ( !nextVal )    {
        nextVal = inArr[r][c].toString();
        console.log('r: ', r, ' c: ', c, ' val: ', nextVal );
        nextReq = 
        {
          'objectId': templateSlideId,
          'pageType':  'SLIDE',
          'pageElements': [thePageElement]
        }  
        requests.push(nextReq);
        console.log(JSON.stringify(nextReq) );
      }
It made no difference to the error messages unfortunately. Today I added a list of all the methods I have tried to the Stackoverflow https://stackoverflow.com/questions/68444131/new-information-google-slides-api-insert-text-in-existing-table  Maybe it will give someone more ideas of things to try.

I teach and my students are on small Chromebooks. I am creating 9x9 tables (labels, frame, and 6x6 grid) to display in a Google slide or Jamboard. I want it to fill the entire available space. The methods l which do not fail result in tiny tables swimming in lots of white space. Using the table in the spreadsheet wastes screen space with the toolbars for the spreadsheet.

Lis Kl

unread,
Aug 8, 2021, 8:05:53 PM8/8/21
to Google Apps Script Community
Here is a link to the whole mess with all the different methods I have tried. https://docs.google.com/spreadsheets/d/1xKoMy7JrguQMWInYCpz8IeRmQwQXW_zWs2jnryzhYBQ/edit?usp=sharing

Clark Lind

unread,
Aug 9, 2021, 2:16:30 PM8/9/21
to Google Apps Script Community
I am looking at it.

Clark Lind

unread,
Aug 9, 2021, 3:39:21 PM8/9/21
to Google Apps Script Community
I was able to get this to work:
function populateMockupTable(inArr)   {
  console.log('Begin populateMockupTable' );
//  console.log(inArr);
  const ui = SpreadsheetApp.getUi();
  let templatePres, slidesArr, tblArr;

  try    {
    templatePres = SlidesApp.openById(templatePresId);
  }  catch ( err)    {
    ui.alert('You do not have a presentation. Ending.');
    return;
  }
  
  try    {
    slidesArr = templatePres.getSlides();
  }  catch ( err)    {
    ui.alert('Could not get slides from presentation. Ending.');
    return;
  }
  let templateSlideId = slidesArr[1].getObjectId();
//  console.log('templateSlideId: ', templateSlideId );
  
  try    {
    tblArr = slidesArr[1].getTables();
  }  catch ( err)    {
    ui.alert('Could not get slides from presentation. Ending.');
    return;
  }
  let theTblId = tblArr[0].getObjectId();  // table page element
//  console.log('theTblId: ', theTblId );
  
    let requests =[], numRows = inArr.length, numCols = inArr[0].length, r, c;
  
  
  for ( let r = 0 ; r < numRows ; r++ ) {
    for ( let c = 0 ; c < numCols ; c++ ) {
      requests.push(
        {"insertText":
          {"objectId": theTblId
          ,"cellLocation": {
             "rowIndex": r,
             "columnIndex": c
           }
          ,"text": inArr[r][c].toString()
          }
        }
      );
    }
  }

//  console.log('number of updates: ', requests.length);
  if (requests.length > 0)   {
  var response = Slides.Presentations.batchUpdate(
      { 'requests': requests }, templatePresId );
  }
 

//  console.log('End populateMockupTable' );
//  console.log(requests);
}

And this works now too:
function insertText()   {
  console.log('Begin insertText' );
  var inArr = buildArr();
 // console.log(inArr );
  const ui = SpreadsheetApp.getUi();
  let templatePres = SlidesApp.openById(templatePresId);
  let slidesArr = templatePres.getSlides();
  let templateSlideId = slidesArr[0].getObjectId();
  console.log('templateSlideId: ', templateSlideId );
  let tblArr = slidesArr[0].getTables();
  let theTblId = tblArr[0].getObjectId();  // table page element
  numTblCols = tblArr[0].getNumColumns();
  numTblRows = tblArr[0].getNumRows();
  console.log('rows, Cols: ',numTblRows, numTblCols );
  
  let requests =[], numRows = inArr.length, numCols = inArr[0].length, r, c;
  
  
  for ( let r = 0 ; r < numRows ; r++ ) {
    for ( let c = 0 ; c < numCols ; c++ ) {
      requests.push(
        {"insertText":
          {"objectId": theTblId
          ,"cellLocation": {
             "rowIndex": r,
             "columnIndex": c
           }
          ,"text": inArr[r][c].toString()
          }
        }
      );
    }
  }

  console.log('number of updates: ', requests.length);
  if (requests.length > 0)   {
  var response = Slides.Presentations.batchUpdate(
      { 'requests': requests }, templatePresId );
  }

  console.log('End insertText' );
}
My result (since I didn't have access to your slide:
MySlide.jpg
Reply all
Reply to author
Forward
0 new messages