【问题标题】:Use button to open URL in new window with Google Appsscript使用按钮通过 Google Apps 脚本在新窗口中打开 URL
【发布时间】:2014-04-05 02:09:52
【问题描述】:

我正在为新的 Google 表格编写一个应用程序,以将另一个 API 与表格集成。我需要通过打开一个允许用户登录以授权我的应用程序的新窗口来授权用户。

到目前为止,我发现这样做的唯一方法是使用简单的超链接。有没有办法通过调用后端函数的按钮来做到这一点?当用户单击按钮时,我无法弄清楚任何可以在新窗口中打开链接的方法。

注意:我想这样做是为了与 UI 保持一致。我需要添加一个“授权”和一个“取消授权”按钮。取消授权按钮只是调用一个函数从用户帐户中删除访问令牌,但授权按钮需要打开一个新的 URL 以将用户发送到另一个站点进行登录。

【问题讨论】:

  • 当您在电子表格的上下文中谈论按钮时,您到底想到了什么?

标签: url hyperlink google-apps-script


【解决方案1】:

这是使用自动打开侧边栏的可能解决方案,如下所示:

以及下面的代码:

function onOpen() {
  var shUi = SpreadsheetApp.getUi();
  var app = UiApp.createApplication().setTitle('Custom functions');
  var panel = app.createVerticalPanel().add(app.createHTML('please select an option below').setStyleAttribute('padding','10px'));
  var grid = app.createGrid(1,2).setWidth('200');
  var dHandler = app.createServerHandler('removeAuth');
  var b1 = app.createButton("Authorize");
  var b2 = app.createButton("De-authorize",dHandler).setTitle('remove authorization');
  var link = app.createAnchor('XXXXX','http://www.google.com').setStyleAttributes({'zIndex':'1' , 'position':'fixed' , 'top':'45' , 'left':'20', 'color':'transparent' }).setTitle('proceed in a new tab');
  var G1 = app.createVerticalPanel().add(b1).add(link);
  grid.setWidget(0,0,G1).setWidget(0,1,b2);
  app.add(panel).add(grid)
  shUi.showSidebar(app);
}

function removeAuth(){
  // some code
}

【讨论】:

  • 这很好用,谢谢!我打算使用 HTML 服务构建我的 UI,而忽略了新的 UI 构建器。有趣的是,HTML 服务似乎没有任何方法可以做到这一点。
  • 实际上它可以通过 html 服务实现,但我更喜欢 uiApp,所以我选择了简单的方法 ;-)
  • HtmlService 中的重写不会是一个坏主意,因为 UiApp 已被弃用。那么我们不必为将“GAS Open URL”问题标记为与此问题重复而感到内疚:)
【解决方案2】:

在 Apps-script 中,您无法打开带有按钮单击事件的新选项卡,但这可以通过 锚点。因此,创建一个锚点并将 Href 设置为要在新选项卡中打开的链接。要获得 Button 感觉,您可以使用 background-image 属性为锚设置 StyleAttribute。

var anchor1 = app.createAnchor('Authorize','http://www.google.com')
                 .setStyleAttribute('backgroundImage','url('tree.png')');

【讨论】:

    猜你喜欢
    • 2017-11-14
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    相关资源
    最近更新 更多