Script Utilities

<< <%SKIN-STRTRANS-SYNTOC%> >>

Navigation:  Scripting >

Script Utilities

All scripts have access to a ScriptUtils class through the script arguments. This class contains the following utility functions:

Function

Description

DataTable LoadCsvFile(string path)

Loads a CSV file from a specified location and returns the data in a DataTable.

DataTable LoadCsvFile(string path, char separator)

Loads a CSV file from a specified location using a specified value separator and returns the data in a DataTable.

DataTable LoadCsvFileFromDefaultInputFolder(string filename)

Loads a CSV file with a specified name from the default input data folder and returns the data in a DataTable.

DataTable LoadCsvFileFromDefaultInputFolder(string filename, char separator)

Loads a CSV file with a specified name from the default input data folder using a specified value separator and returns the data in a DataTable.

void ExecuteCommandLine(string programFilePath, string inputFilePath, string outputFilePath, string options)

Executes a program on the command line. The parameters inputFilePath, outputFilePath and options are added as command line parameters.

void ExecuteCommandLine(string programFilePath, string arguments)

Executes a program on the command line with the specified command line parameters.

int ClearBrowserCookies(string url)

Clears all Internet Explorer cookies on the computer associated with the specified URL.

void ResetBrowserSession()

Clears the Internet Explorer session cookies. This is equivalent to restarting an Internet Explorer browser.

string PostData(string url, string postData, int timeout = 0)

Posts data to a web server.

string PostData(string url, byte[] postData, int timeout = 0)

Posts data to a web server.

string PostData(string url, string postData, string headers, int timeout = 0)

Posts data to a web server.

string PostData(string url, byte[] postData, string headers, int timeout = 0)

Posts data to a web server.

string GetData(string url, int timeout = 0)

Gets data from a web server.

string GetData(string url, string headers, int timeout = 0)

Gets data from a web server.

string TransformContent(string input, string regexScript)

Transforms data with a Content Grabber regex script. The regex script must follow the exact format of a Content Grabber transformation script. Use \r\n for line breaks.

 

Example

The following example loads data from a CSV file located in the agent's default input folder:

 

using System;

using System.Data;

using Sequentum.ContentGrabber.Api;

public class Script

{        

 public static DataTable ProvideData(DataProviderArguments args)

 {

         DataTable data = args.ScriptUtilities.LoadCsvFileFromDefaultInputFolder("inputData.scv");

         return data;

 }

}

 

Database Utilities

All scripts have access to the following function through the script arguments:

 

public IConnection GetDatabaseConnection(string connectionName)

 

The function returns a predefined database connection. See the topic Database Connections for more information about predefined database connections.

 

The IConnection, ICommand and IReader interfaces are Content Grabber interfaces that are meant to make it easier to write and read data to a database, but you can always use the standard .NET libraries if you prefer.

 

The following example writes some extracted data to a database:

 

using System;

using Sequentum.ContentGrabber.Api;

using Sequentum.ContentGrabber.Commands;

public class Script

{        

 public static CustomScriptReturn CustomScript(CustomScriptArguments args)

 {

         IConnection connection = args.GetDatabaseConnection("exportDatabase");

         connection.OpenDatabase();

         try

         {

                 ICommand command = connection.GetNewCommand();

                 command.SetSql("insert into export_table values 

                         (@title, @description)");                

                 command.AddParameterWithValue("title", args.DataRow["title"], CaptureDataType.ShortText);

                 command.AddParameterWithValue("description", 

                         args.DataRow["description"], CaptureDataType.ShortText);

                 command.ExecuteNonQuery();

         }

         finally

         {

                 connection.CloseDatabase();

         }                

         return CustomScriptReturn.Empty();

 }

}

 

The IConnection interface has the following functions and properties:

Function or Property

Description

IReader GetNewReader(string sql)

Returns an IReader interface to a new Reader object that uses the current connection.

ICommand GetNewCommand()

Returns an ICommand interface to a new Command object that uses the current connection.

void OpenDatabase()

Opens the database connection.

void CloseDatabase()

Closes the database connection.

object GetConnection()

Return the underlying database connection that is specific to the database type used. For example, if the database is a MySQL database, the object returned is a MySqlConnection.

void ExecuteNonQuery(string sql)

Executes a SQL statement that doesn't return a result set.

object ExecuteScalar(string sql)

Executes a SQL statement that returns a single scalar value.

long ExecuteCount(string sql)

Executes a SQL statement that returns a single long value. This function should be used when using the SQL function "Count".

bool HasTable(string tableName)

Return true if the specified table exists in the database.

bool HasColumn(string tableName, string columnName)

Return true if the specified columns exists in the specified table.

DataRow[] GetTableColumns(string tableName)

Return all columns in the specified database table.

void DropTable(string tableName)

Drops the specified database table.

void TruncateTable(string tableName)

Truncates the specified database table.

void Lock()

Locks the database connection, so it cannot be used by any other running threads.

void Release()

Releases a lock on the database connection.

IReader ExecuteReader(string sql, params object[] pars)

Executes a SQL with a list of SQL parameters and returns a IReader.

DataTable ExecuteDataTale(string sql, params object[] pars)

Executes a SQL with a list of SQL parameters and returns a DataTable.

void ExecuteNonQuery(string sql, params object[] pars)

Executes a SQL with a list of SQL parameters that doesn't return a result.

object ExecuteScalar(string sql, params object[] pars)

Executes a SQL with a list of SQL parameters that returns a single scalar value.

long ExecuteCount(string sql, params object[] pars)

Executes a SQL with a list of SQL parameters that returns a single long value. This function should be used when using the SQL function "Count".

 

The ICommand interface has the following functions and properties:

Function or Property

Description

void AddParameterWithValue(string pName, object pValue, CaptureDataType type)

Adds a SQL parameter to the Command object. A matching parameter must exist in the associated SQL statement. The value of a parameter is updated if the parameter already exists.

void AddParameter(string pName, CaptureDataType type)

Adds a SQL parameter to the Command object. A matching parameter must exist in the associated SQL statement. The value of a parameter is updated if the parameter already exists.

void SetParameterValue(string pName, object pValue)

Sets the value of an existing SQL parameter. A matching parameter must exist in the associated SQL statement.

void ExecuteNonQuery(string sql)

Executes a SQL statement that doesn't return a result set.

void ExecuteNonQuery()

Executes an existing SQL statement that doesn't return a result set.

object ExecuteScalar(string sql)

Executes a SQL statement that returns a single scalar value.

object ExecuteScalar()

Executes an existing SQL statement that returns a single scalar value.

long ExecuteCount(string sql)

Executes a SQL statement that returns a single long value. This function should be used when using the SQL function "Count".

long ExecuteCount()

Executes an existing SQL statement that returns a single long value. This function should be used when using the SQL function "Count".

void SetSql(string sql)

Set the SQL statement associated with the SQL command.

 

The IReader interface has the following functions and properties:

Function or Property

Description

void SetSql(string sql)

Sets the SQL statement associated with this data reader.

void AddParameterWithValue(string pName, object pValue)

Adds a SQL parameter to the Command object. A matching parameter must exist in the associated SQL statement. The value of a parameter is updated if the parameter already exists.

bool Read()

Reads a row of data. Executes an existing SQL statement if it has not already been executed.

void Close()

Closes the data reader.

DateTime GetDateTimeValue(int columnIndex)

Gets a DateTime value from the current row.

string GetStringValue(int columnIndex)

Gets a String value from the current row.

int GetIntValue(int columnIndex)

Gets a Integer value from the current row.

Guid GetGuidValue(int columnIndex)

Gets a Guid value from the current row.

Type GetFieldType(int columnIndex)

Gets the data type of a specified column.

IDataReader GetDataReader()

Returns the underlying .NET data reader.

object GetFieldValue(int columnIndex)

Gets a data value from the current data row.

 

Extension Methods

Content Grabber provides a few extension methods that can be used in all scripts:

Function

Description

static DataTable ToDataTable(this string stringValue, string columnName)

Converts a single string value into a DataTable with one data column and one data row.

static DataTable ToDataTable(this string[] dataRows, string columnName)

Converts an array of strings into a DataTable with one data column and one data row for each string value.

static DataTable ToDataTable(this string[] dataRows, string columnName, string stringFormat)

Converts an array of strings into a DataTable with one data column and one data row for each string value. A standard .NET format string can be used to format the string value before it is inserted into the DataTable.

static DataTable ToDataTable(this List<string> dataRows, string columnName)

Converts a list of strings into a DataTable with one data column and one data row for each string value.

static DataTable ToDataTable(this List<string> dataRows, string columnName, string stringFormat)

Converts a list of strings into a DataTable with one data column and one data row for each string value. A standard .NET format string can be used to format the string value before it is inserted into the DataTable.

Example

The following script generates a list of URLs and uses an extension method to convert the list of URLs into a DataTable.

 

using System;

using System.Collections.Generic;

using System.Data;

using Sequentum.ContentGrabber.Api;

public class Script

{        

 public static DataTable ProvideData(DataProviderArguments args)

 {

     List<string> urls = new List<string>();

         for(int i=1;i<1000;i++)

     {

         urls.Add("http://www.domain.com/page.php?ID=" +i.ToString());

     }

     return urls.ToDataTable("url");

 }

}