使用 Google 表格中的脚本创建 Google 幻灯片演示文稿后如何启动它?

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

用户访问电子表格并请求应用程序创建提案(这是一个 Google 幻灯片应用程序)。电子表格使用 Google 脚本执行此操作。用户希望在不关闭原始电子表格的情况下立即打开并查看提案。我知道我可以问他们是否愿意使用 ui.alert 打开提案的问题。我知道如何打开新提案,但我不知道如何将焦点放在屏幕上或对其进行传输控制。

有人可以帮忙吗?

要开始演示,您必须使脚本以演示模式打开演示。为此,您首先必须构建 URL:

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

然后您可以打开一个对话框或侧边栏,其中包括客户端代码以打开 URL,就像对任何 URL 所做的那样。为此,您可以使用 Stephen's answer to Google Apps Script to open a URL:

中的函数 openURL
/**
 * 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 ..." );
}