简体   繁体   English

如何集中元素谷歌应用程序脚本UiApp

[英]How to center elements google apps script UiApp

I am creating a simple app with an image. 我正在创建一个带图像的简单应用程序。 I create the app like so: 我这样创建应用程序:

function createApplication(){
  var app = UiApp.createApplication().setTitle("My Simple App");
  mainImage = app.createImage("URL TO IMAGE");
  app.add(mainImage);
  return(app);
}

This puts the image to the top left of the screen. 这会将图像置于屏幕的左上角。 Can someone show me how to center the image ? 有人能告诉我如何使图像居中吗?

Many thanks 非常感谢

You can use setStyleAttribute() and refer to some CSS documentation 您可以使用setStyleAttribute()并参考一些CSS文档

try it like this for example (I'm really not an expert in CSS so don't blame me if it is inelegant ;-) 试试这样的例子(我真的不是CSS的专家所以如果它不优雅,不要怪我;-)

function testImage(){
  var app = UiApp.createApplication().setTitle("My Simple App").setHeight('700').setWidth('900');
  mainImage = app.createImage("https://dl.dropbox.com/u/211279/Time-change-clock_animated_TR80.gif")
  .setStyleAttribute('position','relative').setStyleAttribute('left','50%').setStyleAttribute('top','50%')
  app.add(mainImage);
  ss=SpreadsheetApp.getActive()
  ss.show(app)
}

Note : You can also position your image using PX(pixels) instead of % and, as you noticed, I tested this in a spreadsheet UI. 注意: 您也可以使用PX(像素)而不是%来定位图像,正如您所注意到的,我在电子表格UI中对此进行了测试。


EDIT : and here is a version using pixels and Bryan's suggestion for CSS styling, more compact and with a syntax that approaches the original CSS method (thx again) : 编辑:这是一个使用像素的版本和Bryan对CSS样式的建议,更紧凑,并且语法接近原始CSS方法(再次):

var _imgCSS = {'position':'relative', 'left':'200PX', 'top':'200PX'}

function testImage(){
      var app = UiApp.createApplication().setTitle("My Simple App").setHeight('500').setWidth('500');
      var mainImage = app.createImage("https://dl.dropbox.com/u/211279/Time-change-clock_animated_TR80.gif")
      .setStyleAttributes(_imgCSS)
      app.add(mainImage);
      ss=SpreadsheetApp.getActive()
      ss.show(app)
    }

I've just found another nice way to do it. 我刚刚发现了另一种很好的方法。

Panels by default shrink to perfectly fit its content. 默认情况下,面板会缩小以完全适合其内容。 It's good, because all widgets are very close to each other. 这很好,因为所有小部件彼此非常接近。 So what we do here, we add a fullPanel - that fills whole browser area. 所以我们在这里做的是,我们添加一个fullPanel - 填充整个浏览器区域。 Then add the mainPanel of our application, and inside we place any widgets we need. 然后添加我们应用程序的mainPanel,在里面我们放置我们需要的任何小部件。

+--fullPanel-----------------+
|                            |
|                            |
|       +-mainPanel--+       |
|       |            |       |
|       |            |       |
|       +------------+       |
|                            |
|                            |
+----------------------------+

Result? 结果?

  • all widgets inside mainPanel are perfectly aligned to each other mainPanel内的所有小部件都完全相互对齐
  • mainPanel is always in the center mainPanel总是在中心

There's the code: 有代码:

function createApplication(){
  var app = UiApp.createApplication().setTitle("My Simple App");

  /* Main application Panel */
  var mainPanel = myApp.createVerticalPanel();

  /* Add any widgets to this Panel */
  mainImage = app.createImage("URL TO IMAGE");
  mainPanel.add(mainImage);

  /* Create a panel that will fill all area of browser */
  var fullPanel = myApp.createVerticalPanel();
  fullPanel.setHeight('100%');
  fullPanel.setWidth('100%');
  fullPanel.setHorizontalAlignment(UiApp.HorizontalAlignment.CENTER);
  fullPanel.setVerticalAlignment(UiApp.VerticalAlignment.MIDDLE);
  fullPanel.add(mainPanel);

  app.add(fullPanel);
  return(app);
}

You can use a Google Drive link to an image. 您可以使用Google云端硬盘链接查看图片。 Make sure the image is public on the web, and get the document ID. 确保图像在Web上公开,并获取文档ID。 If the link to share is like: https://docs.google.com/file/d/[docID]/edit?usp=sharing 如果要分享的链接如下:https://docs.google.com/file/d/ [docid] / edit?usp =sharing

The direct link to the image is https://googledrive.com/host/[docID]/ 图像的直接链接是https://googledrive.com/host/[docID]/

Alternatively, if the folder is public, with ID folderID, you can use https://googledrive.com/host/[folderID]/myimage.gif 或者,如果文件夹是公共的,具有ID folderID,则可以使用https://googledrive.com/host/[folderID]/myimage.gif

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

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