Getting email upon inserting rows to google sheets

I do have a google sheet where I would like to be notified with any new row/rows inserted to it.

Is there any extension which can handle that, or I should use Google Apps Script as I am new for it and never used it before.

Upvotes: 2

Views: 832

Answers (2)

Marios
Marios

Reputation: 27350

Explanation:

You are looking for a trigger to be activated when you insert a new row.

  • The following script will send an email when a new row is inserted in Sheet1. Feel free to change the name of the sheet: sh.getName()=='Sheet1'.

Solution:

function sendEmailRow(e) {
 const sh = e.source.getActiveSheet();  
 if(sh.getName()=='Sheet1' && e.changeType === 'INSERT_ROW') {
  MailApp.sendEmail({
      to: "[email protected]", 
      subject: "A new row has been added",
      body: "New row!"
    });
  }
}

Installation

  • Copy and Paste the above code in a blank script in the script editor (Tools => Script editor) and save the changes.

You need to create an installable onChange trigger for sendEmailRow:

  • Click on: current project's triggers

menu1

  • Create a new trigger and then select exactly these settings:

menu2

  • Your application is ready! As soon as you insert a new row in Sheet1, you will send an email to the selected recipient.

Upvotes: 3

Gav
Gav

Reputation: 393

I'm not aware of an extension/add-on that would handle this but you could do it with an script

The below will send an email, on a trigger that alerts you as to values in column A of any new rows that have been added. It then sets a value in column c to say an email has been sent so when the script runs again it doesn't pick these up.

function sendEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('Sheet1'); //<-- Change sheet name
  var rng = sh.getDataRange();
  var val = rng.getDisplayValues();
  var lr = rng.getLastRow();
  var blank = "";
  var match = false;
  var body = "Hello, "+"\n\n"+"A row with"; //<-- change to anything
  for (var i = 1; i < lr; i++) {
    if (val[i][0] != blank && val[i][2] != "Sent") { //<-- 0 refers to value in column a (A=0 B=1 C=2 etc) It also checks here column C for if it has "Sent" in it
      var bodyVal = val[i][0];
      body = body+"\n - "+bodyVal; //<-- use this to add a value from that row to email body
      match = true;
      sh.getRange(i+1,3).setValue('Sent'); //<-- where to set the "Sent" value (change above as well)
    }
  }
  body = body+"\n has been added.";
  if(match) {
    MailApp.sendEmail({
      to: "[email protected]", //<-- change email
      subject: "Reminder", //<-- email subject
      body: body
    });
  }
}

Upvotes: 0

Related Questions