OWASP O2 Platform Blog

O2 Script: loading data from an Xslx (OpenXml) file

The next step for Sarah on her quest to create OWASP Summit Certificates (see Decomposing an Lamba method used in an O2 Script) is to consume the data she has on a Excel document.

O2 has native support for OpenXml (there is even an GUI to view the data), and here is the script that loads the file and returns a list with the full names (First name + Last name)

Func<List<string>> getNames =
    ()=>{
                var sourceFolder = @"C:\O2\_tempDir\Summit Certificates\".createDir();
                var file = sourceFolder.pathCombine("Global Summit Attendees.xlsx");
                var openXml = API_OpenXml.open_SpreadSheet(file);
                var names = new List<String>();
                foreach(var row in openXml.WorkSheets[0].Rows)
                    names.add(row.Cells[0] + " " + row.Cells[1]);
                return names;
          };

//O2File:API_OpenXml.cs

This can then be consumed like this

foreach(var name in getNames())
{
    "This is the full name: {0}".info(name);    // to see this you will need the LogView open
}

March 24, 2011 Posted by | Interoperability | Leave a comment

Decomposing an Lamba method used in an O2 Script

I just spent some time with Sarah Baso explaining how a particular script works (she is trying to automate the creation of pdfs with OWASP Summit certificates using O2 (similar with to O2 Script – Creating PDFs with OWASP AppSec Brazil Certificates ))

I used an etherpad during this session, which at the moment can be seen here: http://primarypad.com/jnfLB22s53

This is the function that loads up the file from the local computer and returns a list of names           

Func<string, List<string>> getNames =
     (dataFile) => {
                             return (from line in dataFile.fileContents().trim().split_onLines().remove(0)
                                     select line.split(",")[1].removeFirstChar().removeLastChar()
                                    ).toList();
                        };

Here is the same function shown above, but this time in a format that is probably be easy to read

Func<string, List<string>> getNames =
     (dataFile) => {
                             return (
                                         from line in dataFile.fileContents()
                                                                     .trim()
                                                                     .split_onLines()
                                                                     .remove(0)
                                         select line.split(",")[1]
                                                        .removeFirstChar()
                                                        .removeLastChar()
                                       ).toList();
                        };

And here is a ful rewrite of it , with the same functionality but writen in a very explicity way (and not taking advantage of the fact that most O2 Extension methods will return an object that can be used on the next extension method) 

Func<string, List<string>> getNames =
     (dataFile) => {
                             var fileContents = dataFile.fileContents();   // get file contents from disk
                             fileContents = fileContents.trim();              // remote extra spaces or enters
                             var lines = splitedContents. split_onLines();  // get a list splitted by line
                             splitedContents = splitedContents.remove(0); // remove the first entry which is the names of the columns
                             var results = new List<string>();
                             foreach(var line in lines)
                             {
                                    var cells = line.split(",");           // get a list splitted by ,  (comma)
                                    var name =  cells[0];
                                    name = name. removeFirstChar(); // Need to do this because the original data was padded with ' (single quotes)
                                    results = name. removeLastChar();  // if it were spaces I could had used ->   name = name.trim();                                    results.add(name);
                             }
                             return name;
                         }

March 24, 2011 Posted by | .NET, O2 Internals | 2 Comments