Google Apps Script
Google Apps Script is a cloud-based scripting platform that allows users to automate and extend the functionality of Google Workspace applications (such as Google Sheets, Google Docs, Google Drive, Gmail, and more). It uses JavaScript to create custom scripts and workflows that automate repetitive tasks, integrate with other Google services, and create custom user interfaces. Google Apps Script can be used to create complex workflows, such as automatically generating reports, sending personalized emails, updating data in real-time, or managing files in Google Drive. It is particularly useful for businesses and individuals who want to increase productivity and streamline tasks without needing extensive programming knowledge. The platform also allows the creation of add-ons to further customize Google Workspace applications.
This is a collection of code snippets that can be executed within Google Apps Script. The snippets are highly customizable and can be very useful when there is no own server infrastructure available.
Google Calendar
For each function, the period can be adjusted by changing the hours
variable. The default value is 96.
Send SMS
The SevenCalendarSMS
function sends an SMS for each event occurring within the specified period.
function SevenCalendarSMS() {
const apiKey = 'YOUR_API_KEY'
const to = 'INSERT_YOUR_RECIPIENT_OF_CHOICE'
const toPlaceholder = key => `:${key}:`
const placeholder_id = toPlaceholder('ID')
const placeholder_time = toPlaceholder('TIME')
const placeholder_title = toPlaceholder('TITLE')
const now = new Date()
const statusTagKey = 'seven_status_sms'
const statusTagValue = 'ok'
const textTemplate = 'Google Calendar informs you about the upcoming event {placeholder_title} with ID {placeholder_id} starting at {placeholder_time}.'
const calendarName = ''
const delay = ''
const flash = false
const foreign_id = ''
const from = 'GoogleApps'
const hours = 96 // hours from now until end date
const label = ''
const calendar = '' === calendarName ?
CalendarApp.getDefaultCalendar() :
CalendarApp.getCalendarsByName(calendarName)
const events = calendar.getEvents(now, new Date(now.getTime() + (hours * 60 * 60 * 1000)))
for (const event of events) {
if (statusTagValue === event.getTag(statusTagKey)) continue
let text = textVorlage
Object.entries({
[placeholder_id]: () => event.getId(),
[placeholder_time]: () => event.getStartTime(),
[placeholder_title]: () => event.getTitle()
}).forEach(([k, v]) => text = text.replace(k, v()))
const payload = {
from: from,
text,
to
}
Object.entries({
delay,
flash,
foreign_id,
from: from,
label,
}).forEach(([k, v]) => {
if (false !== v) {
if (true === v) payload[k] = '1'
else if ('' !== v) payload[k] = v
}
})
const response = UrlFetchApp.fetch('https://gateway.seven.io/api/sms', {
headers: {'X-Api-Key': apiKey},
method: 'post',
payload
}).getContentText()
if (100 !== Number.parseInt(response))
throw new Error(`Unexpected status code "${response}".`)
Logger.log(response)
event.setTag(statusTagKey, statusTagValue)
}
}
Voice Call
The SevenCalendarVoice
function performs a text-to-speech call for each event within the specified period.
function SevenCalendarVoice() {
const apiKey = 'YOUR_API_KEY'
const to = 'INSERT_YOUR_RECIPIENT_OF_CHOICE'
const toPlaceholder = key => `:${key}:`
const placeholder_id = toPlaceholder('ID')
const placeholder_time = toPlaceholder('TIME')
const placeholder_title = toPlaceholder('TITLE')
const now = new Date()
const statusTagKey = 'seven_status_voice'
const statusTagValue = 'ok'
const textTemplate = 'Google Calendar informs you about the upcoming event {placeholder_title} with ID {placeholder_id} starting at {placeholder_time}.'
const calendarName = ''
const from = '+49179876543210'
const hours = 96 // hours from now until end date
const calendar = '' === calendarName ?
CalendarApp.getDefaultCalendar() :
CalendarApp.getCalendarsByName(calendarName)
const events = calendar.getEvents(now, new Date(now.getTime() + (hours * 60 * 60 * 1000)))
for (const event of events) {
if (statusTagValue === event.getTag(statusTagKey)) continue
let text = textTemplate
Object.entries({
[placeholder_id]: () => event.getId(),
[placeholder_time]: () => event.getStartTime(),
[placeholder_title]: () => event.getTitle()
}).forEach(([k, v]) => text = text.replace(k, v()))
const payload = {
from: from,
text,
to
}
const response = UrlFetchApp.fetch('https://gateway.seven.io/api/voice', {
headers: {'X-Api-Key': apiKey},
method: 'post',
payload
}).getContentText()
Logger.log(response)
const code = response.split('')[0]
if ('100' !== code) throw new Error(`Unexpected status code "${code}".`)
event.setTag(statusTagKey, statusTagValue)
}
}
Google Contacts
Send SMS
The SevenContactsSMS
function sends an SMS to all your contacts.
function SevenContactsSMS() {
const apiKey = 'YOUR_API_KEY'
const toPlaceholder = key => `:${key}:`
const placeholder_id = toPlaceholder('ID')
const placeholder_name = toPlaceholder('FULL_NAME')
const placeholder_notes = toPlaceholder('NOTES')
const textTemplate = 'Dear {placeholder_name} our new phone number is +4901234567890. Kind regards Phil from seven!'
const delay = ''
const flash = false
const foreign_id = ''
const from = 'GoogleApps'
const label = ''
for (const contact of ContactsApp.getContacts()) {
const phones = contact.getPhones()
if (!phones.length) continue
let text = textTemplate
Object.entries({
[placeholder_id]: () => contact.getId(),
[placeholder_name]: () => contact.getFullName(),
[placeholder_notes]: () => contact.getNotes()
}).forEach(([k, v]) => text = text.replace(k, v()))
const payload = {
from: from,
text,
to: (phones.find(p => p.isPrimary()) || phones.shift()).getPhoneNumber()
}
Object.entries({
delay,
flash,
foreign_id,
from: from,
label,
}).forEach(([k, v]) => {
if (false !== v) {
if (true === v) payload[k] = '1'
else if ('' !== v) payload[k] = v
}
})
const response = UrlFetchApp.fetch('https://gateway.seven.io/api/sms', {
headers: {'X-Api-Key': apiKey},
method: 'post',
payload
}).getContentText()
if (100 !== Number.parseInt(response))
throw new Error(`Unexpected status code "${response}".`)
Logger.log(response)
}
}
Voice Call
The SevenContactsVoice
function makes a text-to-speech call to all your contacts.
function SevenContactsVoice() {
const apiKey = 'YOUR_API_KEY'
const toPlaceholder = key => `:${key}:`
const placeholder_id = toPlaceholder('ID')
const placeholder_name = toPlaceholder('FULL_NAME')
const placeholder_notes = toPlaceholder('NOTES')
const textTemplate = `Dear {placeholder_name} our new phone number is +4901234567890. Kind regards Phil from seven!`
const from = '+49179876543210'
for (const contact of ContactsApp.getContacts()) {
const phones = contact.getPhones()
if (!phones.length) continue
let text = textTemplate
Object.entries({
[placeholder_id]: () => contact.getId(),
[placeholder_name]: () => contact.getFullName(),
[placeholder_notes]: () => contact.getNotes()
}).forEach(([k, v]) => text = text.replace(k, v()))
const payload = {
from,
text,
to: (phones.find(p => p.isPrimary()) || phones.shift()).getPhoneNumber()
}
const response = UrlFetchApp.fetch('https://gateway.seven.io/api/voice', {
headers: {'X-Api-Key': apiKey},
method: 'post',
payload
}).getContentText()
Logger.log(response)
const code = response.split('')[0]
if ('100' !== code) throw new Error(`Unexpected status code "${code}".`)
}
}