0

I have an image saved in my Google drive called "logo.png".

I want to add the image to a Google document so that it's in the top left corner and so that it does not distort the text around it. (In other words, I want the text to "wrap" around the image).

How do I add an image to a Google Doc using Google Script so that surrounding text wraps around the image?

My script so far:

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();

}

Desired Result:

enter image description here

I saw here that an image can be added in various ways, but neither approach has worked for me AND I do not see how I can control a wrapping attribute.

Update

I tried using the following code (with fake ID shown in URL), but it just created a blank document:

  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)
theforestecologist
  • 4,667
  • 5
  • 54
  • 91
  • The way I would approach this would be to start by determining the layout as seen by Apps Script in your created-with-UI demo document. Once you know how the UI does it, you can usually do the same programmatically. I say "usually" because the Document Service has historically been rather lackluster. With the arrival of the Google Docs API it may be getting some love, however. – tehhowch Apr 09 '19 at 14:38
  • @tehhowch I'm so new to coding in Google script, that I unfortunately have no idea what you're talking about :/. Where do I find the "created-with-UI demo document"? (and what's the "Document Servce"?) – theforestecologist Apr 09 '19 at 14:43
  • Where's the source of your image? Do that in Google Docs, with the UI, e.g. https://docs.google.com. Then open that document by ID (or in a bound script), and use the Document Service methods to find out its structure. (The Document Service is `DocumentApp` and its related functionality: https://developers.google.com/apps-script/reference/document/ ) – tehhowch Apr 09 '19 at 14:44
  • @tehhowch My image is in my google drive. – theforestecologist Apr 09 '19 at 14:51
  • That's the location of your image, sure. But *how did you create the screenshot in your post*? Create what you want, manually, in Google Docs, with the UI. Then you can reverse engineer the structure your need, and by consulting the Document Service methods, determine how you can go about programmatically creating it. – tehhowch Apr 09 '19 at 14:59
  • @tehhowch I created the image in microsoft word real quick to demonstrate what I'd like as an end result. I have *no idea* how to successfully add an image to a Google doc usign script, and all methods I have tried have produced blank documents... – theforestecologist Apr 09 '19 at 15:02
  • Are you suggesting, though, that I just manually create an example document with the image and text where/how I want it and then somehow applying a `documentApp` function to it so I can see my example documents underlying code?? – theforestecologist Apr 09 '19 at 15:07
  • 1
    Possible duplicate of [Manipulate PositionedImage and wrap text around image in a Google Document](https://stackoverflow.com/questions/20274302/manipulate-positionedimage-and-wrap-text-around-image-in-a-google-document) – tehhowch Apr 09 '19 at 15:07

0 Answers0