Google Script - convert link to image in doc

by Curious   Last Updated March 13, 2018 03:03 AM

I've got a few Google Docs that contain some paragraphs of text and multiple Drive links in the following format:

https://drive.google.com/open?id=XXXX

Is there a script that I can use that will replace all the links in the Google Doc with the images that the links direct to?

Any assistance would be appreciated.

Thanks.



Answers 2


Here is a script for this purpose. It uses findText method to locate the links. Once a link is found, the element containing it is inspected: its text content is matched again against the link format, this time using string match method of plain JavaScript. The reason is that findText does not offer an easy way to retrieve the content that was actually matched...

Anyway, once the id is obtained from text.match, the image is acquired and appended to the element's parent. The logic is that the element we are looking at is probably Text, and Text cannot contain other elements. So the parent (probably Paragraph) is used instead.

The appendInlineImage method appends it at the end of paragraph. There is also insertInlineImage which allows for finer positioning, but it operates in terms of element index, not text offset. To my inderstanding, to insert an image directly where its URL is found, one would have to replace the original Text element by two, putting an image in between two pieces. This seemed too complicated to me, so I left it at appendInlineImage.

At the very end, all matching URLs are removed with replaceText method.

function replaceLinksByImages() {
  var searchPattern = "https://drive.google.com/open\\?id=\\w+";
  var body = DocumentApp.getActiveDocument().getBody();
  var found = body.findText(searchPattern); 
  while (found) {
    var elem = found.getElement();
    var text = elem.getText();
    var match = text.match(/https:\/\/drive.google.com\/open\?id=(\w+)/);
    if (match) {
      var id = match[1];
      var image = DriveApp.getFileById(id).getBlob();
      elem.getParent().appendInlineImage(image);
    }
    found = body.findText(searchPattern, found);
  }
  body.replaceText(searchPattern, "");
}
SFTP
SFTP
March 13, 2018 03:57 AM

Thanks @FTP, that did the trick! I was having some issues with an address containing a "-" character as well as resizing the images, so I ended up adding the following to your code:

function replaceLinksByImages(){
  var searchPattern = "https://drive.google.com/open\?id=\S+";
  var body = DocumentApp.getActiveDocument().getBody();
  var found = body.findText(searchPattern); 
  while (found) {
    var elem = found.getElement();
    var text = elem.getText();
    var match = text.match(/https://drive.google.com/open\?id=([\w-]+)/);
    if (match) {
      var id = match[1];
      var image = DriveApp.getFileById(id).getBlob();
      var appendImage = elem.getParent().appendInlineImage(image);
      var width = appendImage.getWidth()
      var height = appendImage.getHeight()
      var ratio = width/height
      var NewH = 640/ratio

appendImage.setWidth(640).setHeight(NewH) } found = body.findText(searchPattern, found); } body.replaceText(searchPattern, ""); }

Thanks again.

Curious
Curious
March 15, 2018 02:56 AM

Related Questions


Google Docs date capitalisation script

Updated December 23, 2017 17:03 PM


Alternative to "UrlFetchApp.fetch" at Google Docs?

Updated November 16, 2017 00:03 AM

Update footer date, don't clear the contents

Updated January 11, 2018 18:03 PM