I have written a script that, when published as a web app, will count the number of emails in my inbox, along with the date of the earliest sent message, and output as a .csv:
function doGet() {
// get all threads in inbox
var threads = GmailApp.getInboxThreads();
var dataLength = threads.length
var earliestMessage = new Date();
for (var y = 0; y < dataLength; y++) {
var message = threads[y]
//sets last message date in thread y
var msgDate = message.getLastMessageDate();
//checks last message date to current value of earliestMessage, updates if necessary
if(msgDate.valueOf() < earliestMessage.valueOf()) {
earliestMessage = msgDate
}
}
//save to external files
return ContentService.createTextOutput(dataLength + '\n' + Utilities.formatDate(earliestMessage, 'GMT', 'dd/MM/yyyy')).downloadAsFile('emailData.csv');
}
This works fine, but I would now like this script to return the same information from a shared mailbox that I have access to. Since this is a shared mailbox, I do not have the option of just accessing its Google Drive and saving the script there (please let me know if I am mistaken in this!), so the script will only ever return information about my own account.
I have tried searching for similar issues, but the closest I have found are the following, neither of which seem to have clear solutions:
Will the script need to be rewritten (and if so, how?), or is there a way of directing the GmailApp class to the shared mailbox instead of my own?
So I believe that your shared mailbox should be setup as just a plain old google user. If so then there is no reason why you can't setup a google drive for them.
The second part of this problem is what you want to create with this script, If you just want the information from your shared mailbox you can either:
User accessing the web app
(in Publish -> Deploy as web app...
) and when you run it, make sure that you are logged in as the shared user.Now if you wanted to combine the two feeds of information, that's a different story, I don't believe that you can, in the same script, run as two different users. I have also tested Libraries and they seem to use the same session user, so as far as I can see you really only have one option. Creating a webapp script to extract the information and run it as your shared user, and return it as json/xml (or even CSV if you are just appending it), then use a HTTP call(using URLFetchApp) in your existing script to retrieve that information and process it and do whatever you need with it (running as you). This will be a slow option, but at least it will do what you want.