【问题标题】:getBlob() causing "Invalid image data." Error. Google Apps ScriptgetBlob() 导致“无效的图像数据”。错误。 Google Apps 脚本
【发布时间】:2019-07-08 19:01:59
【问题描述】:

我的目标是。

  1. 从 google 文档中获取 base64 图像字符串
  2. 将其作为图像保存到驱动器中
  3. 从图像中获取blob,并将新图像插入到与真实图像相同的文档中。
function getImage(doc) {
    var tempBase64 = doc.getBody().getParagraphs()[doc.getBody().getParagraphs().length - 1].getText();
    var base64 = tempBase64.replace("data:image/svg+xml;base64,", "");
    var decoded = Utilities.base64Decode(base64);
    return decoded;
}

function run() {
    var doc = DocumentApp.getActiveDocument();
    var img = getImage(doc); //returns decoded blob
    var body = DocumentApp.getActiveDocument().getBody();
    //gets the name of the current doc
    var filename = DriveApp.getFileById(DocumentApp.getActiveDocument().getId());
    var blobImg = Utilities.newBlob(img, MimeType.SVG, filename);
    var currentFolder = DriveApp.getFolderById("1UuP2vZNXLVtgrZxAHLSNZUqVnx0xeSgW");
    //saves image as same name
    var newimg = currentFolder.createFile(blobImg).getId();
    //wait for image to save
    Utilities.sleep(5 * 1000);
    // get blob 
    var blob = DriveApp.getFileById(newimg).getBlob();
    var cursor = doc.getCursor();
    if (cursor) {
    //insert in to doc
        cursor.insertInlineImage(blob);
    }

}

function onOpen() {
    run();
}

目前它正在读取文档并获取 base64 字符串 ✓ 例如:

image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMTIiIGhlaWdodD0iMTUiPjxwYXRoIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2U9InJnYigwLCAwLCAxMzkpIiBmaWxsPSJub25lIiBkPSJNIDEgMyBsIDEgNCIvPjxwYXRoIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2U9InJnYigwLCAwLCAxMzkpIiBmaWxsPSJub25lIiBkPSJNIDggMSBsIDEgMSIvPjxwYXRoIHN0cm9rZS1saW5lam9pbj0icm91bmQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2U9InJnYigwLCAwLCAxMzkpIiBmaWxsPSJub25lIiBkPSJNIDMgMTAgYyAwLjAyIDAuMDcgMC4zMSAzLjU0IDEgNCBjIDAuODQgMC41NiAzLjg5IDAuNDcgNSAwIGMgMC44IC0wLjM0IDIgLTMgMiAtMyIvPjwvc3ZnPg==

它在特定文件夹中保存为图像✓

它正在返回新图像的 id。✓

它将图像作为一个 blob ✓

在插入图像时,我得到了无效的图像数据。 (第 20 行,文件“代码”)。从字面上不知道如何解决。有人帮忙吗?

【问题讨论】:

    标签: google-apps-script google-drive-api base64 blob google-docs


    【解决方案1】:

    我认为您的脚本是正确的。只有一个问题。在当前阶段,mimeType 为image/svg+xml 的图像还不能直接插入谷歌文档。因此需要将 svg 图像转换为其他 mimeType。在此修改中,我使用 Drive API 转换为 image/png。还有一些不是 Google API 的 API,用于将 svg 转换为其他 API。我认为您的情况有几种解决方法。所以请认为这只是其中之一。

    修改点:

    • 从创建的文件中检索文件 ID 并使用 Drive API 检索 thumbnailLink。
    • 链接图片大小调整后,获取PNG图片块。
      • 已经发现通过修改=s###的查询参数可以修改图片大小。我用过这个。 Ref
    • 将 PNG blob 放入 Document。

    修改脚本:

    在这个修改后的脚本中,run() 被修改了。请确认以下修改后的脚本。

    function run() {
      var doc = DocumentApp.getActiveDocument();
      var img = getImage(doc); //returns decoded blob
      var body = DocumentApp.getActiveDocument().getBody();
      //gets the name of the current doc
      var filename = DriveApp.getFileById(DocumentApp.getActiveDocument().getId());
      var blobImg = Utilities.newBlob(img, MimeType.SVG, filename);
      var currentFolder = DriveApp.getFolderById("1UuP2vZNXLVtgrZxAHLSNZUqVnx0xeSgW");
      //saves image as same name
      var newimg = currentFolder.createFile(blobImg).getId();
      //wait for image to save
      Utilities.sleep(5 * 1000);
      // get blob
    
      // --- Added --- Begin
      var width = 300; // width of the image. A value of 300 pixels was used as a sample.
      var url = "https://www.googleapis.com/drive/v3/files/" + newimg + "?fields=thumbnailLink&access_token=" + ScriptApp.getOAuthToken();
      var link = JSON.parse(UrlFetchApp.fetch(url).getContentText()).thumbnailLink.replace(/=s\d+/, "=s" + width);
      var blob = UrlFetchApp.fetch(link).getBlob();
      // --- Added --- End
    
      var cursor = doc.getCursor();
      if (cursor) {
      //insert in to doc
          cursor.insertInlineImage(blob);
      }
    }
    

    注意:

    • Drive API 无法检索 svg 图像的宽度和高度。所以在这个修改中,我使用了固定宽度。
    • 我认为在您的脚本中,可以使用 Drive API。但如果出现与 API 相关的错误,请在 API 控制台启用 Drive API。

    参考:

    如果这不是您想要的结果,我深表歉意。

    【讨论】:

    • @AJFMEDIA 欢迎。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多