getBlob()导致“图像数据无效。”错误。 Google Apps脚本

问题描述 投票:1回答:1

我的目标是1.从谷歌文档中获取base64图像字符串2.将其作为图像保存到驱动器3.getblob from image并将新图像插入到与真实图像相同的文档中。

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();
}

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

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

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

它正在返回新图像的ID

它将图像视为blob✓

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

google-apps-script base64 blob google-apps
1个回答
1
投票

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

Modification points:

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

Modified script:

在这个修改过的脚本中,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);
  }
}

Note:

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

Reference:

如果这不是你想要的结果,我道歉。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.