1
votes

I have simple script to send e-mail based on data in spreadsheet.

Column 1 is Subject. Column 2,3,4,5 is body.

The problem is - the dipper I am in spreadsheet - the more commas script send in the end of e-mail message. If this is 1st row - no additional commas. 2nd row - 2 commas, 3rd row - 4 commas and so on.

How to get rid of these not required commas? I am really sorry for this newbie question.

The code is

function sendEmails() {
  var time = new Date;
  Logger.log(time);
  var sheet = SpreadsheetApp.getActiveSheet();
  var hojaRegistro = SpreadsheetApp.getActive();
  var activeRow = hojaRegistro.getActiveCell().getRow();
  var subject = sheet.getRange(activeRow, 1, activeRow, 1).getValues();
  var message = sheet.getRange(activeRow, 2, activeRow, 5).getValues();
  MailApp.sendEmail('[email protected]', subject, message);
  sheet.getRange(activeRow, 6).setValue('E-mail sent on ' +time);
  SpreadsheetApp.flush();
}

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Send Email",
    functionName : "sendEmails"
  }];
  sheet.addMenu("Script Center Menu", entries);
};
1

1 Answers

1
votes

If you want to get the first column in active row as the Subject and the columns 2,3,4,5 as body, you have to retrieve range from the same row for all the columns. For this you have to give the 3rd parameter as 1 instead of activeRow(var message = sheet.getRange(activeRow, 2, 1 , 5).getValues();).

Made some change in the code.

Try this:

function sendEmails() {
  var time = new Date;
  Logger.log(time);
  var sheet = SpreadsheetApp.getActiveSheet();
  var hojaRegistro = SpreadsheetApp.getActive();
  var activeRow = hojaRegistro.getActiveCell().getRow();
  var subject = sheet.getRange(activeRow, 1, activeRow, 1).getValues();
  var message = sheet.getRange(activeRow, 2, 1 , 5).getValues();
  MailApp.sendEmail('[email protected]', subject, message);
  sheet.getRange(activeRow, 6).setValue('E-mail sent on ' +time);
  SpreadsheetApp.flush();
}

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Send Email",
    functionName : "sendEmails"
  }];
  sheet.addMenu("Script Center Menu", entries);
}

Hope that helps!