English 中文(简体)
Google 应用程序脚本以打开 URL
原标题:Google Apps Script to open a URL

是否有办法写入 Google 应用程序脚本, 所以当运行时, 第二个浏览器窗口打开到 www.google. com (或我选择的另一个网站)?

I am trying to come up with a work-around to my previous question here: Can I add a hyperlink inside a message box of a Google Apps spreadsheet

最佳回答

您可以建立一个小型 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);
  }

如果您想要显示 URL, 请像这样更改此行 :

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

EDIT : < a href=""https://docs.google.com/spretesheech/ccc?key=0 AnqSFd3iikE3dEHNZlA0NTFOWZIb25iNk5WA1VaalE" rel= "noreferr" >链接到一个演示电子表格 ,只读 ,因为太多的人一直在写不需要的东西(只需复制一份即可使用)。

EDIT:2014年12月11日,谷歌将UiApp淘汰,这一方法随时可能中断,需要更新,以使用 HTML 服务!

EDIT : below is an implementation using html service.

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");
}
问题回答

此函数打开一个 URL < strong >, 不需要额外的用户互动 。

/**
 * 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 服务的情况下工作, 例如脚本编辑器或自定义 GHS 公式 。

不需要像弹出回答中建议的那样创建自定义点击事件, 或者像被接受的回答中建议的那样显示 url 。

window.open(url) 1 在没有用户互动的情况下自动开放网页,只要弹出阻塞器被禁用(Stephen S >

Google Apps Script 将不会自动打开网页, 但是它可以用来显示带有链接的信息, 用户可以点击这些链接打开想要的网页, 或者甚至使用 Window 物件 , 以及 https:// developer. mozilla.org/ en- US/docs/Web/API/EventTargt/addEvent lister" rel="nofoln noreferr > adEventLearer () 以打开 URLU。

值得指出的是,UiApp现在已被贬低。从Class UiApp - Google Apps Script - Google Developers

Deprecated. The UI service was deprecated on December 11, 2014. To create user interfaces, use the HTML service instead.

HTML Service 链接页面的例子很简单,

码数( codgs)

// 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 );
}

显示两个超链接的定制索引. html版本

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

建构一个早期的示例, 我认为这样做有更干净的方法。 在您的工程中创建一个 < code> index. html 文件, 并使用上面的Stephen S 代码, 将其转换为 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 ..." );
}




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!