如何在Google Apps脚本HTML Google站点小工具中打开JavaScript的URL链接

如何在Google Apps脚本HTML Google站点小工具中打开JavaScript的URL链接,javascript,google-apps-script,google-caja,Javascript,Google Apps Script,Google Caja,我已经在GoogleApps脚本中编写了一个HTML(而不是UI)小工具,嵌入到Google站点中。该小工具将显示一个下拉列表,其中包含URL/显示名称/值/文本对的选项 我想在小工具中添加一个按钮,打开与所选URL对应的新窗口。但是,基本上,当我执行时,我得到一个“对象不包含‘打开’方法”错误 window.open(url); 有办法解决这个问题吗?我可以(并且已经)创建带有锚定标记的小工具来成功打开其他窗口,但从javascript执行同样的操作似乎是不允许的 任何能够实现该功能的东西都

我已经在GoogleApps脚本中编写了一个HTML(而不是UI)小工具,嵌入到Google站点中。该小工具将显示一个下拉列表,其中包含URL/显示名称/值/文本对的选项

我想在小工具中添加一个按钮,打开与所选URL对应的新窗口。但是,基本上,当我执行时,我得到一个“对象不包含‘打开’方法”错误

window.open(url);
有办法解决这个问题吗?我可以(并且已经)创建带有锚定标记的小工具来成功打开其他窗口,但从javascript执行同样的操作似乎是不允许的

任何能够实现该功能的东西都可以。基于jQuery的解决方案将非常好


谢谢。

由于caja消毒,不可能做到这一点。下面的选项将不起作用

1) jquery-$(“#某些链接_id”)。单击() 2) javascript-window.open、window.href等

唯一的解决方法是创建target=“\u blank”链接以打开新窗口,但正如我所说的,不可能通过javascript/jquery单击这些链接,但我想这不适合您的问题

<a href="http://www.google.com" id="my_link" class="abc" target="_blank">My Link</a>

由于caja消毒,不可能做到这一点。下面的选项将不起作用

1) jquery-$(“#某些链接_id”)。单击() 2) javascript-window.open、window.href等

唯一的解决方法是创建target=“\u blank”链接以打开新窗口,但正如我所说的,不可能通过javascript/jquery单击这些链接,但我想这不适合您的问题

<a href="http://www.google.com" id="my_link" class="abc" target="_blank">My Link</a>

由于Caja的清理,所有javascript函数都不受支持,因此无法在应用程序脚本中打开新窗口

在应用程序脚本中显示弹出窗口的解决方法是使用覆盖窗口(从技术上讲,它不是弹出窗口,但看起来像弹出窗口),如应用程序脚本中支持的jQuery模式对话框

但是,iframe标记在应用程序脚本中不受支持,因此尝试在模式窗口中借助iframe标记打开url将不起作用

您可以通过以下链接在应用程序脚本中阅读有关这些限制的更多信息:

**


希望有帮助

由于Caja的清理,所有javascript函数都不受支持,因此无法在应用程序脚本中打开新窗口

在应用程序脚本中显示弹出窗口的解决方法是使用覆盖窗口(从技术上讲,它不是弹出窗口,但看起来像弹出窗口),如应用程序脚本中支持的jQuery模式对话框

但是,iframe标记在应用程序脚本中不受支持,因此尝试在模式窗口中借助iframe标记打开url将不起作用

您可以通过以下链接在应用程序脚本中阅读有关这些限制的更多信息:

**


希望有帮助

我可以使用表单来实现这一点。提交时,将表单的操作设置为所需的URL,由选择框中的选定项确定

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
    $('document').ready(function(){
       $('#yourForm').attr('onsubmit',null); //set onsubmit to Null for NATIVE Caja sandboxmode

       $('#yourForm').submit(function(){
          var val = $('#yourSelect').val();  //get value of select box
          $('#yourForm').attr('action',val); //set action of form
       });
    });
    </script>
    <div>
    <select id="yourSelect">
       <option value="https://www.google.com">Google</option>
       <option value="https://www.bing.com">Bing</option>
    </select>

    <form id="yourForm" method="get" target="_blank" action="">
    <input type="submit" value="Open Selected">
    </form>

    </div>

$('document').ready(函数(){
$('#yourForm').attr('onsubmit',null);//对于本机Caja sandboxmode,将onsubmit设置为null
$('#yourForm')。提交(函数(){
var val=$('#yourSelect').val();//获取选择框的值
$('#yourForm').attr('action',val);//设置表单的操作
});
});
谷歌
宾

我能够使用表单实现这一点。提交时,将表单的操作设置为所需的URL,由选择框中的选定项确定

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
    $('document').ready(function(){
       $('#yourForm').attr('onsubmit',null); //set onsubmit to Null for NATIVE Caja sandboxmode

       $('#yourForm').submit(function(){
          var val = $('#yourSelect').val();  //get value of select box
          $('#yourForm').attr('action',val); //set action of form
       });
    });
    </script>
    <div>
    <select id="yourSelect">
       <option value="https://www.google.com">Google</option>
       <option value="https://www.bing.com">Bing</option>
    </select>

    <form id="yourForm" method="get" target="_blank" action="">
    <input type="submit" value="Open Selected">
    </form>

    </div>

$('document').ready(函数(){
$('#yourForm').attr('onsubmit',null);//对于本机Caja sandboxmode,将onsubmit设置为null
$('#yourForm')。提交(函数(){
var val=$('#yourSelect').val();//获取选择框的值
$('#yourForm').attr('action',val);//设置表单的操作
});
});
谷歌
宾

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

(复制一份以查看脚本)

openNewWindow()已分配给按钮

代码G.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...');  
}
函数openNewWindow(){ var htmlString= '' + '' + '' + '窗口。打开('http://www.google.com")'; var htmlOutput=HtmlService .createHtmlOutput(htmlString) .setSandboxMode(HtmlService.SandboxMode.IFRAME) .设置高度(60); 电子表格应用程序 .getUi() .showModalDialog(htmlOutput,“打开新窗口…”); }
您可以通过使用HTMLService在对话框中运行window.open()JS直接打开链接

(复制一份以查看脚本)

openNewWindow()已分配给按钮

代码G.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...');  
}
函数openNewWindow(){ var htmlString= '' + '' + '' + '窗口。打开('http://www.google.com")'; var htmlOutput=HtmlService .createHtmlOutput(htmlString) .setSandboxMode(HtmlService.SandboxMode.IFRAME) .设置高度(60); 电子表格应用程序 .getUi() .showModalDialog(htmlOutput,“打开新窗口…”); }