我使用一些类似的功能从我的 Google 照片库中插入图片:
function sendEmails103(obj) {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('libImages');
const [hA, ...dt] = sh.getDataRange().getValues();
let idx = {};
hA.forEach((h, i) => idx[h] = i);
let imgObj = {};
vs = dt.filter(r => r[idx['filename']] == obj.row[obj.index['htmlFile']])
vs.forEach((r, i) => {
let params = { muteHttpExceptions: true, headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
let aurl = "https://photoslibrary.googleapis.com/v1/mediaItems/" + r[idx['mediaItemId']]
let resp = JSON.parse(UrlFetchApp.fetch(aurl, params).getContentText());
let burl = `${resp.baseUrl}=w${r[idx['maxwidth']]}-h${r[idx['maxheight']]}`
imgObj[r[idx['Key']]] = UrlFetchApp.fetch(burl).getBlob();
});
let htmlTemplate = HtmlService.createTemplateFromFile(obj.row[obj.index['htmlFile']]);
let html = htmlTemplate.evaluate().getContent();
if (html) {
if (obj.row[obj.index['operation']] == 'Create Draft') {
GmailApp.createDraft(obj.row[obj.index['Recipients']], obj.row[obj.index['Subject']], '', { htmlBody: html, inlineImages: imgObj, replyTo: obj.row[obj.index['replyTo']] });
} else {
GmailApp.sendEmail(obj.row[obj.index['Recipients']],obj.row[obj.index['Subject']],'',{htmlBody:html,inlineImages:imgObj,replyTo: obj.row[obj.index['replyTo']]});
}
}
}
我将 mediaItemIds 和密钥保存在这样的电子表格中:
| Key |
Description |
mediaItemId |
maxwidth |
maxheight |
filename |
| img0 |
Redacted |
Redacted |
384 |
384 |
file103 |
| img1 |
Redacted |
Redacted |
384 |
384 |
file103 |
| img2 |
Redacted |
Redacted |
384 |
384 |
file103 |
| img3 |
Redacted |
Redacted |
384 |
384 |
file103 |
| img4 |
Redacted |
Redacted |
384 |
384 |
file103 |
| img5 |
Redacted |
Redacted |
384 |
384 |
file103 |
| img6 |
Redacted |
Redacted |
384 |
384 |
file103 |
| img7 |
Redacted |
Redacted |
384 |
384 |
file103 |
这是一个使用我的云端硬盘中的图像的简单示例:
function sendEmails101(obj) {
let imgObj = {};
let fldr = DriveApp.getFolderById("folderid");
let files = fldr.getFilesByType(MimeType.JPEG);
let n = 0;
let names = ['die1.jpg', 'die2.jpg'];
let filename = [];
while (files.hasNext()) {
let f = files.next();
let index = names.indexOf(f.getName());
if (~index) {
imgObj[`img${n++}`] = f.getBlob();
filename.push(names[index]);
}
}
let htmlTemplate = HtmlService.createTemplateFromFile(obj.row[obj.index['htmlFile']]);
htmlTemplate.values = obj.row.slice(obj.index['Data0']);
htmlTemplate.filename = filename;
let html = htmlTemplate.evaluate().getContent();
if (html) {
if (obj.row[obj.index[operation]] == 'Create Draft') {
GmailApp.createDraft(obj.row[obj.index['Recipients']], obj.row[obj.index['Subject']], '', { htmlBody: html, inlineImages: imgObj, replyTo: obj.row[obj.index['replyTo']] });
} else {
GmailApp.sendEmail(obj.row[obj.index['Recipients']], obj.row[obj.index['Subject']], '', { htmlBody: html, inlineImages: imgObj, replyTo: obj.row[obj.index['replyTo']] });
}
}
}