首页 > 解决方案 > 如何通过谷歌脚本将图像添加到谷歌文档并进行文本换行

问题描述

我有一个名为“logo.png”的图像保存在我的 Google 驱动器中。

我想将图像添加到 Google 文档中,使其位于左上角,并且不会扭曲其周围的文本。(换句话说,我希望文本“环绕”图像)。

如何使用 Google Script 将图像添加到 Google Doc,以便周围的文本环绕图像?

到目前为止我的脚本:

function myFunction(e) {

  var t1 = 'Center for Success';
  var t2 = 'Foundational Hall';
  var t3 = 'Instruction Sheet for Testing Requirements';

  var boldRight ={};
    boldRight[DocumentApp.Attribute.BOLD]=true;
    boldRight[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]=DocumentApp.HorizontalAlignment.RIGHT;

  var boldCenterUnderline ={};
    boldCenterUnderline[DocumentApp.Attribute.BOLD]=true;
    boldCenterUnderline[DocumentApp.Attribute.UNDERLINE]=true;
    boldCenterUnderline[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]=DocumentApp.HorizontalAlignment.CENTER;

  var filename = 'fileTest';

  var doc = DocumentApp.create(filename);
  var body = doc.getBody();

  body.appendParagraph(t1).setAttributes(boldRight);
  body.appendParagraph(t2).setAttributes(boldRight); 
  body.appendParagraph(space); 
  body.appendParagraph(t3).setAttributes(boldCenterUnderline);

  doc.saveAndClose();

}

期望的结果

在此处输入图像描述

我在这里看到可以通过多种方式添加图像,但是这两种方法都不适合我,而且我看不到如何控制包装属性。

更新

我尝试使用以下代码(在 URL 中显示了假 ID),但它只是创建了一个空白文档:

  var image  = "https://drive.google.com/open?id=2PJGK5C64HLKKoQIv52jGhUjjdiXU34Mp";
  var fileID = image.match(/[\w\_\-]{25,}/).toString();
  var blob   = DriveApp.getFileById(fileID).getBlob();
  body.appendImage(blob)

标签: imagegoogle-apps-scriptgoogle-docsword-wrap

解决方案


推荐阅读