1

function onInstall(e) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Form');


  var Timestamp = e.values[0];
  var Load = e.values[1];
  var Email = e.values[2];
  var Location = e.values[3];

  var template = HtmlService.createHtmlOutputFromFile("notification");
  template.Location = Location;

  MailApp.sendEmail(Email, "subject", "", {
    HtmlBody: template.evaluate().getContent()
  });

}

I have written this small program. Everytime user edit spreadsheet. It should run trigger and shoot an email to mentioned email address in column. I am not good in coding. But Please help me solving this, I have been trying from so long.

Here is link to public spreadsheet Spreadsheet

function() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetName('Form');
}

function onEdit(e) {
  var Timestamp = e.values[0];
  var Load = e.values[1];
  var Email = e.values[2];
  var Location = e.values[3];

  MailApp.sendEmail(Email, "check In Call", "location");

}
Rajiv Goswami
  • 13
  • 1
  • 1
  • 6
  • Apparently the onEdit function is not called the way you think, because it does not get called with an argument. Please show us where the onEdit function is called / used. – Valentin Waeselynck Oct 19 '14 at 20:35
  • @ValentinWaeselynck Please visit this link to see screenshot of my onEdit function https://drive.google.com/file/d/0B8oqG1tmIuPARkpwZU0wMW9RZDA/view?usp=sharing – Rajiv Goswami Oct 19 '14 at 20:47

2 Answers2

2

2 things :

  1. If you try to test this code running it from the script editor it won't work and you'll get the error message you are seing because there is no value assigned to e (since there is actually no event).
  2. the simple onEdit function is not capable to send email, see documentation about events and triggers here.

You should test the code while editing (in real condition) and set up an installable onEdit trigger (script editor menu/ressources/this project triggers/add a new trigger)

You can also "simulate " an event with code, for details see Mogsdad answer here.

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • can you write the correct code, Please. Im reading it hard to understand – Rajiv Goswami Oct 19 '14 at 21:06
  • your code is correct, simply change the function name and install an installable onEdit as I explained. Then try editing your sheet and you'll get the mail. – Serge insas Oct 19 '14 at 21:10
  • 1
    This answer is a good example of "teaching how to fish", and will be more useful in the long run than a "free fish" answer. ([Related Meta discussion](http://meta.stackoverflow.com/questions/274652/i-fixed-your-problem-better-than-thats-your-problem?cb=1).) – Mogsdad Oct 20 '14 at 01:41
2

There's a lot of reasons why this script isn't running. Rather then fix the errors, I wrote the snippet from scratch for you, and I'll try explaining why it's not currently working on a line by line basis.

First , here is some working code:

function onEdit(e) {
  var activateSheet = SpreadsheetApp.getActiveSpreadsheet();
  SpreadsheetApp.setActiveSheet(activateSheet.getSheetByName('Form'));
  var sheet = SpreadsheetApp.getActiveSheet();
  var row = e.range.getRowIndex();//Gets the row the change was made to.  

  var timestamp = sheet.getRange(row, 1).getValues(); //Gets the email
  var load = sheet.getRange(row, 2).getValues(); //Gets the email
  var email = sheet.getRange(row, 3).getValues(); //Gets the email
  var location = sheet.getRange(row, 4).getValues(); //Gets the email

  MailApp.sendEmail(email, "Check In Call", location);

}

The fields 'Load' and 'Timestamp' don't seem to be required for what you want, but I left them in anyway, because maybe you need them.

Here's a list of things that weren't working for you:

  1. Your first function where you called the sheet didn't run at any stage, as there's no trigger, and it doesn't pass the activated sheet onto your 'onEdit' function.
  2. 'getSheetName()' is for retrieving the name of the sheet, not getting the sheet by name. The method you're actually looking for is: 'getSheetByName(name)'.
  3. e.value is only available if what's edited is a single cell, which based on your spreadsheet is unlikely.
  4. Even if e.value wasn't a single cell, you weren't using it correctly, but this isn't the subject for this question.

As a side note: I get the feeling from the syntax errors that you're not familiar with Javascript. Before trying anything with Google Apps script, I recommend you familiarize yourself with Javascript to avoid many, many wasted and frustrated hours trying to complete simple actions.

HDCerberus
  • 2,113
  • 4
  • 20
  • 36
  • This script is working. But it's only sending last row as email body. What i wanted was: When I change location Column's data, It should send "edited,new added data" to the email address written in "Email column". Reason behind this is: My job is to manage truck location. I have several customers and each customer is assigned with a truck. I need to inform my customer when truck change it's location. With this I'l create a column of emails and respective location column. When truck changes location I will edit that column and automated email will be sent to customer. this will save time – Rajiv Goswami Oct 19 '14 at 21:47
  • Right.... I've made a change to the code above which will get the row that was edited, and send an email with the details on that row. Fair warning, this will send an email no matter what column you edit, (Including the email, or literally any other cell). I'm considering this an answer to the fairly broad question you had, however. The Stackoverflow community is here to answer specific code questions, not write applications on your behalf. I included my code mostly to give you specific examples to get you started on your way to fixing things. – HDCerberus Oct 19 '14 at 22:16
  • Thank You. I am getting this error Cannot read property "range" from undefined. (line 5, file "Code" – Rajiv Goswami Oct 19 '14 at 22:23
  • The code runs perfectly for me, it's working. Make sure you're running it by actually making changes to the spreadsheet, rather than running the function in the scripts interface, otherwise (As the error message says) the range for the change will be undefined. – HDCerberus Oct 19 '14 at 22:34
  • Im doing the exactly the same way. do we have to define range somewhere?> – Rajiv Goswami Oct 19 '14 at 22:43
  • No, the point of var row = e.range.getRowIndex(); is that we ARE defining the range as 'The row that I made the changes on'. This code is running for me without issue. I will have a brief look if you wish to grant edit access. – HDCerberus Oct 19 '14 at 22:51
  • here u go pls https://docs.google.com/spreadsheets/d/1GlswC-EVEEfIMIKb5altb7Kuh3qgesW_zF2E6CxR7Fo/edit?usp=sharing – Rajiv Goswami Oct 19 '14 at 22:54