Thanks in advance for your help.
I discovered Google Script recently and have been trying to write a script that scans the most recent 500 emails in my inbox a few times a day, reviews the content of each email thread, and then compares the sender of each email, with email addresses in a spreadsheet. If a sender matches an email address in the spreadsheet, the script should enter "yes". Here is the code I have been able to come up with below. The problem I am having is that the code is very slow, and after it gets through about 100 rows of data, it times out.
I am wondering if an expert has an idea on where I am going wrong? I am sure my code is very flawed. However, I'm unable to figure out how to improve it.
function getMail(){
var thread = GmailApp.getInboxThreads(0,500);
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 300; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 10); //
var data = dataRange.getValues();
var target = "Sales Email"
for (var b = 0; b < data.length; ++b){
var row = data[b];
var leadEmail = row[4];
for (var i = 0; i < thread.length; ++i){
var message = thread[i].getMessages();
for(var c = 0; c < message.length; ++c){
var currentMessage = message[c];
var subject = currentMessage.getSubject();
if(subject.indexOf(target) !== -1) {
var sender = currentMessage.getFrom().replace(/^.+<([^>]+)>$/, "$1");
if(sender == leadEmail){
sheet.getRange(startRow + b, 7).setValue("yes");
SpreadsheetApp.flush();
break;
}
}
}
}
}
}
I managed to make the code a lot faster. It isn't necessary to fetch all messages from all threads over and over again for each row. Just fetch them once and write the sender into an array.
function getMail(){
var senders = [],
threads = GmailApp.getInboxThreads(0,500),
target = "Sales Email";
for (var i = 0; i < threads.length; ++i) {
var messages = threads[i].getMessages();
for(var j = 0; j < messages.length; j++) {
var currentMessage = messages[j],
subject = currentMessage.getSubject(),
sender = currentMessage.getFrom().replace(/^.+<([^>]+)>$/, "$1");
if(subject.indexOf(target) !== -1) senders[sender] = true;
}
}
var sheet = SpreadsheetApp.getActiveSheet(),
startRow = 2,
numRows = 300,
dataRange = sheet.getRange(startRow, 1, numRows, 10),
data = dataRange.getValues();
for (var b = 0; b < data.length; ++b) {
var row = data[b],
leadEmail = row[4];
if(senders[leadEmail]) {
sheet.getRange(startRow + b, 7).setValue("yes");
}
}
}
Related
Here is the code I have worked on- I have tried many different combinations and none of them are giving me the desired results- Can you please explain why my code is incorrect?
the code is iterating the the rows looking for rows larger than 0- if so, copy the row to the target sheet- However I am getting the wrong rows copied over?
function try2() {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final');
var lastrow = sheet2.getLastRow()+1;
var target = sheet2.getRange(lastrow,1,1,5);
var data = sheet1.getRange('E1:E500').getValues();
for(var i=1; i< data.length; i++) {
Logger.log(i)
if (data[i] > 0) {
Logger.log(data[i])
sheet1.getRange(i, 1, 1, 5).copyTo(target);
}
}
}
I updated the code to have the lastrow & target variables in the for loop with still undesired results.
function try2() {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final');
var data = sheet1.getRange('E1:E500').getValues();
for(var i=1; i< data.length; i++) {
var lastrow = sheet2.getLastRow()+1;
var target = sheet2.getRange(lastrow,1,1,5);
//Logger.log(i)
if (data[i] !== "") {
Logger.log(data[i])
sheet1.getRange(i, 1, 1, 5).copyTo(target);
}
}
}
Requirement:
Copy data from one sheet to the next free row of another sheet.
Solution:
function try2() {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final');
var targetLastRow = sheet2.getLastRow() + 1;
var dataLastRow = sheet1.getLastRow();
var dataRange = sheet1.getRange(1,5,dataLastRow);
var data = dataRange.getValues();
for(var i = 0; i < data.length; i++) {
if (data[i]) {
sheet1.getRange(i+1, 1, 1, 5).copyTo(sheet2.getRange(targetLastRow+i,1));
}
}
}
Explanation:
This script will loop through all of the rows in column E on your first sheet, and copy any that aren't blank across to your sheet "Final". By using dataLastRow to get the data. You save time over defining a set range E1:E500, the script will now only get the necessary amount of rows to run the copy whereas before it was running the for loop 500 times no matter what.
The data is copied to targetLastRow+i, which is the next available row of sheet "Final", incremented by 1 every time the loop completes.
Code that worked altering #ross code- Thanks
function try3() {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final');
var targetLastRow = sheet2.getLastRow() + 1;
var dataLastRow = sheet1.getLastRow();
var dataRange = sheet1.getRange(1,5,dataLastRow);
var data = dataRange.getValues();
for(var i = 0; i < data.length; i++) {
Logger.log(data[i])
if (data[i] > 0) {
Logger.log(data[i])
sheet1.getRange(i+1, 1, 1, 5).copyTo(sheet2.getRange(targetLastRow+i,1));
}
}
}
I have a Google Apps Script that pulls and lists all the assignments from Google Classroom within a domain in a Google Sheet, but now I need to pull only certain assignments after a certain date. I must be putting the date or condition wrong, because the array remains empty at the end (whereas if I comment out the if condition with the date filter, the code works perfectly fine and lists all assignments).
The other "non-visible" problem is that the for loop is still going through all of the assignments before applying the filter, which wastes time. The idea would be to have for loop be limited to only the assignments after a certain date from the beginning instead of searching through everything, but I don't know if that is possible.
The code is below.
function listAssignmentsbyDate() {
var s1 = SpreadsheetApp.getActiveSpreadsheet();
var sh = s1.getSheetByName('LA');
var response = Classroom.Courses.list();
var courses = response.courses;
var arr1 = []; var arr2 = []; var arr3 = []; var arr4 = [];
var today = new Date();
var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
for (i = 0; i < courses.length; i++) {
var course = courses[i];
var ids = course.id;
var title = course.name;
var state = course.courseState;
arr1.push([ids,title,state]);
}
for (i = 0; i < arr1.length; i++) {
if (arr1[i][2] !== 'ARCHIVED'){
var list = arr1[i][0];
var page = null, t = [];
do {
var tea = Classroom.Courses.CourseWork.list(list, {pageToken: page, pageSize: 100});
page = tea.nextPageToken; var t = t.concat(tea.courseWork);
} while (page);
try {
for (q = 0; q < t.length; q++) {
var c = t[q];
var name = arr1[i][1];
// The line below is what is not working.
if(oneWeekAgo < c.creationTime){
var ti = c.title;
var des = c.description;
var st = c.state;
var link = c.alternateLink;
var typ = c.workType;
var poi = c.maxPoints;
var create = c.creationTime;
var due = c.dueDate;
var duet = c.dueTime;
var cour = c.courseId;
var ids = c.id;
var user = c.creatorUserId;
arr2.push([name,ti,des,st,link,typ,poi,create,due,duet,cour,ids]);
arr3.push(user);
}
}
} catch (e) {continue;}
}
}
for (r=0; r < arr3.length; r++){
try {
var eu = arr3[r];
var us = AdminDirectory.Users.get(eu).primaryEmail;
arr4.push([us]);
} catch(e){continue;}
}
sh.getRange(2, 2, arr4.length, arr5[0].length).setValues(arr4);
sh.getRange(2, 3, arr2.length, arr2[0].length).setValues(arr2);
s1.getSheetByName('CACHE').getRange('B1').setValue('');
}
I have the following Apps Script, which is almost what I'm looking for. What it's doing is getting a list of items from a Google Sheet to populate a Google Form Multiple Choice Question with a list of items. However, it's including all 100 rows, rather than just the ones that have values in them.
What am I missing to make sure it only includes cells that have a len > 1?
function getWorkbookNames(){
var form = FormApp.getActiveForm();
var items = form.getItems();
var ss = SpreadsheetApp.openByUrl(
'spreadsheetnamestringhere');
var sheet = ss.getSheetByName('Unique Databases');
var sheetValues = sheet.getRange("A2:A").getValues();
var values = sheetValues.slice(1);
var names = [];
for(var p = 0; p < values.length; p++){
names.push(values[p][0])
}
var pValues = items[8].asListItem();
pValues.setChoiceValues(names).setRequired(true)
var areas = [];
for (var i = 0; i < values.length; i++) {
areas.push(values[i][1])
}
}
Any help/advice you all could provide would be greatly appreciated.
Solved by adding another filter layer:
function getWorkbookNames() {
var form = FormApp.getActiveForm();
var items = form.getItems();
var ss = SpreadsheetApp.openByUrl(
'spreadsheetnamestringhere');
var sheet = ss.getSheetByName('Unique Databases');
var sheetValues = sheet.getRange("A2:A").getValues();
var filterValues = sheetValues.filter(String);
var values = filterValues.slice(1);
var names = [];
for (var p = 0; p < values.length; p++) {
names.push(values[p][0])
}
var pValues = items[8].asListItem();
pValues.setChoiceValues(names).setRequired(true)
var areas = [];
for (var i = 0; i < values.length; i++) {
areas.push(values[i][1])
}
}
I would like to make sure that an e-mail is not send twice. The script itself is working, however the Insertion of:
sheet.getRange(startRow + i, 6).setValue(EMAIL_SENT);
does not work, yet it adds a random value to row 40.
Could someone help me figure out the problem? Thanks a lot already in advance!
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(1, 2);
var subject = range.getValues();
var range = sheet.getRange(1, 9);
var numRows = range.getValues();
var startRow = 4;
var dataRange = sheet.getRange(startRow, 1, numRows,9)
var data = dataRange.getValues();
var EMAIL_SENT = "EMAIL_SENT";
for (i in data) {
var row = data[i];
var emailAddress = row[0];
var message = row[8];
var emailSent = row[5];
if (emailSent != EMAIL_SENT) {
MailApp.sendEmail(emailAddress, subject, message,{noReply: true});
sheet.getRange(startRow + i, 6).setValue(EMAIL_SENT);
}
}
}
Short answer
Instead of using for(i in data){...} use for(var i = 0;i < data.lenght;i++) {...}
Explanation
for (variable in object) assigns a different property name on each iteration but what you need is the data index to be used to calculate the row index.
Reference
for..in
I'm trying to transpose (copy column content into a row) a range from one sheet to another. Yet the catch is that each row has a different number of columns populated, and I don't want to have blank columns copied over into the output sheet.
So I was trying to do the following:
Go to 'import manipulation' tab
row by row (in a loop), check what the last POPULATED column is
Then col by col (second loop), copy each cell's value then paste it onto "Transpose" sheet in the first available row
Keep doing this until the last Populated column was copied over, and then move onto the next row
Repeat
I've ran the debugger and nothing comes up, however when I run the function it doesn't paste anything into the "Test" sheet. Any suggestions?
function transpose(sh) {
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Import Manipulation");
var lastrow = sh.getLastRow();
var numrows = lastrow;
for(var i= 1; i<= numrows; i++) {
Logger.log(i);
var rowrange = sh.getRange(i, 50);
Logger.log(rowrange);
var lastcol = rowrange.getValues().length;
for(var j=5; j<= lastcol; j++) {
var colrange = sh.getRange(i,j);
var copycell = colrange.getValue();
var pastesheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
var pasterow = pastesheet.getLastRow();
Logger.log(pasterow);
var pastecell = pastesheet.getRange(pasterow + 1, 1);
Logger.log(pastecell);
var doit = pastecell.setValue(copycell);
doit(pastecell);
}
}
}
I was able to do it so that it transposes, and it's working (albeit probably not the most efficient).... appreciate suggestions for how to make it more efficient!
function transpose(sh) {
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Import Manipulation");
var lastrow = sh.getLastRow();
var numrows = lastrow;
var lastcol = sh.getLastColumn();
for(var i= 1; i<= numrows; i++) {
Logger.log(i);
var rowrange = sh.getRange(i,1,1,lastcol);
Logger.log(rowrange);
for(var j=5; j<= lastcol; j++) {
var colrange = sh.getRange(i,j);
var copycell = colrange.getValue();
var pastesheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Transpose");
var pasterow = pastesheet.getLastRow();
Logger.log(pasterow);
var pastecell = pastesheet.getRange(pasterow + 1, 1);
Logger.log(pastecell);
pastecell.setValue(copycell);
}
}
}
This line of code:
var rowrange = sh.getRange(i, 50);
Is only getting one cell. On the first loop, it gets the cell in row 1 column 50.
The variable lastcol in this line:
var lastcol = rowrange.getValues().length;
will always be 1. getValues() gets a 2D array and in this case it will be [[cellValue]]
I created some new code:
function transpose(sh) {
var arrayThisRow,cellValue,i,j,lastCol,lastrow,numrows,pastesheet,
rowrange,rowValues,sh;
sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Import Manipulation");
pastesheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
lastrow = sh.getLastRow();
lastCol = sh.getLastColumn();
Logger.log('lastCol: ' + lastCol);
numrows = lastrow;
arrayThisRow = [];
for(i=1; i<= numrows; i++) {
//Logger.log(i);
rowrange = sh.getRange(i,1,1,lastCol);
//Logger.log(rowrange);
rowValues = rowrange.getValues();//
rowValues = rowValues[0];//There is only 1 inner array
for(j=0; j <= lastCol; j++) {
cellValue = rowValues[j];
if (cellValue) {
arrayThisRow.push(cellValue);
} else {
break;//Stop pushing values into the array as soon as a blank cell is found
}
}
}
Logger.log('arrayThisRow: ' + arrayThisRow)
if (arrayThisRow.length > 0) {
pastesheet.appendRow(arrayThisRow)
}
}