Google Apps脚本:如何水平对齐inlineImage [英] Google apps script: How to horizontally align an inlineImage

查看:75
本文介绍了Google Apps脚本:如何水平对齐inlineImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,它是一个较大程序的一部分.我正在尝试将Google驱动器中的图片插入Google文档,并调整其大小并居中.到目前为止,我已经能够获得该程序来插入图像并调整其大小,但是我不知道如何居中inlineImage.我是使用Google Apps脚本的新手,并且我基本上一直在复制其他人的示例并对其进行修改.您的帮助将不胜感激.让我知道是否需要澄清.再次,我试图输入inlineImage(var inlineI).谢谢!

I have the following code, which is part of a larger program. I am trying to insert an image from my Google drive into a google doc and have it resized and centered. So far I am able to get the program to insert the image and resize it, but I do not know how to center an inlineImage. I am new to using google apps script and I have basically been copying other people's examples and modifying them. Your help would be appreciated. Let me know if I need to clarify. Again, I am trying to CENTER the inlineImage (var inlineI). Thanks!

var GDoc = DocumentApp.openByUrl("URL"); //I deleted my actual URL

function insertImageFromDrive(){
 var img = DriveApp.getFileById(myImageFileID).getBlob(); //I deleted my actual image ID
 var inlineI = GDoc.appendImage(img); //insert image

  //resizing the image
  var width = inlineI.getWidth();
  var newW = width;
  var height = inlineI.getHeight();
  var newH = height;
  var ratio = width/height;
  Logger.log('w='+width+'h='+height+' ratio='+ratio);
    if(width>320){ 
      //max width of image
      newW = 320; 
      newH = parseInt(newW/ratio);
    }
  inlineI.setWidth(newW).setHeight(newH); //resizes the image but also needs to center it
} 

推荐答案

您需要将包含图像的段落居中对齐.添加此代码即可:

You need to center-align the paragraph that contains your image. Add this code to do it:

var styles = {};
styles[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
inlineI.getParent().setAttributes(styles);

getParent()方法获取包含您的图像的容器元素(段落). setAttributes() 方法将自定义样式属性(在这种情况下为居中对齐)应用于元素.

getParent() method gets the container element (paragraph) containing your image. setAttributes() method applies custom style attributes (center alignment in this case) to the element.

这篇关于Google Apps脚本:如何水平对齐inlineImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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