(这个答案是一个正在进行中的工作。开始一个答案,以便在我弄清楚这些位时将它们放好。如果其他人帮助我找出丢失的位,我会接受他们的而不是这个)。
如何获取原始图片URL(或提取为base64字符串)
据我所知,没有办法获取默认 URL。但是,我能够获得 base64 字符串。这有点令人费解,但有效。
代码.gs
// Gets an InlineImage in some way. I'm using the currently selected image,
// but that's irrelevant to the code sample.
// @return {InlineImage}
function getImage() {
// gets the InlineImage element somehow
}
// Gets the actual data URI.
// Note: this function uses image/png only. You can change this by changing
// it in the two places, or using a variable. Just be sure the two spots
// match.
// @return {string}
function getDataUri() {
return 'data:image/png;base64,' + Utilities.base64Encode(getImage().getAs('image/png').getBytes());
}
MyDialogJavaScript.html
$(function () {
google.script.run
.withSuccessHandler(function (data) { console.log(data); })
.withFailureHandler(function (err) { console.log('failure: ' + err); })
.getDataUri();
});
重要提示:创建对话框时必须使用 SandboxMode.IFRAME,否则您会得到如下信息:
Rejecting <img>.setAttribute('src', blahblahblah
这显然是由于通常使用的 Caja 编译器的限制。请在此处查看答案以获取更多信息:Using base64-encoded images with HtmlService in Apps Script