如何在Google Apps脚本HTML Google Site Gadget中通过JavaScript打开URL链接 [英] How to open a URL link from JavaScript inside a Google Apps Script HTML Google Site Gadget

查看:93
本文介绍了如何在Google Apps脚本HTML Google Site Gadget中通过JavaScript打开URL链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google Apps Script中编写了一个HTML(不是用户界面)小工具,将其嵌入到Google Site中。该小工具显示包含URL /显示名称值/文本对的选项的下拉列表。



我想在小工具中放置一个按钮,以打开与所选URL对应的新窗口。但是,基本上,当我执行时,我得到一个Object不包含'open'方法的错误。 URL);

有没有办法解决这个问题?我可以(并且已经)创建了带有成功打开其他窗口的锚标签的小工具,但是从javascript执行相同的操作似乎不被允许。



任何可以实现功能的功能很好。一个基于jQuery的解决方案将会非常棒。



谢谢。

解决方案

您可以通过在使用HTMLService的对话框中运行window.open()JS来直接打开链接。
$ b

下面是演示表(带副本查看脚本)。

openNewWindow()分配给按钮。



Code.gs:

函数openNewWindow(){

var htmlString =
'< div>'+
'< input type =button value =关闭onclick =google.script.host.close()/>'+
'< / div>'+
'< script> window.open(http ://www.google.com)< /脚本>';

var htmlOutput = HtmlService
.createHtmlOutput(htmlString)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setHeight(60);

SpreadsheetApp
.getUi()
.showModalDialog(htmlOutput,'Opening New Window ...');
}


I have written an HTML (not UI) gadget in Google Apps Script to be embedded in a Google Site. The gadget presents a drop-down with options that contain URL/display name value/text pairs.

I want to put a button in the gadget that opens a new window corresponding to the selected URL. But, basically, I get an "Object does not contain an 'open' method" error when I execute

window.open(url);

Is there a way around this? I can (and have) created gadgets with anchor tags that successfully open other windows, but doing this same action from javascript appears to not be allowed.

Anything that accomplishes the functionality is fine. A jQuery-based solution would be great.

Thanks.

解决方案

You can open the link directly by running the window.open() JS in a dialog using the HTMLService.

Here's a demo sheet (take a copy to see the script).

openNewWindow() is assigned to the button.

Code.gs:

function openNewWindow() {

  var htmlString = 
    '<div>' + 
      '<input type="button" value="Close" onclick="google.script.host.close()" />' + 
    '</div>' +
    '<script>window.open("http://www.google.com")</script>';

  var htmlOutput = HtmlService
    .createHtmlOutput(htmlString)
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setHeight(60);

  SpreadsheetApp
    .getUi()
    .showModalDialog(htmlOutput, 'Opening New Window...');  
}

这篇关于如何在Google Apps脚本HTML Google Site Gadget中通过JavaScript打开URL链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆