将单元格中的Base64图像文本转换为插入该特定单元格的图像。

huangapple go评论72阅读模式
英文:

converting base64 image text in a cell to an image inserted in that specific cell

问题

Sure, here's the translated code part:

function insertImageFromBase64Src2() {
  var data = 'iVBORw0KGgoAAAANSUhEUgAAAYYAAACVCAYAAABctV1kAAAAAXNSR0IArs4c6QAAD+ZJREFUeF7tnb0LNFcVxk+6WAgpLOyilRaCFhZ2UbCwUxCxkmgpFkGwiIWoWFjoH5BgoekCCkZsTGUCKV67pFNENGCRgEWsTCEoT5jH3HfcfXfmzJ3ZmTO/gWWX3bkf53fu3ud+z2PBBQEIQAACEGgIPAYNCEAAAhCAQEsAYaA87JnAR4bMfTYinmwyqu//FhGvDu/6zAUBCHQigDB0AnnCaFQ56/XUYLsrcX/fIlHFrZcq+LYS12fd/8IQz8sR8fggAo5P97w5ROaw/k1pK05d/k1xvRsRD0ZpndBFmAyBHAGEIcftLKFUAbetdVf6rph/MYBQxe3K35X9lFa8W/7jyn1K2NYHrSjp+6cj4ocIw1mKKXb2JoAw9CZ6nPjaFr8qdrW+XcHaClfQ7ZDN3Er7HkS+PwwzvXKPxEkTAkcngDAc3YPT89+2/t1Cfz4i3hpa1vq9SkX688Em9Rq4IACBmQQQhpnADnS7hcDj8Kr01fLX+xFa/VnUsvv3w1CSh7qycREOAqckgDDUcbsqRI2t6/p6IwCajK0sBGMPShR0fa6Oa7EEAtsSQBi25d0ztVYIPDTU9gp6pnWUuDSEJC6IwlE8Rj53SQBh2KVbrmbKYqAegS4NlZytR3ANjkTh7Yh47mQ9pGOVYHJ7CAIIwyHcFFpl4zkDiQGTqg/7TcNH4vONQhPoxyiZ5LIkAYRhv25tewf0DK77CVHYbxkmZwclgDDsz3ESBPUQNG9A7+C6f8TJcwr0FPZXjsnRgQkgDPtxnoRAgqAVRJo3qLKnYC3C7iloovlMq67W4km8EPgfAYTh/oXBLV9Vbmr5ct0mwPDRbUbcAYE0AYQhjW5xQK0s0r4DBGEeSomCelfqKdCrmseOuyEwiQDCM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xEPLtF5m+koXL8JI2aHXiCLLxH4KjC4APv9Aenl7B9YZYovBAR2iQ493Kl3FbiqhTby/e8ERFfvFDhu6JvBeDNRgD8vcqJLomIhry86kd5t3DMzf8a9/+VH

<details>
<summary>英文:</summary>

say i have a cell containing a base64-image text and i want to convert that into its actual image, is there a way to code that in google sheet?

my problem was i am able to turn it into a blob image with the actual image i want but i dont know how to insert it in a specific cell.

Here&#39;s my code:
`function insertImageFromBase64Src2() {
var data = &#39;iVBORw0KGgoAAAANSUhEUgAAAYYAAACVCAYAAABctV1kAAAAAXNSR0IArs4c6QAAD+ZJREFUeF7tnb0LNFcVxk+6WAgpLOyilRaCFhZ2UbCwUxCxkmgpFkGwiIWoWFjoH5BgoekCCkZsTGUCKV67pFNENGCRgEWsTCEoT5jH3HfcfXfmzJ3ZmTO/gWWX3bkf53fu3ud+z2PBBQEIQAACEGgIPAYNCEAAAhCAQEsAYaA87JnAR4bMfTYinmwyqu//FhGvDu/6zAUBCHQigDB0AnnCaFQ56/XUYLsrcX/fIlHFrZcq+LYS12fd/8IQz8sR8fggAo5P97w5ROaw/k1pK05d/k1xvRsRD0ZpndBFmAyBHAGEIcftLKFUAbetdVf6rph/MYBQxe3K35X9lFa8W/7jyn1K2NYHrSjp+6cj4ocIw1mKKXb2JoAw9CZ6nPjaFr8qdrW+XcHaClfQ7ZDN3Er7HkS+PwwzvXKPxEkTAkcngDAc3YPT89+2/t1Cfz4i3hpa1vq9SkX688Em9Rq4IACBmQQQhpnADnS7hcDj8Kr01fLX+xFa/VnUsvv3w1CSh7qycREOAqckgDDUcbsqRI2t6/p6IwCajK0sBGMPShR0fa6Oa7EEAtsSQBi25d0ztVYIPDTU9gp6pnWUuDSEJC6IwlE8Rj53SQBh2KVbrmbKYqAegS4NlZytR3ANjkTh7Yh47mQ9pGOVYHJ7CAIIwyHcFFpl4zkDiQGTqg/7TcNH4vONQhPoxyiZ5LIkAYRhv25tewf0DK77CVHYbxkmZwclgDDsz3ESBPUQNG9A7+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AASSBBCGJDiCQQACEKhKAGGo6lnsggAEIJAkgDAkwREMAhCAQFUCCENVz2IXBCAAgSQBhCEJjmAQgAAEqhJAGKp6FrsgAAEIJAkgDElwBIMABCBQlQDCUNWz2AUBCEAgSQBhSIIjGAQgAIGqBBCGqp7FLghAAAJJAghDEhzBIAABCFQlgDBU9Sx2QQACEEgSQBiS4AgGAQhAoCoBhKGqZ7ELAhCAQJIAwpAERzAIQAACVQkgDFU9i10QgAAEkgQQhiQ4gkEAAhCoSgBhqOpZ7IIABCCQJIAwJMERDAIQgEBVAghDVc9iFwQgAIEkAYQhCY5gEIAABKoSQBiqeha7IAABCCQJIAxJcASDAAQgUJUAwlDVs9gFAQhAIEkAYUiCIxgEIACBqgQQhqqexS4IQAACSQL/BTZRVrTb2JToAAAAAElFTkSuQmCC&#39;;
var imageBlob = Utilities.newBlob(Utilities.base64Decode(data), &#39;image/png&#39;, &#39;sample&#39;);  // Modified
var ss = SpreadsheetApp.getActiveSpreadsheet();  //This code is bound to a Sheet
var po = {
shName:&#39;Sheet1&#39;,
column:2,
row:2
  }
var sh = ss.getSheetByName(po.shName);
var image = sh.insertImage(imageBlob, po.column, po.row);//Insert an image and return the image
}`

i expect the formed blob to be inserted inside a cell and not outside the cell

</details>


# 答案1
**得分**: 0

Sure, here's the translated part of the content you provided:

如果您的base64数据是有效的图像数据那么可以考虑将其与Class CellImage一起使用作为数据URL吗当这反映在您的脚本中时它变为如下所示

### 修改后的脚本
```javascript
function insertImageFromBase64Src2() {
  var data = '###'; // 您的base64数据

  var sheet = SpreadsheetApp.getActiveSheet();
  var image = SpreadsheetApp.newCellImage().setSourceUrl(`data:image/png;base64,${data}`).build();
  sheet.getRange(2, 2).setValue(image);
}
  • 当我使用您的base64数据测试了这个修改后的脚本后,我确认图像被放置在单元格“B2”中。如下所示。

    将单元格中的Base64图像文本转换为插入该特定单元格的图像。

  • getRange(2, 2)来自您的脚本var po = { shName: 'Sheet1', column: 2, row: 2 }

参考链接:

英文:

If your base64 data is valid image data, how about using it with Class CellImage as the data URL? When this is reflected in your script, it becomes as follows.

Modified script:

function insertImageFromBase64Src2() {
  var data = &#39;###&#39;; // Your base64 data

  var sheet = SpreadsheetApp.getActiveSheet();
  var image = SpreadsheetApp.newCellImage().setSourceUrl(`data:image/png;base64,${data}`).build();
  sheet.getRange(2, 2).setValue(image);
}
  • When I tested this modified script using your base64 data, I confirmed that an image is put to cell "B2". It is as follows.

    将单元格中的Base64图像文本转换为插入该特定单元格的图像。

  • getRange(2, 2) is from your script of var po = { shName:&#39;Sheet1&#39;, column:2, row:2 }.

Reference:

huangapple
  • 本文由 发表于 2023年4月13日 14:36:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定