'Trying to create an Apps Script that pulls data from a few specific cells in Google Sheets and sends it via MailApp to other users
I was hoping I could figure out why the body is not generating in my emails created via Google Apps Script.
My department in our company is responsible for checking over 500 computers in the plant to verify that all software packages are up to date, I have already created a google sheet that has each computer in a "name" field, with a date located in a "last Evaluated" field. The last updated field will highlight red if it has not been updated in 90 days, and there is a COUNTIFs function to find the total of PCs that have not been updated in 90 days, as well as 180 days, 365 days, and last update unknown.
I want to use Google Apps Script to generate an email that sends weekly (or monthly, not sure yet) to other users in the dept where it reads something like this:
Subject: "Weekly PC Update Report
Body: There are {90 days overdue quantity} PCs that are 90 days overdue, {180 days quantity}..., {365 quantity}..., and {unknown quantity}PCs who's last update is unknown.
Searching for solutions, I found an example that pulls data from a stocks spreadsheet, After making the needed changes, I came up with this (I apologize for any readability issues/errors, I am new to Javascript and programming in general):
(days90 is a named range in a google sheet called Update Log)
function sendEmail(){
var overdue90 = get90data();
var body = getEmailText(overdue90);
MailApp.sendEmail({
to: "EMAIL WITHHELD FOR PRIVACY",
subject: "Weekly PC Update Report",
body: body
});
}
function getEmailText(overdue90) {
var text = "";
overdue90.forEach(function(day90) {
text = text + "There are " + days90 + "computers that have not been updated in the last 90 days." + "\n" + "\n";
});
return text;
}
function get90data() {
var values = SpreadsheetApp.getActive().getSheetByName("Update Log").getRange("days90").getValues();
values.shift(); //remove headers
var days90 = [];
values.forEach(function(value) {
var day90 = {};
day90.amount = value[0];
days90.push(day90);
});
//Logger.log(JSON.stringify(days90));
return days90;
}
The email does send to my inbox with the appropriate subject line, but the body is empty, when I tried making small changes I recieved some emails with the text [Ljava.lang.Object;@265ca7f2. (not every one of these emails were identical, but they all had the [Ljava.lang.Object!@ portion.
Solution 1:[1]
Try to replace return text; in getEmailText function by
return text.toString()
or
return JSON.Stringify(text)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mike Steelson |
