【问题标题】:Google apps script: How to horizontally align an inlineImage谷歌应用脚​​本:如何水平对齐 inlineImage
【发布时间】:2014-12-02 14:39:26
【问题描述】:

我有以下代码,它是一个更大程序的一部分。我正在尝试将我的谷歌驱动器中的图像插入谷歌文档并调整其大小和居中。到目前为止,我能够让程序插入图像并调整其大小,但我不知道如何将 inlineImage 居中。我是使用谷歌应用程序脚本的新手,我基本上一直在复制其他人的示例并对其进行修改。您的帮助将不胜感激。让我知道是否需要澄清。同样,我试图将 inlineImage (var inlineI) 居中。谢谢!

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
} 

【问题讨论】:

    标签: javascript image google-apps-script center


    【解决方案1】:

    您需要将包含图片的段落居中对齐。添加此代码以执行此操作:

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

    getParent() 方法获取包含您的图像的容器元素(段落)。 setAttributes() 方法将custom style attributes(在本例中为中心对齐)应用于元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多