使用base64编码的数据通过Apps脚本将图像插入Google表格 [英] Insert Image into a Google Sheet with Apps Script using base64 encoded data

查看:83
本文介绍了使用base64编码的数据通过Apps脚本将图像插入Google表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为png文件提供一些base64编码的数据,如下面的示例中来自image标记的示例.

Given some base64 encoded data for a png file, as in the example below from the image tag.

<img src="data:image/png;base64,R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7">

我需要使用insertImage()方法的base64编码数据创建图像blob.

I need to create an image blob from the base64 encoded data for the insertImage() method.

sheetClass.insertImage(imageBlob, column, row)

文档: Sheets类-insertImage方法

我尝试使用下面的代码,但是会引发错误:

I've tried using the code below, but it throws the error:

执行失败:从URL或错误的URL检索图像时出错

Execution failed: Error retrieving image from URL or bad URL

文档指出该方法需要一个Blob,而不是URL,但似乎需要链接到图像文件.

The documentation states that the method needs a blob, not a URL, but it seems like it's expecting a link to an image file.

function insertImageFromBase64Src() {
  var data = 'R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7';

  var imageBlob = Utilities.newBlob(Utilities.base64Decode(data), 'image/png').getBytes();

  var ss = SpreadsheetApp.getActiveSpreadsheet();//This code is bound to a Sheet

  var po = {
    shName:'Update File',
    column:1,
    row:37
  }

  var sh = ss.getSheetByName(po.shName);

  var image = sh.insertImage(imageBlob, po.column, po.row);//Insert an image and return the image
}

推荐答案

此修改如何?

  • insertImage可以使用Blob和URL(即图像文件的直接链接).

  • insertImage can use the blob and URL which is the direct link of the image file.

  • 在您的脚本中,imageBlob是一个字节数组,为"number []".这样,我认为会发生错误.
  • In your script, imageBlob is an byte array which is "number[]". By this, I think that an error occurs.

请将名称添加到Blob.

Please add the name to the blob.

  • 未提供名称时,会发生错误.

修改脚本后,它如下所示.

When your script is modified, it becomes as follows.

function insertImageFromBase64Src2() {
  var data = 'R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7';

  var imageBlob = Utilities.newBlob(Utilities.base64Decode(data), 'image/png', 'sample');  // Modified

  var ss = SpreadsheetApp.getActiveSpreadsheet();  //This code is bound to a Sheet

  var po = {
    shName:'Update File',
    column:1,
    row:37
  }

  var sh = ss.getSheetByName(po.shName);

  var image = sh.insertImage(imageBlob, po.column, po.row);//Insert an image and return the image
}

  • 在此修改中,它假定data可以正确解码.
    • In this modification, it supposes that data can be correctly decoded.
      • insertImage(blobSource, column, row)
      • insertImage(url, column, row)

      如果这不是您问题的直接解决方案,我深表歉意.

      If this was not the direct solution of your issue, I apologize.

      这篇关于使用base64编码的数据通过Apps脚本将图像插入Google表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆