This question already has an answer here:
Count number of Gmail emails per sender
(1 answer)
Closed 2 years ago.
Referencing another user's question, I've been attempting to obtain the number of emails for each email address, from within my Gmail Inbox. I was sure to enable to Gmail API in Google Developer Console, as well as in the Google Apps Script. However, upon running the script, I receive an error which states
"Exception: Invalid argument: value (line 22, file "Code")".
I've been searching Google for some answers, but my lack of understanding has hindered my progress.
Below is the full code, excluding my email address. Any suggestions are greatly appreciated.
function sender_list_paged(token) {
var token=token||null;
var query="in:inbox";
var sender_array=[];
var uA=[]
var cObj={};
do{
var result=Gmail.Users.Messages.list("MY-EMAIL#gmail.com", {maxResults:10,pageToken:token,q:query});
var list=result;
Logger.log(list);
for(var i=0;i<list.messages.length;i++) {
var sender=GmailApp.getMessageById(list.messages[i].id).getFrom();
if(uA.indexOf(sender)==-1) {
uA.push(sender);
sender_array.push([sender]);
cObj[sender]=1;
}else{
cObj[sender]+=1;
}
}
token=list.nextPageToken;
PropertiesService.getUserProperties().setProperty("lastpagetoken", token);
}
while(token);
sender_array.forEach(function(r){
r.splice(1,0,cObj[r[0]]);
});
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet()
sh.clear();
sh.appendRow(['Email Address','Count']);
sh.getRange(2, 1,sender_array.length,2).setValues(sender_array).sort({column:1,ascending:true});
}
I see the post you are referring to has a workable solution that runs:
function sender_list() {
var inbox_threads=GmailApp.search('in:anywhere');
var sender_array=[];
var uA=[];
var cObj={};
for(var i=0;i<inbox_threads.length;i++) {
var message=inbox_threads[i].getMessages();
for(var x=0;x<message.length; x++) {
var sender=message[x].getFrom();
if(uA.indexOf(sender)==-1) {
uA.push(sender);
sender_array.push([sender]);
cObj[sender]=1;
}else{
cObj[sender]+=1;
}
}
}
sender_array.forEach(function(r){
r.splice(1,0,cObj[r[0]]);
});
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet()
sh.clear();
sh.appendRow(['Email Address','Count']);
sh.getRange(2, 1,sender_array.length,2).setValues(sender_array).sort({column:1,ascending:true});
}
Desclaimer:
The code used in this post comes from here:
Count number of Gmail emails per sender
Related
I am using the following code to fetch the email data from Google groups. It is working well for most of the groups, but for the ones that has more than 90-100 email IDs, it's returning an error : "Exception: Service invoked too many times in a short time: premium groups read. Try Utilities.sleep(1000) between calls."
I am not able to find any solution to the above, could someone help me figure this out? I have GSuite enterprise edition.
var wsSettings = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("INPUT");
var name = wsSettings.getRange("B1").getValue();
try {
var group = GroupsApp.getGroupByEmail(name);
}
catch(e) {
var group = false;
}
if (group) {
var members = group.getUsers();
var membersLength = members.length;
for (var i=0; i<membersLength; i++) {
var memberEmailAddress = members[i].getEmail();
var memberRole = group.getRole(memberEmailAddress).toString().toLowerCase();
memberDetails.push([memberEmailAddress, memberRole]);
}
var groupMembersSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final List');
var lastRow = groupMembersSheet.getLastRow();
groupMembersSheet.getRange(2, 1, lastRow, 2).clearContent();
groupMembersSheet.getRange(2, 1, memberDetails.length, 2).setValues(memberDetails);
}
else {
}
I'm trying to get the data out of my google classroom course I'm using google app script. I want to get the title and the id to appear in a google sheet. my reference for understanding is https://developers.google.com/classroom/guides/manage-classwork#retrieve_student_responses I want to retrieve student responses from the assignments in google classroom.
But I first need access to the courseId. which can be found here https://developers.google.com/classroom/reference/rest/v1/courses.courseWork/list
this is the code i am using to get the course work from the google app script API
function getCourseWork(courseId) {
var id = [];
var title = [];
var optionalArgs = {
pageSize: 100
};
var response = Classroom.Courses.CourseWorkMaterials.list(courseId, optionalArgs)
var courseWork = response.courseWork
for (var i = 0; i <= courseWork.length; i++) {
try {
var courseWorkId=id.push(courseWork[i].id);
var courseWorkTitle=title.push(courseWork[i].id);
} catch (err) {
return { "id":courseWorkId, "title":courseWorkTitle}
}
}
}
I'm. not sure why the code is not working.
I'm trying to run a check that would send an email automatically if the due date is tomorrow on a piece of equipment. The problem is that I'm not sure if I can possibly use this -> https://developers.google.com/apps-script/reference/spreadsheet/relative-date?hl=ru To run the check.
This is what I have so far, hopefully someone can help me here, I'm still fairly new to app script, and am honestly not the best coder in general. Combine that with my inability to find good examples for app script, and my problems are fairly compounded.
function emailForLaptops() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Main');
var tomorrow = new Date(TOMORROW);
var active = sheet1.getActiveRange().getValues(); // gets the active highlighted data
var data = sheet1.getRange("C3:D21").getValues();
data.forEach(function(row) {
if (row[1] == tomorrow){
MailApp.sendEmail(row[2], "Code Admin Test", "This is a test to see if an email can be sent via app scripting");
}
});
}
function emailForLaptops() {
var ss=SpreadsheetApp.getActive();
var sheet1=ss.getSheetByName('Main');
var dt=new Date();
var tomorrow=Utilities.formatDate(new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()+1),Session.getScriptTimeZone(),"MM/dd/yyyy");
var active=sheet1.getActiveRange().getValues(); // gets the active highlighted data
var data=sheet1.getRange("C3:D21").getValues();
data.forEach(function(row) {
var r1dts=Utilities.formatDate(new Date(row[1]),Session.getScriptTimeZone(),"MM/dd/yyyy");
if (r1dts==tomorrow){
MailApp.sendEmail(row[2], "Code Admin Test", "This is a test to see if an email can be sent via app scripting");
}
});
}
First time using script this is what I have after a couple hours.
I'm setting up a daily text blast which in testing works but I get this error Exception: Failed to send email: no recipient
at sendtext(Code:8:9)
Here's my sheet:
https://i.stack.imgur.com/RS5c2.png
And my code:
function sendtext() {
var sh=SpreadsheetApp.getActive().getSheetByName("Word-a-Day");
var lrow=sh.getLastRow()
for(var i=2;i<=lrow;i++) {
MailApp.sendEmail(sh.getRange(i,2).getValue(), "Khmer Word-A-Day", sh.getRange(2, 3).getValue())
}
}
The test texts goes through but I get the an error every time. Is this because of using the sms gateway?
Try this:
function sendtext() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName("Word-a-Day");
var lrow=sh.getLastRow();
var rg=sh.getRange(2,2,sh.getLastRow()-1,2);
var vs=rg.getValues();
var q=MailApp.getRemainingDailyQuota();
for(var i=0;i<vs.length;i++) {
MailApp.sendEmail(vs[i][0], "Khmer Word-A-Day", vs[i][1]);
//Utilities.sleep(5000);//Perhaps you need a delay here.
}
}
Found random space in a cell in the text gateway column so it was throwing an error. Thanks for the help.
this is my app script function where I am updating the google spreadsheets. I am getting the all the Html user filled data separated by "/".
I am sending a response from an Html page to google spreadsheet and want to
get the milliseconds details on date object, to avoid the overwriting.
Let's say if multiple people are submitting at once then, I can get only
the last entry because the date object is created with mm/dd/yyyy hh:mm:ss
format and if multiple people are submitting at the same instance of time
like ( 4 people are submitting at 03/03/2020 04:55:55) then I will be
getting the last submitted record and other records will be overwritten by
the last record.
Can anyone of you help me with this? How should I solve this requirement?
Please find my javascript function from the HTML page where I am calling google app script API to run the function userClick written in app script.
function sendData(u_wk_Det)
{
google.script.run.withSuccessHandler(function(response)
{console.log(response);})
.withFailureHandler(function(err){console.log(err);})
.userClick(u_wk_Det);
}
/*code shown below is my app script function using which I am updating the google spreadsheets.*/
`function userClick(u_wk_det)
{
Logger.log('table data - ');
var ss =
SpreadsheetApp.openById('sheetid').getSheetByName('Sheet2');
var Srow = 2;
var Lrow = ss.getLastRow();
var Lcol = ss.getLastColumn();
var data = ss.getRange(2, 1, Lrow , Lcol).getValues().filter(function(o)
{return o !=="" });
for(i in data)
{
var j=i;
j++;
j++;
var Crow = data[i];
if (Crow[0]=='')
{
var dt = new Date();
Logger.log(dt);
Logger.log(Crow[0]);
if(u_wk_det!=="")
{
var user_week_details = u_wk_det.split("/");
ss.getRange(j,1).setValue(dt);
ss.getRange(j,2).setValue(user_week_details[0]);
ss.getRange(j,3).setValue(user_week_details[1]);
ss.getRange(j,4).setValue(user_week_details[2]);
}
}
}
}`