用于打开网址的Google Apps脚本

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

我想在这里找到解决上一个问题的方法:Can I add a hyperlink inside a message box of a Google Apps spreadsheet

回答(6)

2 years ago

您可以构建一个小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只读,因为有太多人继续写不需要的东西......制作副本使用 .

编辑:!! UiApp于2014年12月11日被Google折旧,此方法可能随时中断,需要更新才能使用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");
}

2 years ago

此功能打开URL without requiring additional user interaction .

/**
 * 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表格公式 .

2 years ago

Google Apps脚本不会自动打开网页,但可以用来显示带有链接的消息,用户可以点击它们打开所需网页的按钮,甚至可以使用Window objectaddEventListener()等方法打开网址 .

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

不推荐 . UI服务已于2014年12月11日弃用 . 要创建用户界面,请改用HTML服务 .

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

Code.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>

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

2 years ago

Build 一个早期的例子,我认为有一种更清洁的方法 . 在项目中创建 index.html 文件并使用上面的Stephen代码,只需将其转换为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 ..." );
}

2 years ago

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'
   )
}

2 years ago

这两者之间的唯一区别是:

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

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

是在第一种情况下,链接将在对话框中显示"open" . 到目前为止,我发现没有办法自动打开链接...(见https://developers.google.com/apps-script/class_anchor) .

自动打开文档的唯一方法似乎是:

var doc = DocumentApp.openById(foundFile.getId());

但后来我不确定应该用doc做什么!即没有doc.show()......