Reputation: 41
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
Upvotes: 3
Views: 7430
Reputation: 19309
Since #Photo#
is a paragraph of its own, you can do the following:
#Photo#
via findText(searchPattern) and get the Element that corresponds to this text via getElement().#Photo#
), since the element itself will exist even if the text is an empty string. You can use setText("") for that.#Photo#
was part of, and use insertInlineImage to insert the retrieved image to the beginning of this paragraph.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
}
Upvotes: 1