I am trying to do something very similar to another thread (Convert all links to images in a Google Document with actual images) but with non google stored URL links.

DriveApp only works for google, so how would I modify those lines for another source. I have images on GoCanvas e.g. https://www.gocanvas.com/values/6456586878

Changing the search pattern/match variables is easy enough, but having troubles reading the image as it always seems to copy it as a string.

  function replaceLinksByImages() {
  var searchPattern = "https://www.gocanvas.com/values/";
  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:\/\/www.gocanvas.com\/values\/([\w-]+)/);
    if (match) {
      //This part needs changing
      //      var id = match[1];
      //      var image = DriveApp.getFileById(id).getBlob();
      //      elem.getParent().appendInlineImage(image);
  var bytes = UrlFetchApp.fetch(match[0]).getBlob().getAs(MimeType.JPEG);

    found = body.findText(searchPattern, found);
  body.replaceText(match[0], bytes);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.