Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active October 30, 2022 18:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/02c4c4ec7cb0ac83771e4306afcd422c to your computer and use it in GitHub Desktop.
Save tanaikech/02c4c4ec7cb0ac83771e4306afcd422c to your computer and use it in GitHub Desktop.
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