Google应用程序脚本:从js文件发送邮件inlineImage [英] Google app script: Send mail, inlineImage from js File
问题描述
我使用 google.script.run 在Google应用脚本中使用 MailApp API ,并且传递了这样的邮件对象:
I use google.script.run to use the MailApp API in google app script and I pass a mail object like this :
google.script.run.sendMail(mail);
邮件对象的结构如下:
var mail = {
to:"",
cc:"",
subject:"",
htmlBody:"",
inlineImages: inlineImages
}
inlineImages 是一个Javascript对象,可将密钥字符串映射到来自
inlineImages is a Javascript object that map key string to image data (BlobSource) from google ressources
但是随后我在 inlineImages 中传递了 File 对象,我得到由于非法值而失败.
But then I pass File object in inlineImages I get Failed due to illegal value.
我得到这样的 inlineImages :
var inlineImages
function createImages(event) {
var file = event.target.files[0];
var key = "image"+Object.keys(inlineImages).length;
inlineImages[key] = file;
}
我也尝试获取Image对象:
I also try to get Image object :
function createImages(event) {
var file = event.target.files[0];
var key = "image"+Object.keys(inlineImages).length;
var reader = new FileReader();
reader.onload = function(e) {
var img = new Image();
img.src = e.target.result;
inlineImages[key] = img;
}
reader.readAsDataURL(file);
}
推荐答案
FileObject
不是合法值.
法律参数和返回值是JavaScript原语,例如a
+号码,
+布尔值,
+字符串,或
+ null,以及
+由基元,对象和数组组成的JavaScript对象和数组.
+页面中的表单元素作为参数也是合法的,但它必须是函数的唯一参数,并且作为返回值也是不合法的
Legal parameters and return values are JavaScript primitives like a
+ Number,
+ Boolean,
+ String, or
+ null, as well as
+ JavaScript objects and arrays that are composed of primitives, objects and arrays.
+ A form element within the page is also legal as a parameter, but it must be the function’s only parameter, and it is not legal as a return value
要通过文件从客户端到服务器通过上述限制,获取文件的唯一方法是将fileObject转换为上述类型之一.我将使用表单:
The only way you're going to get a file past through the above restrictions from client to server are if you convert the fileObject to one of the above types. I'll go with form:
如果以表单元素作为参数调用服务器函数,则表单将成为一个单独的对象,其字段名称为键,字段值作为值.这些值都将转换为字符串,但文件输入字段的内容除外,这些内容将成为 Blob 对象.
If you call a server function with a form element as a parameter, the form becomes a single object with field names as keys and field values as values. The values are all converted to strings, except for the contents of file-input fields, which become Blob objects.