Home > OS >  Get all columns data in 1 row in email on google script
Get all columns data in 1 row in email on google script

Time:11-24

enter image description here

I have this google script code that sends out email based on a condition. The code takes the data from different columns and the output of the email is as shown below:

Existing code:

function sendEmails() 
{
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1; // First row of data to process
  var numRows = 6; // Number of rows to process
  // Fetch the range of cells A1:P300
  var dataRange = sheet.getRange(startRow, 1, numRows, 13);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  var dayofemail = data[0][12];
  data.reduce((m, [qua_num,manufacturer,model,serial,descrip,,,,daysPastCal,emailAddress]) => {
  if (daysPastCal > -14) m.set(emailAddress, m.has(emailAddress) ? [...m.get(emailAddress), qua_num,manufacturer,model,serial,descrip] : [qua_num,manufacturer,model,serial,descrip]);
  return m;
}, new Map())
.forEach((qua_num, emailAddress) => {
  var message = qua_num.join('\n')   '\nThe above equipment are approaching due date or out of cal.\nPlease check with QA for replacement or sending the equipment out for re-calibration.';
var subject = 'AUTOMATED ALERT: CHECK EQUIPMENT CALIBRATION ';
  MailApp.sendEmail(emailAddress, subject   dayofemail, message);
});
}

OUTPUT

QUA-014
COHERENT
J-10GE-LE
0303C15R
SENSOR
QUA-029
FLUKE
115
248301132
DIGITAL MULTIMETER
The above equipment are approaching due date or out of cal.
Please check with QA for replacement or sending the equipment out for 
re-calibration

I want to see the email as this:

QUA-014  COHERENT  J-10GE-LE  0303C15R  SENSOR
QUA-029  FLUKE     115        248301132 DIGITAL MULTIMETER

The above equipment are approaching due date or out of cal.
Please check with QA for replacement or sending the equipment out for 
re-calibration

How do I achieve this in google scripts?

CodePudding user response:

Good example of not exactly maintainable code... Well, I'd try to add two \n to this line:

if (daysPastCal > -14) m.set(emailAddress, m.has(emailAddress) ? 
[...m.get(emailAddress), qua_num,manufacturer,model,serial,descrip] : 
[qua_num,manufacturer,model,serial,descrip]);

This way:

if (daysPastCal > -14) m.set(emailAddress, m.has(emailAddress) ? 
[...m.get(emailAddress), qua_num,manufacturer,model,serial,descrip,'\n'] : 
[qua_num,manufacturer,model,serial,descrip,'\n']);

And change \n to \t here:

var message = qua_num.join('\n')   ...

This way:

var message = qua_num.join('\t')   ...

But I can't check it, since you didn't provide a reproducible code.

  • Related