Google Script CacheService cache image for inlineImages in sendMail

此生再无相见时 提交于 2019-12-01 00:30:14

Google Cache Service accepts only string values, while you are trying to push a blob into it which is apparently different type. Once you turn it to a proper string - it will be fine.

To ensure that we save it correctly we should use base64encode.

Try to replace

var cache = CacheService.getDocumentCache().put('icon', icon);

with

var cache = CacheService.getDocumentCache()
cache.put('icon', Utilities.base64Encode(icon.getBytes()));

And respectively, when you are getting cached value back you will need to create a new blob object from our string.

First we create empty blob and read cached value:

var iconBlob = Utilities.newBlob("")
var cachedValue = cache.get('icon');

And then you can use it like:

iconBlob.setBytes(Utilities.base64Decode(cachedValue ))
iconBlob.setName('yourname')
iconBlob.setContentType('image/png')

You can check both methods in reference. You can also save Objects in Cache Service the same way if you Stringify them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!