Home > OS >  How to start a Google Slides presentation after creating it using a script in Google Sheets?
How to start a Google Slides presentation after creating it using a script in Google Sheets?

Time:04-27

The user accesses a Spreadsheet and requests the application create a Proposal (which is a Google Slides app). The Spreadsheet performs this action using Google scripts. The user would like to immediately open and review the Proposal without closing out of the original Spreadsheet. I know I can ask the question of whether they would like to open the Proposal using a ui.alert. I kow how to open the new Proposal but I don't know how to give it focus on the screen or tranfser control to it.

Can anyone help?

CodePudding user response:

To start a presentation, you have to make your script to open the presentaion in presentation mode. To do this first you have to build the URL:

  const presentation = SlidesApp.getActivePresentation();
  const url = `https://docs.google.com/presentation/d/${presentation.getId()}/present`;

Then you might open a dialog or sidebar including client side code to open the URL as is done with any URL. For this you might use the function openURL from the Stephen's answer to Google Apps Script to open a URL:

/**
 * Open a URL in a new tab.
 */
function openUrl( url ){
  var html = HtmlService.createHtmlOutput('<html><script>'
   'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
   'var a = document.createElement("a"); a.href="' url '"; a.target="_blank";'
   'if(document.createEvent){'
   '  var event=document.createEvent("MouseEvents");'
   '  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'                          
   '  event.initEvent("click",true,true); a.dispatchEvent(event);'
   '}else{ a.click() }'
   'close();'
   '</script>'
  // Offer URL as clickable link in case above code fails.
   '<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="' url '" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
   '<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
   '</html>')
  .setWidth( 90 ).setHeight( 1 );
  SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );
}
  • Related