简体   繁体   English

如何使用 Apps Script 从 Google Docs 获取 GIF?

[英]How do get GIF from Google Docs using Apps Script?

I want to get the gif image from google docs.我想从谷歌文档中获取 gif 图像。 Using Apps Script, gif images are got as InlineImage.使用 Apps Script,gif 图像被作为 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.您想从 Google 文档中检索原始图像。
    • In your case, you want to retrieve an animation GIF from Google Document.在您的情况下,您想从 Google 文档中检索动画 GIF。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本来实现这一点。

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() .不幸的是,似乎无法使用getImages()直接检索原始图像。 So in this answer, I use the method of documents.get in Google Docs API.所以在这个答案中,我使用了Google Docs API 中的documents.get 方法。

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.使用Google Docs API 中的documents.get 方法从Google Document 检索对象。
  2. Retrieve the source information from the retrieved object.从检索到的对象中检索源信息。
    • The embeded original images can be retrieved from the property of inlineObjects .嵌入的原始图像可以从inlineObjects的属性中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.在使用此脚本之前, 请在高级 Google 服务中启用 Google Docs API。

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.您可以在日志中看到它们的文件 ID。 The filename is "image1", "image2",,, as the sample.文件名为“image1”,“image2”,,,作为示例。

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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