【问题标题】:Google Apps Script to open a URL用于打开 URL 的 Google Apps 脚本
【发布时间】:2012-05-31 11:11:24
【问题描述】:

有没有办法编写一个谷歌应用程序脚本,以便在运行时打开第二个浏览器窗口到 www.google.com(或我选择的其他网站)?

我正在尝试解决我之前的问题: Can I add a hyperlink inside a message box of a Google Apps spreadsheet

【问题讨论】:

    标签: html redirect google-apps-script hyperlink


    【解决方案1】:

    此函数无需额外的用户交互即可打开一个 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 ..." );
    }
    

    此方法通过创建一个临时对话框来工作,因此它不适用于无法访问 UI 服务的上下文,例如脚本编辑器或自定义 G 表格公式。

    【讨论】:

    • @MaxMakhrov 确实如此!我刚刚用修复更新了代码。似乎程序化点击在 Firefox 中一直存在问题,但现在脚本可靠地显示 Firefox 中的“无法打开”回退并为用户提供工作链接。
    • 斯蒂芬,我不遵循上面的脚本。当我运行它时,它会进入一个错误页面,提示找不到 URL。您能否发布一个可以打开 google.com 之类网站的代码示例?
    • @PY_ 你可以将任何你想要的 URL 传递给函数。要打开 google.com,请包含上述脚本,然后调用 openUrl( 'https://google.com' );
    • @StephenM.Harris - 当您将 SpreadsheetApp 替换为 DocumentApp 时,它会打开两个新窗口(相同的 url)。关于如何修复它的任何建议?我正在尝试限制我的 Google Doc 插件中的范围。
    • @PY_,我也遇到了同样的问题,你需要在 url 的开头加上http://,然后就可以了
    【解决方案2】:

    您可以构建一个小型 UI 来完成这样的工作:

    function test(){
    showURL("http://www.google.com")
    }
    //
    function showURL(href){
      var app = UiApp.createApplication().setHeight(50).setWidth(200);
      app.setTitle("Show URL");
      var link = app.createAnchor('open ', href).setId("link");
      app.add(link);  
      var doc = SpreadsheetApp.getActive();
      doc.show(app);
      }
    

    如果您想“显示”网址,只需像这样更改此行:

      var link = app.createAnchor(href, href).setId("link");
    

    编辑:link to a demo spreadsheet in 只读,因为太多人一直在上面写不需要的东西(只需复制一份以代替使用)。

    编辑:Google 已于 2014 年 12 月 11 日弃用 UiApp,此方法随时可能中断,需要更新以改用 HTML 服务!

    编辑: 下面是一个使用 html 服务的实现。

    function testNew(){
      showAnchor('Stackoverflow','http://stackoverflow.com/questions/tagged/google-apps-script');
    }
    
    function showAnchor(name,url) {
      var html = '<html><body><a href="'+url+'" target="blank" onclick="google.script.host.close()">'+name+'</a></body></html>';
      var ui = HtmlService.createHtmlOutput(html)
      SpreadsheetApp.getUi().showModelessDialog(ui,"demo");
    }
    

    【讨论】:

    • 查看编辑,我添加了一个示例电子表格向您展示应该使用它。
    • 抱歉回复慢。示例电子表格处于仅查看模式,因此它不允许我进入脚本编辑器。但是,由于某种原因,现在上面的脚本可以工作了。感谢您的帮助!
    • 信息:另见这篇文章:stackoverflow.com/questions/18151564/…
    • 对于那些在 2018 年或以后查看此内容的人,上述解决方案不再有效。请参阅斯蒂芬 M 哈里斯的回答。自撰写此评论之日起生效。
    • 对不起,这不是真的,新版本作为 EDIT 工作,它使用 HTMLService。
    【解决方案3】:

    确实不需要按照赏金答案中的建议创建自定义点击事件,也不需要按照已接受答案中的建议显示网址。

    window.open(url)1 会自动打开网页而无需用户交互,前提是禁用弹出窗口拦截器(就像 Stephen 的 answer 一样)

    openUrl.html

    <!DOCTYPE html>
    <html>
      <head>
       <base target="_blank">
        <script>
         var url1 ='https://stackoverflow.com/a/54675103';
         var winRef = window.open(url1);
         winRef ? google.script.host.close() : window.alert('Allow popup to redirect you to '+url1) ;
         window.onload=function(){document.getElementById('url').href = url1;}
        </script>
      </head>
      <body>
        Kindly allow pop ups</br>
        Or <a id='url'>Click here </a>to continue!!!
      </body>
    </html>
    
    

    code.gs:

    function modalUrl(){
      SpreadsheetApp.getUi()
       .showModalDialog(
         HtmlService.createHtmlOutputFromFile('openUrl').setHeight(50),
         'Opening StackOverflow'
       )
    }    
    

    【讨论】:

      【解决方案4】:

      Google Apps 脚本不会自动打开网页,但它可用于显示带有链接、按钮的消息,用户可以点击这些按钮打开所需的网页,甚至可以使用 Window object 和类似的方法addEventListener() 打开网址。

      值得注意的是,UiApp 现在已被弃用。来自Class UiApp - Google Apps Script - Google Developers

      已弃用。 UI 服务是deprecated on December 11, 2014。到 创建用户界面,请改用HTML service

      HTML 服务链接页面中的示例非常简单,

      代码.gs

      // Use this code for Google Docs, Forms, or new Sheets.
      function onOpen() {
        SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
            .createMenu('Dialog')
            .addItem('Open', 'openDialog')
            .addToUi();
      }
      
      function openDialog() {
        var html = HtmlService.createHtmlOutputFromFile('index')
            .setSandboxMode(HtmlService.SandboxMode.IFRAME);
        SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
            .showModalDialog(html, 'Dialog title');
      }
      

      index.html 的自定义版本,用于显示两个超链接

      <a href='http://stackoverflow.com' target='_blank'>Stack Overflow</a>
      <br/>
      <a href='http://meta.stackoverflow.com/' target='_blank'>Meta Stack Overflow</a>
      

      【讨论】:

      • window.open(url, "_blank") 可用于打开网页。
      【解决方案5】:

      基于前面的示例,我认为有一种更简洁的方法。在您的项目中创建一个index.html 文件并使用上面的斯蒂芬代码,只需将其转换为 HTML 文档。

      <!DOCTYPE html>
      <html>
        <base target="_top">
        <script>
          function onSuccess(url) {
            var a = document.createElement("a"); 
            a.href = url;
            a.target = "_blank";
            window.close = function () {
              window.setTimeout(function() {
                google.script.host.close();
              }, 9);
            };
            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();
          }
      
          function onFailure(url) {
            var div = document.getElementById('failureContent');
            var link = '<a href="' + url + '" target="_blank">Process</a>';
            div.innerHtml = "Failure to open automatically: " + link;
          }
      
          google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).getUrl();
        </script>
        <body>
          <div id="failureContent"></div>
        </body>
        <script>
          google.script.host.setHeight(40);
          google.script.host.setWidth(410);
        </script>
      </html>
      

      然后,在您的 Code.gs 脚本中,您可以有类似以下的内容,

      function getUrl() {
        return 'http://whatever.com';
      }
      
      function openUrl() {
        var html = HtmlService.createHtmlOutputFromFile("index");
        html.setWidth(90).setHeight(1);
        var ui = SpreadsheetApp.getUi().showModalDialog(html, "Opening ..." );
      }
      

      【讨论】:

      • 我试过这个例子。几天前它工作得很好。今天我运行它时,它只打开对话框,但不会在新选项卡中打开外部 url。我的浏览器中是否有某些设置出错?
      • @shanti 你有没有想过这个问题?似乎类似的东西突然停止了对我的工作:stackoverflow.com/questions/10744760/…
      • @Ryan 是的,这与我的 Chrome 设置或某些 Chrome 扩展程序有关。我不得不将 Chrome 设置恢复到原来的默认值,并且它起作用了。我也在此链接中设置了答案:stackoverflow.com/questions/65265727/…
      • @shanti 谢谢。我也想出了一个解决方案,我在我的评论中提到过。
      【解决方案6】:

      我喜欢@Stephen M. Harris 的回答,直到最近才对我有用。我不知道为什么stopped working

      2021-09-01 现在对我有用的方法:

      function openUrl( url ){
        Logger.log('openUrl. url: ' + url);
        const html = `<html>
      <a id='url' href="${url}">Click here</a>
        <script>
           var winRef = window.open("${url}");
           winRef ? google.script.host.close() : window.alert('Configure browser to allow popup to redirect you to ${url}') ;
           </script>
      </html>`; 
        Logger.log('openUrl. html: ' + html);
        var htmlOutput = HtmlService.createHtmlOutput(html).setWidth( 250 ).setHeight( 300 );
        Logger.log('openUrl. htmlOutput: ' + htmlOutput);
        SpreadsheetApp.getUi().showModalDialog( htmlOutput, `openUrl function in generic.gs is now opening a URL...` ); // https://developers.google.com/apps-script/reference/base/ui#showModalDialog(Object,String)  Requires authorization with this scope: https://www.googleapis.com/auth/script.container.ui  See https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes
      }
      

      https://developers.google.com/apps-script/reference/base/ui#showModalDialog(Object,String) 需要此范围的授权:https://www.googleapis.com/auth/script.container.ui 请参阅https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes

      【讨论】:

      • 看起来类似于my answer
      • 如何修改以将单元格 A2 中的数据包含到 URL 中,例如在 A2 中运行谷歌搜索数据
      • 它对我有用。这种解决方法非常好而且非常容易。另一个解决方案对我不起作用。此外,您必须创建一个新的 HTML 文件。如果您创建一个 HTML 文件,我会遇到“找不到函数错误”
      猜你喜欢
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      相关资源
      最近更新 更多