I am creating a script that will grab all unread e-mails, move the bodies of these e-mails into a Google spreadsheet, push all of this data through to Slack via webhook, then cl
Edit: In your added answer, you can either delete clear()
and add in sheet.getRange("A1:A40").clearContent();
below Logger.log(row);
. Another way is that you can also take out the clear()
out of the saveEmails()
and call like this,
function saveEmails() {
var array2d = getEmails_(SEARCH_QUERY);
if (array2d) {
appendData_(SpreadsheetApp.getActiveSheet(), array2d);
}
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
var row = [];
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (values[i][j]) {
row.push(values[i][j]);
}
}
}
Logger.log(row);
var str = values.toString();
var payload = {
"text": str
}
clear();
// var post = {
// "method": "post",
// "payload": JSON.stringify(payload)
//
// };
// return UrlFetchApp.fetch(url, post);
}
function clear() {
var app = SpreadsheetApp;
var sheet = app.getActiveSpreadsheet().getActiveSheet();
sheet.getRange("A1:A40").clearContent();
}
I figured it out! It wanted a toString() command. The following works perfectly EXCEPT that it does not clear the sheet at the end. It will only clear if a separate function is running.
var SEARCH_QUERY = "label:inbox is:unread to:me";
var url = "SLACK WEBHOOK URL";
function getEmails_(q)
{
var emails = [];
var threads = GmailApp.search(q);
for (var i in threads) {
var msgs = threads[i].getMessages();
for (var j in msgs)
{
emails.push([msgs[j].getPlainBody().replace(/<.*?>/g, '\n')
.replace(/^\s*\n/gm, '').replace(/^\s*/gm, '').replace(/\s*\n/gm, '\n')]);
msgs[j].markRead();
}
}
return emails;
}
function appendData_(sheet, array2d) {
sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
}
function saveEmails() {
var array2d = getEmails_(SEARCH_QUERY);
if (array2d) {
appendData_(SpreadsheetApp.getActiveSheet(), array2d);
}
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
var row = [];
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (values[i][j]) {
row.push(values[i][j]);
}
}
}
Logger.log(row);
var str = values.toString();
var payload = {
"text": str
}
// var post = {
// "method": "post",
// "payload": JSON.stringify(payload)
//
// };
// return UrlFetchApp.fetch(url, post);
function clear() {
var app = SpreadsheetApp;
var sheet = app.getActiveSpreadsheet().getActiveSheet();
sheet.getRange("A1:A40").clearContent();}
}