2

I programmed one that calls a Google Doc and in said Doc there is a variable called #Name# this is replaced by the name that is stored in a SpreadSheet.

My question is that to that Doc I want to add a variable called # Photo # and it is replaced by the photo stored in a Drive URL and this URL is in a SpreadSheet

How can I call that URL that is in Spreadsheet and be able to replace the field #photo#

With the name it does well with the function

body.replaceText ('#name#', name);

but with the image I don't know what to call it

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    https://stackoverflow.com/questions/26597379/update-replace-inline-image-on-google-document – Mario Zamora Jun 26 '20 at 19:45
  • For example, as other method, how about using Slides API? When the ReplaceAllShapesWithImageRequest of the batchUpdate method in Slides API is used, the image URL can be directly used as the replace image. [Ref](https://developers.google.com/slides/reference/rest/v1/presentations/request#replaceallshapeswithimagerequest) – Tanaike Jun 27 '20 at 00:53
  • Is the placeholder `#PHOTO#` part of a paragraph or another container element? – Iamblichus Jun 29 '20 at 09:30
  • @lamblichus It is a variable that will be replaced by the photo, this is found in a paragraph, I also have a tag called # Name # which is replaced by the name of the person stored in the spreadsheet, that I call it with the function: `var name = sheet.getRange (i, 2) .getValue (); body.replaceText ('# name #', name); // update the temp doc ` – Robert Rodriguez Jun 30 '20 at 14:10
  • Is there other content in the paragraph? I'm thinking you could have a paragraph just for this (`#PHOTO#`). Otherwise, I don't think what you want to do is possible (either the whole paragraph will be replaced by the image, or the image will be appended just before or just after the paragraph, but not at the exact same location you want it. What do you think of this? – Iamblichus Jul 01 '20 at 13:55
  • @Iamblichus This is intended for a birthday system, I have a paragraph with an image of congratulations, in the paragraph there are the variables #name# that is replaced by the name of the birthday boy, that's how it works, but in addition to that I want to add the variable #photo# that would be replaced by the birthday boy's photo. fijate en la siguiente imagen: https://drive.google.com/file/d/1K5GNWKJPM4nwaGL_PVheJpxSwWb2LMFO/view?usp=sharing this is a screenshot of the template that i have in a doc and i want to replace the variable #photo# which is stored in a url in a spreadsheet – Robert Rodriguez Jul 01 '20 at 14:40
  • Thank you for your response. Could you please provide a copy of the Google Doc you're working on? (of course, please remove any sensitive information). – Iamblichus Jul 01 '20 at 14:50
  • This is a copy of the complete project, I inform you how it works: shorturl.at/frMY0 1.- fill out the Google form with the data 2.- Fill the Spreadsheet with the form 3.- The Doc is the template that is sent as HTML format to the birthday boy's email. 4.- in the template only the variable # Name # works, since # Photo # is the one that I cannot insert in the Doc, the idea is that in the form there is the option to upload the image and store the URL in the spreadsheet, and that url is called from Apps Script as with the name. @lamblichus – Robert Rodriguez Jul 01 '20 at 17:41
  • Hi, I posted an answer regarding this. Could you please confirm whether that solves your issue? – Iamblichus Jul 03 '20 at 07:00

1 Answers1

1

Since #Photo# is a paragraph of its own, you can do the following:

  • Using the Document Body, find the text #Photo# via findText(searchPattern) and get the Element that corresponds to this text via getElement().
  • Once you have retrieved this element, you can remove the text (#Photo#), since the element itself will exist even if the text is an empty string. You can use setText("") for that.
  • Get the Blob from the image URL via UrlFetchApp.fetch(url) and getBlob().
  • Get the retrieved element's parent, which is the paragraph #Photo# was part of, and use insertInlineImage to insert the retrieved image to the beginning of this paragraph.

Code sample:

function replaceTextWithImage() {
  var body = DocumentApp.getActiveDocument().getBody(); // Get current document's body
  var element = body.findText("#Photo#").getElement(); // Get element of #Photo#
  element.asText().setText(""); // Remove #Photo# placeholder
  var url = "YOUR_IMAGE_URL"; // Change this accordingly
  var blob = UrlFetchApp.fetch(url).getBlob(); // Get blob from image URL
  var image = element.getParent().asParagraph().insertInlineImage(0, blob); // Insert image
}

Notes:

  • I'm assuming that your question was only about inserting the image to the Document, not about the previous steps of populating the spreadsheet, retrieving the image URL, etc.
  • This sample refers to a script that is bound to the Google Doc (hence, it's using getActiveDocument() to retrieve the Doc). If that's not the case, you should retrieve the document via openById(id) or openByUrl(url).
Iamblichus
  • 18,540
  • 2
  • 11
  • 27