Search in list and tables via C# macro

darkdiver

Administrator
Команда форума
Регистрация
13.01.2009
Сообщения
2 271
Благодарностей
2 652
Баллы
113
These samples can be useful solving common tasks of searching in lists and tables


Search inlists:


Task 1.
There is a list, where we need find all elements that mach regular expression and put them to another list.
You can use this C# macro for that (in the attached project, we go through all domains from the list and select ones that contain digitals)
JavaScript:
// prepare regular expression for parsing
var parserRegexPattern = project.Variables["listSearchRegex"].Value;
var parserRegex = new System.Text.RegularExpressions.Regex(parserRegexPattern);
// get a list for searching
var sourceList = project.Lists["SourceList"];
// get result list
var destList = project.Lists["OutputList"];
// search in each line of the list 
lock(SyncObjects.ListSyncer)
{
    for(int i=0; i < sourceList.Count; i++)
    {
        // take one line from the list
        var str = sourceList[i];
        // check the line on matches by regex, if there are matches we put it to result list
        if (parserRegex.IsMatch(str))
        {
            destList.Add(str);        
        }
    }
}

Task 2.
There is a list, where we need check a text presence
It can be solved using this C# macro (in the attached project, we go through all domains from the list and check if domain contains ".jp")
JavaScript:
// take search text from variable 
var textContains = project.Variables["listSearchTextContains"].Value;
// get a list or search
var sourceList = project.Lists["SourceList"];
// search in each line of list
lock(SyncObjects.ListSyncer)
{
    for(int i=0; i < sourceList.Count; i++)
    {
        // get line from list
        var str = sourceList[i];
        // check if line contains text, if there are matches return "yes"
        if (str.Contains(textContains))
            return "yes";
    }
}
// ifnothing found return "no"
return "no";



Search in tables:


Task 1
There is a table, we ned select rows, that suit search criteria and put them to another table.
It can be solved using this C# macro (in the attached project, we are searching for people whose jobs related with sales)
JavaScript:
// take regex for parsing from a variable
var parserRegexPattern = project.Variables["tableSearchRegex"].Value;
var parserRegex = new System.Text.RegularExpressions.Regex(parserRegexPattern);
// get a table where we search
var sourceTable = project.Tables["SourceTable"];
// get result table
var destTable = project.Tables["OutputTable"];
// search in each row
lock(SyncObjects.TableSyncer)
{
    for(int i=0; i < sourceTable.RowCount; i++)
    {
        // read a row (array of cells)
        var cells = sourceTable.GetRow(i).ToArray();
        // check the second row if it matches a regex, if there are matches we put it to result table
        if (parserRegex.IsMatch(cells[1]))
            destTable.AddRow(cells);
    }
}

Task 2
We need to check if there is a cell that contains a specific text.
It can be solved using this C# macro (in the attached project, we are searching a cell that contains "Mexico")
JavaScript:
// take text for search from a variable
var textContains = project.Variables["tableSearchTextContains"].Value;
// get table for search
var sourceTable = project.Tables["SourceTable"];
// search in each row
lock(SyncObjects.TableSyncer)
{
    for(int i=0; i < sourceTable.RowCount; i++)
    {
        // read a row (array of cells)
        var cells = sourceTable.GetRow(i).ToArray();
        // loop through all cells
        for (int j=0; j < cells.Length; j++)
        {
            // check if cell contains specified text, if there are matches return "yes"
            if (cells[j].Contains(textContains))
                return "yes";
        }
    }
}
// if nothing found return "no"
return "no";
 

Вложения

convict29

Пользователь
Регистрация
10.07.2014
Сообщения
113
Благодарностей
1
Баллы
18
Thanks a lot!
 
Последнее редактирование:

raphael

Client
Регистрация
20.07.2012
Сообщения
61
Благодарностей
4
Баллы
8
Hi,

seems not working if we have this to search and that is the exact match

20.10.18.00

can it be a problem with "." in the search ?

Thanks in advance
 

awzse00

Client
Регистрация
16.03.2013
Сообщения
169
Благодарностей
13
Баллы
18
are these templates still working guys ? Because i'm trying to make them work for about 2 hours and i'm getting nowhere :-)
 

awzse00

Client
Регистрация
16.03.2013
Сообщения
169
Благодарностей
13
Баллы
18
It give me a YES result no matter what, the list or table is empty and he still gets a YES either way, with the name there or the table/list emtpy
 

raphael

Client
Регистрация
20.07.2012
Сообщения
61
Благодарностей
4
Баллы
8
Hi all,

the script work always good but im trying to find a part of a content:

row contain
JAMBPR9906-Uni-Green-S
and i try to search
JAMBPR9906.* in regex

but seems not working

Thanks in advance for your help
 

raphael

Client
Регистрация
20.07.2012
Сообщения
61
Благодарностей
4
Баллы
8
Sry after many test it not search in column 1 of the table
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)