简体   繁体   中英

How do get GIF from Google Docs using Apps Script?

I want to get the gif image from google docs. Using Apps Script, gif images are got as InlineImage. But it's only static without animating.

My code

var doc = DocumentApp.openByUrl('url');    
Logger.log(doc.getBody().getImages()[0]);
var encoded = Utilities.base64Encode(doc.getBody().getImages()[0].getBlob().getBytes());
Logger.log(encoded);
  • You want to retrieve the original images from Google Document.
    • In your case, you want to retrieve an animation GIF from Google Document.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Unfortunately, it seems that the original images cannot be directly retrieved using getImages() . So in this answer, I use the method of documents.get in Google Docs API.

Flow:

The flow of this sample script is as follows.

  1. Retrieve the object from Google Document using the method of documents.get in Google Docs API.
  2. Retrieve the source information from the retrieved object.
    • The embeded original images can be retrieved from the property of inlineObjects .
  3. Create the original images from the retrieved source information.

Sample script:

Before you use this script, please enable Google Docs API at Advanced Google services.

function myFunction() {
  var documentId = "###"; // Please set Google Document ID.

  // Retrieve the object from Google Document using the method of documents.get in Google Docs API.
  var obj = Docs.Documents.get(documentId);

  // Retrieve the source information from the retrieved object.
  var inlineObjects = Object.keys(obj.inlineObjects).reduce(function(ar, e, i) {
    var o = obj.inlineObjects[e].inlineObjectProperties.embeddedObject;
    if (o.hasOwnProperty("imageProperties")) {
      var res = UrlFetchApp.fetch(o.imageProperties.contentUri, {headers: {Authorization: "Baerer " + ScriptApp.getOAuthToken()}, muteHttpExceptions: true});
      if (res.getResponseCode() == 200) ar.push(res.getBlob().setName("image" + (i + 1)));
    }
    return ar;
  }, []);

  // Create the original images from the retrieved source information.
  inlineObjects.forEach(function(blob) {
    var id = DriveApp.createFile(blob).getId();
    Logger.log(id)
  })
}
  • When you run the script, the image files are created to the root folder. And you can see the file IDs of them at the log. The filename is "image1", "image2",,, as the sample.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM