当前位置:首页 > 未分类 > 正文内容

Get Email Notifications for Edits in Google Spreadsheet

ceacer3周前 (05-02)未分类1765

A client wants to receive email notifications as soon as someone edits a Google Spreadsheet that is shared with a team of people. Google Docs supports the onEdit() trigger that runs whenever an edit is made to any cell of the sheet but a limitation is that the onEdit trigger cannot send emails. Nor can be used to call external API though the URLFetch service.

As a workaround, the edits were stored as a Property and another time-based trigger would periodically send the stored value by email.

/**
 * @OnlyCurrentDoc
 */

function onEdit(e) {

    var key = "ROWCHANGES";

    var range = e.range;

    var date = Utilities.formatDate(new Date(), e.source.getSpreadsheetTimeZone(), "dd-MM-yy HH:MM:s");

    var properties = PropertiesService.getUserProperties();

    var sheet = e.source.getActiveSheet();

    var data = sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).getValues()[0];

    data[range.getColumn() - 1] = "" + data[range.getColumn() - 1] + "";

    var edits = {
        name: sheet.getSheetName(),
        data: data
    };

    var existing = JSON.parse(properties.getProperty(key)) || {};

    existing[date] = edits;

    properties.setProperty(key, JSON.stringify(existing));

}

function onEdit_Email() {

    var properties = PropertiesService.getUserProperties();

    var json = JSON.parse(properties.getProperty("ROWCHANGES"));

    var html = "":

        for (var keys in json) {

            html = html + "
["
+ keys + "][" + json[keys].name + "] — " + json[keys].data;
} if (html !== "") { MailApp.sendEmail(email, subject, "", { htmlBody: html }); properties.deleteAllProperties(); } }

相关文章

How to Send Email Reminders for Google Forms Automatically

How to Send Email Reminders for Google Forms Automatically

You are using Google Forms to collect registrations for an upcoming event and would like to send ema...

How to Link Postal Addresses to Google Maps in Google Sheets

How to Link Postal Addresses to Google Maps in Google Sheets

Bob Canning writes: I have a Google Spreadsheet with postal addresses in column A. Each week, a real...

How to Transcribe Audio and Video Attachments in Gmail

How to Transcribe Audio and Video Attachments in Gmail

The Save Gmail to Google Drive add-on lets you automatically download email messages and file attach...

Formulas in Google Sheets Disappear When New Rows Are Added

Formulas in Google Sheets Disappear When New Rows Are Added

An order form, created in Google Forms, requires customers to provide their full name, the item quan...

How to Make Personalized Place Cards with Guest Names

How to Make Personalized Place Cards with Guest Names

Whether it is a wedding party or a business conference, those tent-shaped place cards are ideal for...

Send WhatsApp Messages with a Google Sheets Function

Send WhatsApp Messages with a Google Sheets Function

In a previous tutorial, you learned how to send WhatsApp messages from Google Sheets using the offic...