Nil*_*rde 2 google-sheets google-apps-script
我已经对一个 png 图像进行了 base64 编码,并将其放置在 Google Sheet 单元格中。需要在 Google App Script 中将其解码为图像并使用以下代码。
代码
var ss = SpreadsheetApp.getActiveSheet();
var strBase64Image = ss.getRange('F1').getValue()
ss.getRange('F2').setValue(strBase64Image); // test fn is working
var decodedBytes = Utilities.base64Decode(strBase64Image); // decode to byte array
var blobImg = Utilities.newBlob(decodedBytes, MimeType.PNG); // create blog from byte array
ss.insertImage(blobImg, 6, 3); // write image to F3 cell
Run Code Online (Sandbox Code Playgroud)
错误
异常:无法解码字符串。
在https://codebeautify.org/base64-to-image-converter中测试时,此 base64 编码的 png 图像字符串将被解码为图像
您需要从 Base64 字符串中删除data:image/png;base64,
- 该标头的存在就是给您带来错误的原因
Exception: Could not decode string.
创建 blob时,您需要为其指定一个名称:var blobImg = Utilities.newBlob(decodedBytes, MimeType.PNG, 'MyImageName');
- 如此处示例代码所示。否则你会得到错误
Exception: Unexpected error while getting the method or property insertImage on object SpreadsheetApp.Sheet.
。
归档时间: |
|
查看次数: |
2799 次 |
最近记录: |