简体   繁体   English

将字符串匹配到数组

[英]Matching String to Array

I'm having trouble matching a string in an array. 我在匹配数组中的字符串时遇到麻烦。 Column B2:Lastrow is defined as array which is "ID". B2:Lastrow列定义为“ ID”数组。 I am trying to paste only unique entries to google sheet which aren't available in Column B2:Lastrow. 我正在尝试仅将唯一的条目粘贴到Google表格中,而这些条目在B2:Lastrow列中不可用。 Issue is..when I run the code it allows duplicates in the google sheet as well. 问题是..当我运行代码时,它也允许在Google工作表中重复。

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. 我通过工作表上的count公式使用它,但是这导致最大的代码运行时错误。因此,我将范围用作数组。 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. 实际:我胡扯,代码时间仍然相同。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM