繁体   English   中英

使用按钮可通过Google Apps脚本在新窗口中打开URL

[英]Use button to open URL in new window with Google Appsscript

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

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

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

这是使用自动打开侧边栏的可能解决方案,它看起来像这样:

在此处输入图片说明

和下面的代码:

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
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM