1. Home
  2. Third Party Solutions
  3. Google Apps Script

Google Apps Script

This is a collection of code snippets which can be run inside Google Apps Script. The snippets are highly customizable and can be very handy in cases where no own server infrastructure is given.

Google Calendar

For every function the time span can be changed by altering the variable hours. The default value is 96.

SMS

The function Sms77CalendarSMS sends a SMS for every event occurring in the given timeframe.

function Sms77CalendarSMS() {
    const apiKey = 'INSERT_YOUR_SMS77_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 = 'sms77_status_sms';
    const statusTagValue = 'ok';

    const textTemplate = `Google Calendar: Upcoming event: ${placeholder_title} with id ${placeholder_id} starting ${placeholder_time}.`;

    const calendarName = '';
    const debug = false;
    const delay = '';
    const flash = false;
    const foreign_id = '';
    const from = 'GoogleApps';
    const hours = 96; // hours from now until end date
    const label = '';
    const performance_tracking = false;

    for (const event of ('' === calendarName ? CalendarApp.getDefaultCalendar()
        : CalendarApp.getCalendarsByName(calendarName)).getEvents(
        now, new Date(now.getTime() + (hours * 60 * 60 * 1000)))) {
        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,
            text,
            to,
        };
        Object.entries({
            debug,
            delay,
            flash,
            foreign_id,
            from,
            label,
            performance_tracking
        }).forEach(([k, v]) => {
            if (false !== v) {
                if (true === v) {
                    payload[k] = '1';
                } else if ('' !== v) {
                    payload[k] = v;
                }
            }
        });

        const response = UrlFetchApp.fetch('https://gateway.sms77.io/api/sms', {
            headers: {
                SentWith: 'Google-Apps',
                '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

The function Sms77CalendarVoice issues a text-to-speech call for every event occurring in the given timeframe.

function Sms77CalendarVoice() {
    const apiKey = 'INSERT_YOUR_SMS77_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 = 'sms77_status_voice';
    const statusTagValue = 'ok';

    const textTemplate = `Google Calendar informs you about the upcoming event ${placeholder_title} starting at ${placeholder_time}.`;

    const calendarName = '';
    const from = '+491771783130';
    const hours = 96; // hours from now until end date
    const xml = false;

    for (const event of ('' === calendarName ? CalendarApp.getDefaultCalendar()
        : CalendarApp.getCalendarsByName(calendarName)).getEvents(
        now, new Date(now.getTime() + (hours * 60 * 60 * 1000)))) {
        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,
            text,
            to,
        };

        if (xml) {
            payload.xml = '1';
        }

        const response = UrlFetchApp.fetch('https://gateway.sms77.io/api/voice', {
            headers: {
                SentWith: 'Google-Apps',
                'X-Api-Key': apiKey,
            },
            method: 'post',
            payload,
        }).getContentText();

        Logger.log(response);

        const code = response.split('\n')[0];
        if ('100' !== code) {
            throw new Error(`Unexpected status code "${code}".`);
        }

        event.setTag(statusTagKey, statusTagValue);
    }
}

Google Contacts

SMS

The function Sms77ContactsSMS sends a SMS to all of your contacts.

function Sms77ContactsSMS() {
    const apiKey = 'INSERT_YOUR_SMS77_API_KEY';

    const toPlaceholder = key => `:${key}:`;
    const placeholder_id = toPlaceholder('ID');
    const placeholder_full_name = toPlaceholder('FULL_NAME');
    const placeholder_notes = toPlaceholder('NOTES');

    const textTemplate = `Dear ${placeholder_full_name} our new phone number is +4901234567890. Kind regards Phil from Sms77!`;

    const debug = false;
    const delay = '';
    const flash = false;
    const foreign_id = '';
    const from = 'GoogleApps';
    const label = '';
    const performance_tracking = false;

    for (const contact of ContactsApp.getContacts()) {
        const phones = contact.getPhones();
        if (!phones.length) {
            continue;
        }

        let text = textTemplate;
        Object.entries({
            [placeholder_id]: () => contact.getId(),
            [placeholder_full_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(),
        };
        Object.entries({
            debug,
            delay,
            flash,
            foreign_id,
            from,
            label,
            performance_tracking
        }).forEach(([k, v]) => {
            if (false !== v) {
                if (true === v) {
                    payload[k] = '1';
                } else if ('' !== v) {
                    payload[k] = v;
                }
            }
        });

        const response = UrlFetchApp.fetch('https://gateway.sms77.io/api/sms', {
            headers: {
                SentWith: 'Google-Apps',
                'X-Api-Key': apiKey,
            },
            method: 'post',
            payload,
        }).getContentText();

        if (100 !== Number.parseInt(response)) {
            throw new Error(`Unexpected status code "${response}".`);
        }

        Logger.log(response);
    }
}

Voice

The function Sms77ContactsVoice issues a text-to-speech call to all of your contacts.

function Sms77ContactsVoice() {
    const apiKey = 'INSERT_YOUR_SMS77_API_KEY';

    const toPlaceholder = key => `:${key}:`;
    const placeholder_id = toPlaceholder('ID');
    const placeholder_full_name = toPlaceholder('FULL_NAME');
    const placeholder_notes = toPlaceholder('NOTES');

    const textTemplate = `Dear ${placeholder_full_name} our new phone number is +4901234567890. Kind regards Phil from Sms77!`;

    const from = '+491771783130';
    const xml = false;

    for (const contact of ContactsApp.getContacts()) {
        const phones = contact.getPhones();
        if (!phones.length) {
            continue;
        }

        let text = textTemplate;
        Object.entries({
            [placeholder_id]: () => contact.getId(),
            [placeholder_full_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(),
        };

        if (xml) {
            payload.xml = '1';
        }

        const response = UrlFetchApp.fetch('https://gateway.sms77.io/api/voice', {
            headers: {
                SentWith: 'Google-Apps',
                'X-Api-Key': apiKey,
            },
            method: 'post',
            payload,
        }).getContentText();

        Logger.log(response);

        const code = response.split('\n')[0];
        if ('100' !== code) {
            throw new Error(`Unexpected status code "${code}".`);
        }
    }
}