Archive for the ‘Report Scripting’ Category

C#, Visual Basic,

May 21, 2012 Leave a comment

You can choose which language you want to use when you create scripts for your reports.  C#, Visual Basic, and are all available choices.   Choose the language from the report properties window.

Directory List Report

May 18, 2012 Leave a comment

Here is a sample report with a script that will list the contents of a folder on a hard drive.

First start with a blank report.   It will be unbound (no data source).   The data source will be created in a script.   On the report add a table and name each cell as follows:   Add a second table in the report header to name the columns.   And then finally add a label control for a title.

Now for the script that will run this report.

You set the variable “directory” to the folder you want to get a file list of.    You can modify this so it is a report parameter, but to keep things simple now, just hardcode the folder.   The rest of the script creates an array with file information and assigns the array to the datasource of the report.   Then each cell in the table needs to be bound to an element of the array.   Finally the title is set to the string variable “directory”.

The resulting report looks like this when previewed:

You can download this sample report HERE.

Tag Property

May 11, 2012 Leave a comment

Within the Sunset Report’s report designer, each object has a “Tag” property.   This property allows you to add some unique information that can be accessed via scripts.   This could be an id, or actually a data field if the report has a data source.   With this tag property all kinds of possibilities exist with scripts.

Unbound Report with Scripts

May 10, 2012 Leave a comment

Here is an example of what you can do with scripts in a report.   First start with a blank report design with no data source.    Drag a chart object onto the design surface as shown in the figure.

Next add the following code in the scripts tab….

'Define a class to represent an individual record with properties to be data source fields
Public Class Record
  Dim _id As Integer
  Dim _txt As String
  Dim _num as double
  Public Sub New(ByVal id As Integer, ByVal txt As String, ByVal num As Double)
    Me._id = id
    Me._txt = txt
    Me._num = num
  End Sub
  Public Property ID() As Integer
          Return _id
       End Get
       Set(ByVal Value As Integer)
          _id = Value
       End Set
   End Property
   Public Property Txt() As String
          Return _txt
       End Get
       Set(ByVal Value As String)
          _txt = Value
       End Set
    End Property
   Public Property Num() As Integer
          Return _num
       End Get
       Set(ByVal Value As Integer)
          _num = Value
       End Set
    End Property
 End Class

'Populate the chart before printing Private Sub chart1_BeforePrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
     ' Create a list.
     Dim listDataSource As New ArrayList()
    ' Populate the list with records.
     listDataSource.Add(New Record(1, "Android", 29))
     listDataSource.Add(New Record(2, "Galaxy", 32))
     listDataSource.Add(New Record(3, "Lumia", 51))
     listDataSource.Add(New Record(4, "iPhone", 84))
    ' Remove the default series if any
     for i as integer = 0 to chart1.series.count-1
       dim serie as series = chart1.series(i)
     next i
     ' Bind the chart to the list.
     chart1.DataSource = listDataSource
    ' Create a series, and add it to the chart.
     Dim series1 As New Series("My Series", ViewType.Bar)
    ' Adjust the series data members.
     series1.ArgumentDataMember = "txt"
     series1.ValueDataMembers.AddRange(New String() { "num" })
    ' Access the view-type-specific options of the series.
     CType(series1.View, BarSeriesView).ColorEach = True 
     series1.LegendPointOptions.Pattern = "{A}"
End Sub

Now preview the report and you will  get the following:

Sample Report available HERE to download.

Report Scripts

April 15, 2012 Leave a comment

When designing a report you can use scripts to handle special requirments. Scripts are program commands, placed within the event handlers of the report, band, and control elements. When the corresponding event occurs, the script code runs.  Events occur when the report is previewed, printed, or exported.

Although when in the Report Designer, virtually any task can be accomplished without scripting (Conditionally Change a Control’s Appearance, Conditionally Change a Label’s Text and Conditionally Hide Bands), scripting is made available to extend the standard functionality even more.  The Scripts Tab allows you to manage your report’s scripts. It provides centralized access to all the scripts being written for a report or any of its elements (bands and controls).  The built-in scripts validation capability is provided in this tab. After clicking Validate, the result is displayed in the Scripts Errors Panel.  Scripts are saved to a file along with the report’s layout.

The report scripts may be written in one of the following languages that the .NET framework supports – C#, Visual Basic and J#. Since J# is not installed with the framework installation, by default, make sure it is present before writing code in it. The scripting language is specified via the Script Language property of the Report object.

Script execution is performed in the following way: The report engine generates a temporary class in memory. The names of the variables are defined by the Name properties of the controls and objects they represent. When the script is preprocessed, its namespace directives are cut from the script code and added to the namespace, where the temporary class is defined. After preprocessing, all scripts are placed in the code of the temporary class. Then, the resulting class is compiled in memory, and its methods are called when events occur. Scripting offers many advantages: you can declare classes (they will become inner classes), variables, methods, etc. A variable declared in one script is accessible in another script, because it is, in fact, a variable of the temporary class.

The Script References property of the Report object specifies the full paths (including the file names) to the assemblies that are referenced in the scripts used in a report. These paths should be specified for all the assemblies that are included in scripts via the using (C#), Imports (Visual Basic) or import (J#) directives. Usually, you don’t need include any assembles, because most standard assemblies that you may require in scripts are already referenced by the program.