Deleting Positioned Images on Google Document using Google Apps Script

Gists

This is a sample script for deleting the positioned images on Google Document using Google Apps Script. In the current stage, unfortunately, there are no methods for deleting the positioned images in Class PositionedImage, yet. But when Google Docs API is used, the positioned images can be deleted.

When you use this script, please enable Google Docs API at Advanced Google Services and API console. You can see how to enable them at here

Sample script

function myFunction() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  body.clear();

  // Retrieve paragraphs.
  var paragraphs = body.getParagraphs();

  // Retrieve the object IDs of the positioned images.
  // Create request body for the method of batchUpdate in Google Docs API using the retrieved object IDs.
  var requests = paragraphs.reduce(function(ar, e) {
    return ar.concat(e.getPositionedImages().map(function(f) {
      return {deletePositionedObject: {objectId: f.getId()}};
    }));
  }, []);

  // Delete the positioned images.
  if (requests.length > 0) {
    Docs.Documents.batchUpdate({requests: requests}, doc.getId());
  }
}

Reference

 Share!