Skip to content

Commit b7091dc

Browse files
committed
automations update
1 parent f819124 commit b7091dc

17 files changed

Lines changed: 684 additions & 2 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"scriptId": "1d-At2A2QTQAobdXicLsAai-_P7OrYovTa4qYOWbtV-3CjJaE8XztRgQ6",
3+
"rootDir": "/Users/foobarstein/Documents/courant/courses/agile-dev-devops/generic-mern-stack-project/.automations"
4+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
function emailSprint1ChartsToMe() {
2+
emailChartsToMe('Sprint 1 - Team Plots')
3+
}
4+
5+
function emailSprint2ChartsToMe() {
6+
emailChartsToMe('Sprint 2 - Team Plots')
7+
}
8+
9+
function emailSprint3ChartsToMe() {
10+
emailChartsToMe('Sprint 3 - Team Plots')
11+
}
12+
13+
function emailSprint4ChartsToMe() {
14+
emailChartsToMe('Sprint 4 - Team Plots')
15+
}
16+
17+
function emailChartsToMe(sheetName) {
18+
const charts = getCharts(sheetName)
19+
const to = Session.getActiveUser().getEmail();
20+
const replyTo = "no-reply@knowledge.kitchen"
21+
const subject = "Agile Software Development & DevOps - Team Contribution Charts"
22+
const message = generateEmail(charts)
23+
const options = {
24+
replyTo: replyTo
25+
}
26+
27+
// prepare inline image object
28+
const inlineImages = {}
29+
charts.forEach( (chart, i) => {
30+
inlineImages[`chart_${i}`] = chart
31+
})
32+
33+
// GmailApp.sendEmail(to, subject, message, options);
34+
// Send message with inlineImages object, matching embedded tags.
35+
MailApp.sendEmail(to, subject, "", {
36+
htmlBody: message,
37+
inlineImages: inlineImages
38+
});
39+
40+
Logger.log(message)
41+
}
42+
43+
function generateEmail(charts) {
44+
let imageTags = []
45+
charts.forEach( (chart, i) => {
46+
const tag = `<img src='cid:chart_${i}' />`
47+
imageTags.push(tag)
48+
})
49+
imageTags = imageTags.join('<br />') // convert to string with line break separator
50+
const message = `<h1>Charts</h1>${imageTags}`
51+
return message
52+
}
53+
54+
function generateHtml(sheetName, charts) {
55+
// data:image/gif;base64,
56+
let imageTags = []
57+
charts.forEach( (chart, i) => {
58+
const chartData = Utilities.base64Encode(chart.getBytes()) // get base64 encoded data for this chart
59+
const tag = `<img src='data:image/png;base64,${chartData}' />`
60+
imageTags.push(tag)
61+
})
62+
imageTags = imageTags.join('<br />') // convert to string with line break separator
63+
const message = `<h1>${sheetName}</h1>${imageTags}`
64+
return message
65+
}
66+
67+
function getCharts(sheetName) {
68+
// get all charts
69+
// const sheet = SpreadsheetApp.getActiveSheet();
70+
const ss = SpreadsheetApp.getActiveSpreadsheet() // container spreadsheet
71+
let sheet = ss.getSheetByName(sheetName) // specific worksheet
72+
const range = sheet.getRange("A:Z")
73+
const charts = sheet.getCharts()
74+
75+
// loop through charts
76+
const images = []
77+
charts.forEach( (chart, i) => {
78+
Logger.log(`chart id: ${chart.getChartId()}`)
79+
// Logger.log(JSON.stringify(chart.getOptions(), null, 2))
80+
const image = chart.getAs('image/png')
81+
image.setName(`chart_${i}`)
82+
images.push(image) // add to array
83+
})
84+
return images
85+
}

0 commit comments

Comments
 (0)