【发布时间】:2020-08-18 21:12:07
【问题描述】:
我正在使用 Google Apps 脚本在 Google 表格中创建一个侧边栏表单,用户可以在其中上传至少一张图片。之后,我想获取每个图像的 Base64 编码内容和图像的文件名。然后,我将向 SendGrid 的 API 发出请求,以创建并发送一封电子邮件,其中附件是用户上传的图像。
这是我目前的代码:
Form.html
<form onsubmit="handleFormSubmit(this)">
<div class="custom-file">
<input type="file" class="custom-file-input" id="images_input" name="images_input" multiple required>
<label class="custom-file-label" for="images_input">Select images...</label>
</div>
<button type="submit">Submit</button>
</form>
<script>
function handleFormSubmit(formObject) {
google.script.run.processForm(formObject);
}
</script>
代码.gs
function processForm(formObject) {
var images = formObject.images_input;
var base64Images = Utilities.base64Encode(images);
// Set up the request to SendGrid
var API_KEY = PropertiesService.getScriptProperties().getProperty('API_KEY');
var sendGridUrl = 'https://api.sendgrid.com/v3/mail/send';
var headers = {
'Authorization': 'Bearer ' + API_KEY
};
var data = {
"personalizations": [{
"to": [{
"email": ...
}],
"dynamic_template_data": {
...
}
}],
"attachments": [{
"content": base64Images,
"filename": "somefilename"
}],
"from": {
"email": ...
},
"template_id": ...",
};
var options = {
'method': 'post',
'contentType': 'application/json',
'headers': headers,
'payload': JSON.stringify(data)
};
UrlFetchApp.fetch(sendGridUrl, options);
}
它不起作用(显然)。我被困在需要获取每个图像的 Base64 编码内容的部分:我认为我做的不对。我应该在客户端使用 FileReader,还是在服务器端使用 Utilities.base64Encode 得到我想要的?任何想法表示赞赏!
【问题讨论】:
标签: google-apps-script google-sheets sendgrid sendgrid-api-v3