Erase inline objects from Docs using Docs API

Viewed 124

I want to delete the inline embedded images when the id is supplied

I have tried with

Docs.Documents.batchUpdate({requests: [{"deletePositionedObject": {"objectId": "kix.b0ejdj9gt8ia"}}]}, doc.getId());

But this will not delete the images inside

1 Answers

Findings

On my replication, the DeletePositionedObjectRequest produces an error as seen below after inserting an embedded image with an objectId:

The DeletePositionedObjectRequest Request:

enter image description here

The Error:

{
  "error": {
    "code": 400,
    "message": "Invalid requests[0].deletePositionedObject: No positioned object with ID: kix.ig5lfhg9lak6",
    "status": "INVALID_ARGUMENT"
  }
}

This looks like the objectId needed in DeletePositionedObjectRequest is a positioned object & is different from objectId of the inline embedded image.

Recommendation

As an alternative solution, you can use the deleteContentRange instead & adjust it accordingly to your setup.

Test Result:

1. Get the startIndex & endIndex of the embedded inline image on the document.

enter image description here

2. The deleteContentRange request body

enter image description here

Sample document before running the request:

enter image description here

Sample Document after:

enter image description here

Related