0
votes

I'm trying to update a placeholder image with a new image that has an updated URL. The URL in fact is a valid Google Static Map URL that I'm using in other contexts successfully. I'm using the Google Document API to manipulate the document. Following the code I've been using:

 var element = body.findElement(DocumentApp.ElementType.INLINE_IMAGE).getElement();
  var imageMap = element.asInlineImage();
  // if there was an image found in document
  if (imageMap != null) {
     // get current parent and index inside parent
    var parent = imageMap.getParent();
    var childIndex = parent.getChildIndex(imageMap);
    // remove image from paragraph
    imageMap = imageMap.removeFromParent();
    // get static image url for territory
    var url = getStaticMapURLForTerritory(id);
    Logger.log(url);
    imageMap.setLinkUrl(url);
    // create a new image
    parent.insertInlineImage(childIndex, imageMap)
  }

This seems to work fine in that it does update the image url correctly. However, the image itself (the result of the url) is not updated. When I click on the link URL it does return the correct image.

Is there a way to force a refetch of the image blob associated with the URL? I've also attempted to use UrlFetchApp but that complains about a missing size parameter (google static api) which is certainly included in the url string and within the max 640x640 bounds.

I've exhausted all my options unless....

TIA, --Paul

1

1 Answers

0
votes

setLinkUrl only does that: sets the link. To actually add a new image you'll have to get its blob:

function replaceImage() {
  // [...]

  // get static image url for territory
  const url = getStaticMapURLForTerritory(id)
  const response = UrlFetchApp.fetch(url)

  // create a new image
  parent.insertInlineImage(childIndex, response.getBlob())
    .setAltDescription(img.getAltDescription())
    .setAltTitle(img.getAltTitle())
    .setWidth(img.getWidth())
    .setHeight(img.getHeight())
    .setLinkUrl(url)
}

References