谷歌电子表格-从按钮打开外部URL [英] google spreadsheet - open external URL from button

查看:161
本文介绍了谷歌电子表格-从按钮打开外部URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Google电子表格脚本非常了解。

I have very basic knowledge of google spreadsheet scripts.

现在,我正在寻找一些通过单击按钮来打开外部URL的功能。这是我想通过单击按钮从工作表中打开的链接: http://h22235.www2.hp.com/hpinfo/globalcitizenship/environment/productdata/Countries/us/ii_q4135b_us_eng_v18.pdf

Right now I am looking for some function for open an external URL by clicking on a button. this is the link what i want to open from a sheet by clicking on a button: http://h22235.www2.hp.com/hpinfo/globalcitizenship/environment/productdata/Countries/us/ii_q4135b_us_eng_v18.pdf

感谢您的帮助

推荐答案

这听起来很简单,但实际上是一个棘手的要求。

This sounds simple but it actually quite a tricky requirement.

Google Apps脚本不会自动打开网页。您可以创建一个HYPERLINK,您的用户可以单击它,但是您似乎无法自动启动它。如果您为此操作记录一个宏,则它仅具有Cell的activate()函数,但没有打开新浏览器的行。

Google Apps Script will not automatically open web pages. You can create a HYPERLINK and your user could click on it but you don't seem to be able to initiate it automatically. If you record a macro for this operation it just has the activate() function for the Cell but doesn't have the line that opens the new browser.

过时的建议解决方案是打开一个自定义对话框,其中包含链接或按钮,用户可以单击该对话框来打开新选项卡,并且可以使用HtmlService来完成。

The obsolete suggested solutions are to open a custom dialog that has a link or a button in it that the user can click on to open the new tab and this can be done using the HtmlService.

这个问题具有创建带有链接的对话框的代码,并且此代码运行良好。

The third answer to this question has code to create a dialog with a link in it and this code works well.

var htmlOutput = HtmlService
    .createHtmlOutput('Go to <a href="https://www.google.ca/">this site</a> for help!')
    .setWidth(250) //optional
    .setHeight(50); //optional
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Help Dialog Title');

请求的内容与要求不完全相同,因为它要求用户单击链接并且他们已经单击了

It's not exactly what was requested as it requires the user to click on the link and they already clicked on a button or something else to get to the dialog.

对于您来说,将一些javascript写入对话框以自动调用window.open()似乎很简单。建议的内容此处

It would seem simple for you to write some javascript into the dialog to automatically call window.open() and this is what is proposed here.

function openTab() {
  var selection = SpreadsheetApp.getActiveSheet().getActiveCell().getValue();

  var html = "<script>window.open('" + selection + "');google.script.host.close();</script>";
  var userInterface = HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Tab');
}

此代码创建一个带有脚本的对话框,该脚本打开从中读取的链接工作表,然后调用 google.script.host.close()摆脱对话框。
不幸的是,这似乎不起作用,花了一段时间才弄清楚原因。

This code creates a dialog with a script that opens the link that it read from the Sheet and then calls google.script.host.close() to get rid of the dialog. Unfortunately, this doesn't appear to work and it took a while to figure out why.

answer 是大多数流行的浏览器中的弹出窗口阻止程序仅在以下情况下允许打开新窗口:它是通过直接用户操作(例如单击)运行的代码而打开的。

The answer is that popup blockers in most popular browsers will only allow a new window to be opened if it is opened as a result of code running from a direct user action such as a click.

您可以通过关闭Chrome中的弹出窗口阻止功能来验证这一点,并且代码可以正常工作。

You can verify this by turning off Popup blocking in chrome and the code works fine.

您不太可能说服您的用户关闭弹出窗口阻止功能,因此我认为您无法通过一种方法来打开新的浏览器标签。我认为您能做的最好的事情就是拥有一个按钮,该按钮可打开一个带有按钮的对话框或一个链接,该对话框将打开新的浏览器标签。

It is highly unlikely that you can convince your users to turn off popup blocking so I don't think there is a way for you have a button that opens a new browser tab. The best I think you can do is to have a button that opens a dialog with a button or a link that will open the new browser tab.

这篇关于谷歌电子表格-从按钮打开外部URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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