Permanently delete gmail message after some days - google-apps-script

I'm using this script for deleting old messages from gmail every X days.
It functions correctly, however messages are sent to trash.
I want to delete the messages permanently without sending them to trash.
Someone can modify this script?
// The name of the Gmail Label that is to be autopurged?
var GMAIL_LABEL = "mylabel";
// Purge messages automatically after how many days?
var PURGE_AFTER = "21";
function purgeGmail() {
var age = new Date();
age.setDate(age.getDate() - PURGE_AFTER);
var purge = Utilities.formatDate(age, Session.getTimeZone(), "yyyy-MM-dd");
var search = "label:" + GMAIL_LABEL + " before:" + purge;
// This will create a simple Gmail search
// query like label:Newsletters before:10/12/2012
try {
// We are processing 100 messages in a batch to prevent script errors.
// Else it may throw Exceed Maximum Execution Time exception in Apps Script
var threads = GmailApp.search(search, 0, 100);
// For large batches, create another time-based trigger that will
// activate the auto-purge process after 'n' minutes.
// if (threads.length == 100) {
// ScriptApp.newTrigger("purgeGmail")
// .timeBased()
// .at(new Date((new Date()).getTime() + 1000*60*10))
// .create();
// }
// An email thread may have multiple messages and the timestamp of
// individual messages can be different.
for (var i=0; i<threads.length; i++) {
var messages = GmailApp.getMessagesForThread(threads[i]);
for (var j=0; j<messages.length; j++) {
var email = messages[j];
if (email.getDate() < age) {
email.moveToTrash();
}
}
}
// If the script fails for some reason or catches an exception,
// it will simply defer auto-purge until the next day.
} catch (e) {}
}
thanks

It's pretty simple all you have to do is get all of your message id's in an array and then use the following two lines.
var request={"ids":messageIdArray};
Gmail.Users.Messages.batchDelete(request, "me");
You will have to enable the Advanced Gmail API
batchDelete
So if the rest of your code actually works then this should do it:
var GMAIL_LABEL = "mylabel";
var PURGE_AFTER = "21";
function purgeGmail() {
var age = new Date();
age.setDate(age.getDate() - PURGE_AFTER);
var purge = Utilities.formatDate(age, Session.getTimeZone(), "yyyy-MM-dd");
var search = "label:" + GMAIL_LABEL + " before:" + purge;
try {
var msgA=[];
for (var i=0; i<threads.length; i++) {
var messages = GmailApp.getMessagesForThread(threads[i]);
for (var j=0; j<messages.length; j++) {
var email = messages[j];
if (email.getDate() < age) {
msgA.push(email.getId());
}
}
}
} catch (e) {}
}

Related

Exceeded maximum execution time google script while extract email addresses from my Gmail

I've a google sheet script that can extract emails from gmail labels it work very well on small number of emails but if the emails to in large number it gives me time out error "Exceeded maximum execution time" Is there anyone that can help me out from this problem? following i'm attaching the code that is woking fine with small number of emails but not on large number of emails.
I copy this script form here.
function GetAddresses ()
{
// Get the active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Label to search
var userInputSheet = ss.getSheets()[0];
var labelName = userInputSheet.getRange("B2").getValue();
// Create / empty the target sheet
var sheetName = "Label: " + labelName;
var sheet = ss.getSheetByName (sheetName) || ss.insertSheet (sheetName, ss.getSheets().length);
sheet.clear();
// Get all messages in a nested array (threads -> messages)
var addressesOnly = [];
var messageData = [];
var startIndex = 0;
var pageSize = 100;
while (1)
{
// Search in pages of 100
var threads = GmailApp.search ("label:" + labelName, startIndex, pageSize);
if (threads.length == 0)
break;
else
startIndex += pageSize;
// Get all messages for the current batch of threads
var messages = GmailApp.getMessagesForThreads (threads);
// Loop over all messages
for (var i = 0; i < messages.length ; i++)
{
// Loop over all messages in this thread
for (var j = 0; j < messages[i].length; j++)
{
var mailFrom = messages[i][j].getFrom ();
var mailDate = messages[i][j].getDate ();
// mailFrom format may be either one of these:
// name#domain.com
// any text <name#domain.com>
// "any text" <name#domain.com>
var name = "";
var email = "";
var matches = mailFrom.match (/\s*"?([^"]*)"?\s+<(.+)>/);
if (matches)
{
name = matches[1];
email = matches[2];
}
else
{
email = mailFrom;
}
// Check if (and where) we have this already
var index = addressesOnly.indexOf (mailFrom);
if (index > -1)
{
// We already have this address -> remove it (so that the result is ordered by data from new to old)
addressesOnly.splice(index, 1);
messageData.splice(index, 1);
}
// Add the data
addressesOnly.push (mailFrom);
messageData.push ([name, email, mailDate]);
}
}
}
// Add data to corresponding sheet
sheet.getRange (1, 1, messageData.length, 3).setValues (messageData);
}
//
// Adds a menu to easily call the script
//
function onOpen ()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet ();
var menu = [
{name: "Extract email addresses",functionName: "GetAddresses"}
];
sheet.addMenu ("Start Extracting", menu);
}
In this case you are receiving this error message as your code is exceeding the maximum execution time for Apps Script. Currently the limit is of 6 minutes. You can verify this information in the Current limitations.
You will notice that this is the information displayed for the runtime quota:

Deleting past / old events from all calendars

So I'm trying to delete all events that are older than a month from my calendars.
This script goes halfway; the logger prints out the name of the first event, but then I receive this error:
Exception: Action not allowed
deleteOldEvents # deleteOldEvents.gs.gs:13
The error points to ev.deleteEvent(); so something went wrong there.
Here is the full script:
function deleteOldEvents() {
var fromDate = new Date(2013, 0, 1, 0, 0, 0);
var lastMonth = new Date()
lastMonth.setMonth(lastMonth.getMonth() - 1)
var calendars = CalendarApp.getAllCalendars();
for (var i = 0; i < calendars.length; i++) {
var calendar = calendars[i];
var events = calendar.getEvents(fromDate, lastMonth);
for (var j = 0; j < events.length; j++) {
var ev = events[i];
Logger.log(ev.getTitle()); // show event name in log
ev.deleteEvent();
}
}
}
Modification points:
I think that var ev = events[i]; should be var ev = events[j];.
CalendarApp.getAllCalendars() returns all calendars. In this case, the calendars of "contacts" and "holiday" are also included in the returned calendars. From your error message of Exception: Action not allowed, I guessed that the reason for your current issue might be due to this.
When these points are reflected in your script, how about the following modification?
Modified script:
function deleteOldEvents() {
var fromDate = new Date(2013, 0, 1, 0, 0, 0);
var lastMonth = new Date()
lastMonth.setMonth(lastMonth.getMonth() - 1)
var calendars = CalendarApp.getAllCalendars();
for (var i = 0; i < calendars.length; i++) {
var calendar = calendars[i];
var id = calendar.getId();
if (!["#contacts", "#holiday"].some(e => id.includes(e))) {
var events = calendar.getEvents(fromDate, lastMonth);
for (var j = 0; j < events.length; j++) {
var ev = events[j];
Logger.log(ev.getTitle()); // show event name in log
ev.deleteEvent();
}
}
}
}
For example, CalendarApp.getAllCalendars() includes the other user's calendars and you have no writer permission, how about modifying the above script as follows?
From
if (!["#contacts", "#holiday"].some(e => id.includes(e))) {
To
if (!["#contacts", "#holiday"].some(e => id.includes(e)) && calendar.isOwnedByMe()) {
Or
From
var events = calendar.getEvents(fromDate, lastMonth);
for (var j = 0; j < events.length; j++) {
var ev = events[j];
Logger.log(ev.getTitle()); // show event name in log
ev.deleteEvent();
}
To
try {
var events = calendar.getEvents(fromDate, lastMonth);
for (var j = 0; j < events.length; j++) {
var ev = events[j];
Logger.log(ev.getTitle()); // show event name in log
ev.deleteEvent();
}
} catch(e) {
console.log(e.message);
}
Or, use just try-cath as follows.
function deleteOldEvents() {
var fromDate = new Date(2013, 0, 1, 0, 0, 0);
var lastMonth = new Date()
lastMonth.setMonth(lastMonth.getMonth() - 1)
var calendars = CalendarApp.getAllCalendars();
for (var i = 0; i < calendars.length; i++) {
var calendar = calendars[i];
try {
var events = calendar.getEvents(fromDate, lastMonth);
for (var j = 0; j < events.length; j++) {
var ev = events[j];
Logger.log(ev.getTitle()); // show event name in log
ev.deleteEvent();
}
} catch(e) {
console.log(e.message);
}
}
}
Reference:
getAllCalendars()
Added:
If the number of events is large, I'm worried that the process cost might become high. If you want to reduce the process cost of the script, as another direction, how about the following sample script?
In this case, the batch requests method is used for deleting the events. So, before you run this script, please do the following flow.
1. Install a Google Apps Script library.
In this sample script, batch requests are used. But, the script for requesting batch requests might be a bit complicated. So, in this sample script, a Google Apps Script library is used.
Please install a Google Apps Script library of "BatchRequest". Ref You can see how to install it at here.
2. Enable Calendar API.
Please enable Calendar API at Advanced Google services.
3. Sample script:
function myFunction() {
// Retrieve calendar IDs of calendars with the owner or writer.
const calendarIds = Calendar.CalendarList.list({ maxResults: 250, minAccessRole: "writer" }).items.map(({ id }) => id);
// or const calendarIds = ["###calendarId of your sample Calendar###"];
// Search all events older than 1 month
const max = new Date();
max.setMonth(max.getMonth() - 1);
const timeMax = max.toISOString();
calendarIds.forEach(calendarId => {
const requests = Calendar.Events.list(calendarId, { maxResults: 2500, timeZone: "UTC", timeMax }).items.map(({ id }) => ({
method: "DELETE",
endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events/${id}`,
}));
const len = requests.length;
// Delete searched events.
if (len > 0) {
console.log(`${len} items are deleted.`);
BatchRequest.EDo({ batchPath: "batch/calendar/v3", requests });
console.log(`"${calendarId}" was done.`);
} else {
console.log(`"${calendarId}" has no delete events.`);
}
});
}
In this sample script, from your showing script, for the Google Calendars that you have permission for writing, all events older than 1 month are retrieved. And, those events are deleted.
IMPORTANT:
When this script is run, the events are deleted. So, please be careful about this. I would like to recommend testing this script using a sample Calendar. At that time, please modify const calendars = Calendar.CalendarList.list({ maxResults: 250, minAccessRole: "writer" }).items.map(({ id }) => id); to const calendarIds = ["###calendarId of your sample Calendar###"];, and test it. By this, the specific calendar is used.
In this sample script, as a simple script, pageToken is not used. So, the maximum number of Calendars is 250 and the maximum number of events is 2500. If your actual situation is more than theirs, please use pageToken. By this, all events from all Calendars can be retrieved.
Deleet Events occuring between 100 years ago and 1 month ago:
function deleteEventsOlderThanNMonths(n=1) {
let dt = new Date();
let edt = new Date(dt.getFullYear(),dt.getMonth() - n,dt.getDate());//a months ago
let sdt = new Date(dt.getFullYear() -100,dt.getMonth(),dt.getDate());//100 years ago
let cal = CalendarApp.getCalendarById(calid);
let evs = cal.getEvents(sdt,edt);
evs.forEach(ev => {ev.deleteEvent()})
}

Fusion tables importRows via Apps Script Failing more than not

I am trying to pull csv attachments from a GMail account and push them to a Fusion table. It works sometimes but most of the time it fails with the error "Execution failed: Internal error when processing import. Please try again." from the importRows call for the Fusion table. The size of the import is ~4k so well below the 1m limit.
I will try using the Fusion table SQL INSERT option instead of importRows but just wanted to see if I was perhaps doing something wrong with the importRows call. I even placed a delay of 5 seconds between calls to see if perhaps the Fusion table needed to be fed more slowly.
Here is the App Script:
function getDataFromMailbox() {
var tableID = "XXXXXXXX";
var threads = GmailApp.getInboxThreads();
if (threads.length) {
var thread = threads.pop();
var messages = thread.getMessages();
for (var j=0; j < messages.length; j++) {
var message = messages[j];
if ( message.isUnread() ) {
var from = message.getFrom();
if (from == "xxxxxx#gmail.com") {
var attachments = message.getAttachments();
for (var k=0; k< attachments.length; k++) {
var attachment = attachments[k];
Logger.log( attachment.getSize() );
FusionTables.Table.importRows(tableID, attachment, {startLine:2});
}
} else {
thread.moveToTrash(); // Thread isn't from our source
}
message.markRead();
}
}
Logger.log("Processed %s messages", messages.length);
if ( !thread.isUnread() ) { thread.moveToArchive(); }
}
}
I finally had the chance to look into this again. It looks like the sleep call was the key. If I have a 5 second sleep after a fusion table import I get the import error. If I sleep my script for 10 seconds I get a timeout error. 7 seconds seems to be just right (at least today). Here is my updated script in case anyone wants to log data to a gmail account and then pull it to a Fusion table with an App Script.
function getDataFromMailbox() {
var tableID = "XXXXXXXXXXXXXXXXXX";
var threads = GmailApp.getInboxThreads();
while (threads.length) {
var thread = threads.pop();
var messages = thread.getMessages();
for (var j=0; j < messages.length; j++) {
var message = messages[j];
if ( message.isUnread() ) {
var from = message.getFrom();
if (from == "XXXXXXXXX#gmail.com") {
var attachments = message.getAttachments();
for (var k=0; k< attachments.length; k++) {
var attachment = attachments[k];
var attString = attachment.getDataAsString();
var lines = attString.match(/^.*((\r\n|\n|\r)|$)/gm);
lines.shift(); lines.shift();
Logger.log( lines.length );
FusionTables.Table.importRows(tableID, attachment, {startLine:2});
}
} else {
thread.moveToTrash(); // Thread isn't from our source
}
Logger.log( "Processed message: " + message.getId() );
message.markRead();
Utilities.sleep(7000); // Wait for fusion tables to catch up
}
}
Logger.log("Processed %s messages", messages.length);
if ( !thread.isUnread() ) { thread.moveToArchive(); }
}
}

Google apps script Gmail get message without previous conversation

I am trying to use a google apps script to dump emails that I have under a specific label to a Google Docs Spreadsheet. I want to list each email message body in a thread as a separate row, such that if a thread has a chain of 9 messages, each one is listed separately (without the chain) in a row.
I have managed get it to where each message body + its entire previous thread is stored, in one cell and I can get the entire thread in a cell. But this is not what I want.
This code will put the entire body of the thread in a row.
function getEmails() {
clearCanvas();
var label = GmailApp.getUserLabelByName(LabelWithEmails);
var threads = label.getThreads();
// var threads = GmailApp.getInboxThreads(0, 50);
var row = getFirstRow() + 1;
var firstmessageId = getfirstmsgid();
UserProperties.setProperty("firstmsgid", firstmessageId);
spreadsheet.toast("Loading emails..Please wait. It could take few seconds", "Status", -1);
var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array
for (i = 0; i < 5; ++i)
{
try {
j = messages[i].length; //to process most recent conversation in thread (contains messages from previous conversations as well, reduces redundancy
messageBody = messages[i][j-1].getBody(); //gets body of message in HTML
messageSubject = messages[i][j-1].getSubject();
messageDate = messages[i][j-1].getDate();
messageFrom = messages[i][j-1].getFrom();
Logger.log("Message Subject:" + messageSubject);
Logger.log("Message Date:" + messageDate);
Logger.log("Message From:" + messageFrom);
sheet.getRange(row, 1).setValue(messageFrom);
sheet.getRange(row, 2).setValue(messageSubject);
sheet.getRange(row, 3).setValue(messageDate);
sheet.getRange(row, 4).setValue(getTextFromHtml(messageBody));
row++;
} catch (error) {
spreadsheet.toast("Error Occured. Report it # http://techawakening.org/", "Status", -1);
}
if (i == threads.length - 1) {
spreadsheet.toast("Successfully loaded emails.", "Status", -1);
spreadsheet.toast("Now mark emails to be forwarded by changing the background color of the cells to green. Then select Forward->Forward selected emails", "Status", -1);
}
}
}
This will put each message body including it's previous thread/message chain in a row.
function getEmails() {
clearCanvas();
var label = GmailApp.getUserLabelByName(LabelWithEmails);
var threads = label.getThreads();
// var threads = GmailApp.getInboxThreads(0, 50);
var row = getFirstRow() + 1;
var firstmessageId = getfirstmsgid();
UserProperties.setProperty("firstmsgid", firstmessageId);
spreadsheet.toast("Loading emails..Please wait. It could take few seconds", "Status", -1);
var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array
// messages.length
// jknipp - working except it keeps the thread chain
for (var i = 0; i < threads.length; i++) {
try {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
sheet.getRange(row, 1).setValue(messages[m].getFrom());
sheet.getRange(row, 2).setValue(messages[m].getSubject());
sheet.getRange(row, 3).setValue(messages[m].getDate());
sheet.getRange(row, 4).setValue(getTextFromHtml(messages[m].getBody()));
row++;
}
} catch (error) {
spreadsheet.toast("Error Occured. Report it # http://techawakening.org/", "Status", -1);
}
if (i == threads.length - 1) {
spreadsheet.toast("Successfully loaded emails.", "Status", -1);
spreadsheet.toast("Now mark emails to be forwarded by changing the background color of the cells to green. Then select Forward->Forward selected emails", "Status", -1);
}
}
}
References
https://stackoverflow.com/a/11034461/39803
I was able to pull out only the body of the emails by identifying where the 'previous conversation' started.
var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var LabelWithEmails = sheet.getRange(3, 2).getValue();
function getEmails() {
clearCanvas();
var label = GmailApp.getUserLabelByName(LabelWithEmails);
var threads = label.getThreads();
var row = getFirstRow() + 1;
var firstmessageId = getfirstmsgid();
UserProperties.setProperty("firstmsgid", firstmessageId);
spreadsheet.toast("Loading emails..Please wait. It could take few seconds", "Status", -1);
var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array
for (var i = 0; i < 2;/*threads.length;*/ i++) {
try {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
var msg = messages[m];
var isForward = msg.getBody().search(/---------- Forwarded message/i) != -1;
if(!isValidMessage) continue;
sheet.getRange(row, 1).setValue(msg.getFrom());
sheet.getRange(row, 2).setValue(msg.getTo() + ";" + msg.getCc() + ";" + msg.getBcc());
sheet.getRange(row, 3).setValue(msg.getSubject());
sheet.getRange(row, 4).setValue(msg.getDate());
if(!isForward) {
// Get only this messages body, ignore the previous chain
var body = msg.getBody();
var firstIndexOfThread = body.search(/gmail_quote/i);
body = (firstIndexOfThread == -1) ? body : body.substring(0, firstIndexOfThread);
sheet.getRange(row, 5).setValue(getTextFromHtml(body));
} else {
// Use the whole body if its a forward
sheet.getRange(row, 5).setValue(getTextFromHtml(msg.getBody()));
sheet.getRange(row, 6).setValue("***");
}
row++;
}
} catch (error) {
Logger.log(error);
spreadsheet.toast("Error Occured - please see the logs.", "Status", -1);
}
if (i == threads.length - 1) {
spreadsheet.toast("Successfully loaded emails.", "Status", -1);
}
}
}
This is a case of "garbage-in, garbage-out". When you're using the gmail app in thread view, Google's servers are parsing the body of emails and cleverly hiding the bodies of old messages. This makes it appear that the latest message in a thread consists of only the new lines of that message, and that you have a "chain" of smaller messages.
This is an illusion. The last message in a thread typically contains the new content first, followed by the content of all previous message bodies, as a single message body. Different email services and clients use different patterns for this.
You would need to be able to identify most or all of the ways that the content from previous messages in a thread are represented in the current message body, and use that to extract only the new content.

Google Apps script stops randomly or refuses to launch

I have developed a Google Apps script in a Google Drive spreadsheet that processes e-mails with a certain label (download notifications) and adds these to the spreadsheet. I'm running this through the script editor of the spreadsheet.
My initial solution was quite inefficient - the whole analysis was repeated each time on all e-mails, which caused the runtime to increase for each day. A few days ago I got an error message "runtime exceeded", which is not strange.
My problem is that when trying to update the script to be more efficient, I get some weird problems. The script either stops randomly after processing a few e-mails, or simply refuses to start. Especially the script debugger, it begins to load but never starts.
I have tried several times over the last few days, and even created a new spreadsheet with the same code (in the same account), but still having these problems.
Sometimes when the script manages to run for a while, I have noted that the script output log does not match recent changes in the logging. I have of course saved the script before running it. It feels like there is some lock preventing my script from running/updating/refreshing?
Is there anyone here that recognize these problems?
The code is attached below.
The two relevant entry points are:
processInbox: Updates the spreadsheet based on new (starred) e-mails with a specific label. The label and star is set by an e-mail filter on reception. The star (indicating "new") is removed from each message after processing.
resetAllMsgs: Clears the spreadsheet and stars all relevant messages, causing processInbox to process all messages received with the relevant label.
function resetAllMsgs() {
Logger.log("Starting ResetAll");
var d = SpreadsheetApp.getActive();
var dl_sheet = d.getSheetByName("Download List");
var dlperday_sheet = d.getSheetByName("DownloadsPerDay");
dl_sheet.clear();
dlperday_sheet.clear();
Logger.log("Clearing spreadsheet");
dl_sheet.appendRow(["", "", "Downloads", ""]);
dl_sheet.appendRow(["", "", "Downloaders", ""]);
dl_sheet.appendRow(["Last Download Date", "First Download Date", "Email" , "Product", "Download Count"]);
dlperday_sheet.appendRow(["Date", "Download Count"]);
var label = GmailApp.getUserLabelByName("Download Notification");
// get all threads of the label
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
// get all messages in a given thread
var messages = threads[i].getMessages();
Logger.log("Starring thread " + i);
for (var j = 0; j < messages.length; j++) {
Logger.log(" Starring message " + j);
// Only starred messages are processed by processInbox
messages[j].star();
Utilities.sleep(100);
}
}
};
function processInbox() {
var d = SpreadsheetApp.getActive();
var dl_sheet = d.getSheetByName("Download List");
var dlperday_sheet = d.getSheetByName("DownloadsPerDay");
// If empty spreadsheet, reset the status of all relevant e-mails and add the spreadsheet headers
if (dl_sheet.getLastRow() <= 1) {
resetAll();
}
var label = GmailApp.getUserLabelByName("Download Notification");
var k = 0;
// get all threads of the label
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
if (threads[i].hasStarredMessages()) {
// get all messages in a given thread
var messages = threads[i].getMessages();
// iterate over each message
for (var j = 0; j < messages.length; j++) {
// Unread messages are not previously processed...
if (messages[j].isStarred()) {
var msg = messages[j].getBody();
msg = msg.replace(/\r?\n/g, "");
var email = getDownloader(msg);
if (email == "") {
Logger.log("Found no downloader info: " + messages[j].getSubject() + " " + messages[j].getDate());
}
var date = formatDate(getDownloadDate(msg));
// Check if a new date
var dateCell = find(date, dlperday_sheet.getDataRange(), 0);
if (dateCell == null) {
// If new date, append row in "downloads per day"
dlperday_sheet.appendRow([date, 1]);
dlperday_sheet.getRange(2, 1, dl_sheet.getLastRow()-1, 2).sort([1]);
}
else
{
// If existing date, update row in "downloads per day"
var dlcount = dlperday_sheet.getRange(dateCell.getRow(), dateCell.getColumn()+1).getValue();
}
var productname = getProductName(msg);
// Check if e-mail (user) already exists in the downloads list
var matchingCell = find(email, dl_sheet.getDataRange(), 0);
if ( matchingCell != null ) {
// If user e-mail exists, update this row
var lastDownloadDate = dl_sheet.getRange(matchingCell.getRow(), matchingCell.getColumn()-1).getValue();
var lastDownloadCount = dl_sheet.getRange(matchingCell.getRow(), matchingCell.getColumn()+2).getValue();
if (lastDownloadDate != date) {
dl_sheet.getRange(matchingCell.getRow(), matchingCell.getColumn()-1).setValue(date);
}
dl_sheet.getRange(matchingCell.getRow(), matchingCell.getColumn()+2).setValue(lastDownloadCount+1);
}
else // If new user e-mail, add new download row
{
dl_sheet.appendRow([date, date, email, productname, 1]);
dl_sheet.getRange(2, 4).setValue(dl_sheet.getRange(2, 4).getValue() + 1);
dl_sheet.getRange(4, 1, dl_sheet.getLastRow()-3, 5).sort([1]);
}
// Mark message as processed, to avoid processing it on the next run
messages[j].unstar();
}
}
}
}
};
/**
* Finds a value within a given range.
* #param value The value to find.
* #param range The range to search in.
* #return A range pointing to the first cell containing the value,
* or null if not found.
*/
function find(value, range, log) {
var data = range.getValues();
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
if (log == 1)
{
Logger.log("Comparing " + data[i][j] + " and " + value);
}
if (data[i][j] == value) {
return range.getCell(i + 1, j + 1);
}
}
}
return null;
};
function getDownloader(bodystring) {
var marker = "Buyer Info";
var marker_begin_index = bodystring.indexOf(marker, 1000);
var email_begin_index = bodystring.indexOf("mailto:", marker_begin_index) + 7;
var email_end_index = bodystring.indexOf("\"", email_begin_index);
if (email_end_index < 1000)
{
return "";
}
var email = bodystring.substring(email_begin_index, email_end_index);
if (log == 1)
{
Logger.log("Found e-mail address: " + email + "");
Logger.log(" marker_begin_index: " + marker_begin_index);
Logger.log(" email_begin_index: " + email_begin_index);
Logger.log(" email_end_index: " + email_end_index);
}
latestIndex = email_end_index;
return email;
};
function formatDate(mydate)
{
var str = "" + mydate;
var dateParts = str.split("/");
var day = dateParts[1];
if (day.length == 1)
day = "0" + day;
var month = dateParts[0];
if (month.length == 1)
month = "0" + month;
return dateParts[2] + "-" + month + "-" + day;
};
function getDownloadDate(bodystring) {
var marker = "Download Date:</strong>";
var marker_begin_index = bodystring.indexOf(marker, latestIndex);
var date_begin_index = marker_begin_index + marker.length;
var date_end_index = bodystring.indexOf("<br>", date_begin_index);
latestIndex = date_end_index;
return bodystring.substring(date_begin_index, date_end_index).trim();
};
function getProductName(bodystring) {
var marker = "Item:</strong>";
var marker_begin_index = bodystring.indexOf(marker, latestIndex);
var pname_begin_index = marker_begin_index + marker.length;
var pname_end_index = bodystring.indexOf("</td>", pname_begin_index);
latestIndex = pname_end_index;
return bodystring.substring(pname_begin_index, pname_end_index).trim();
};
Update: Any script I run stops after about 5 seconds, even if it does not call any services.
I tried the following code:
function test() {
Logger.log("Test begins");
Utilities.sleep(5000);
Logger.log("Test ends");
}
The script terminates after about 5 sec, but the last line is not printed. If decreasing the delay to 3 seconds it behaves as expected.
Moreover, to make the script update properly after modifying it, I need to save it, start it, click cancel, and then start it again, otherwise it the log output seems to come from the old version. (I'm running this through the script editor.)
Also, the debugger refuses to start, even for the small script above. Seems to be some problem with my account (johan.kraft#percepio.se). Are there any google employee out there that can check this?