1

I'm having trouble matching a string in an array. Column B2:Lastrow is defined as array which is "ID". I am trying to paste only unique entries to google sheet which aren't available in Column B2:Lastrow. Issue is..when I run the code it allows duplicates in the google sheet as well.

I was using it through count formula on the sheet but that leads to maximum code runtime error..hence I'm using the range as an array. Solves the error but not able to recognize if the string is unique.

// Code: List Gmail Label to Google Sheet and save attachment to GDrive
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Summary');
var label = GmailApp.getUserLabelByName("Caterpiller Account");
var threads = label.getThreads();

function getEmails() {

    for (var i = 0; i < threads.length; i++) {
        var row = sheet.getLastRow() + 1;
        var message = threads[i].getMessages()[0];
        var ID   = message.getId();
        var fulldata = sheet.getRange('B2:B' + row).getValues();

        if (fulldata.indexOf(ID) == -1) {
            var messages=threads[i].getMessages();
            var listID=threads[i].getPermalink();
            var listdate=threads[i].getLastMessageDate();
            var message = threads[i].getMessages()[0];
            var attachment = message.getAttachments();
            var attachmentBlob = message.getAttachments()[0].copyBlob();
            var folder = DriveApp.getFolderById("1ilsecZOexqTWGfAMu5xJDx1pKh3z1US-");

            // EXTRACTOR CODE:
            for (var m=0; m < messages.length; m++) {
                sheet.getRange(row,1).setValue(messages[m].getSubject());
                sheet.getRange(row,2).setValue(ID);  
                sheet.getRange(row,3).setValue(listdate); // Value - Date

                for (var z=0; z<attachment.length; z++) {
                    var file = DriveApp.getFolderById("1ilsecZOexqTWGfAMu5xJDx1pKh3z1US-").createFile(attachmentBlob);
                    //Pending: Weblinkview (basically get permanent url of file) / Or self developed function that gets file through description (where description is email ID)
                }
                row++;
            }
        }
    }
}

Expected: Unique entries & a faster Code runtime. Actual: I'm crap & code time is still the same.

lupz
  • 3,620
  • 2
  • 27
  • 43

1 Answers1

-1
bool IsSame(string str,char arr[100])
{
    if(str.lenght!=strlen(arr))return false;
    for(int i=0;i<str.lenght;i++)
    {
        if(str[i]!=arr[i]) return false;
    }
    return true;
}
Marten
  • 1,376
  • 5
  • 28
  • 49