Skip to content

Instantly share code, notes, and snippets.

@tanaikech tanaikech/submit.md
Last active Apr 1, 2019

Embed
What would you like to do?
Deleting Positioned Images on Google Document using Google Apps Script

Deleting Positioned Images on Google Document using Google Apps Script

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.