Tuesday, February 15, 2011

SAP: How to get a Google Chart onto a Windows SAP GUI screen

A way to get a google chart onto a windows SAPGUI screen

Google chart api allows you to download charts for offline use. Using GuiXT, cURL and dos commands a chart can be shown on a windows SAPGUI screen.

Example below for SAP transaction db02, showing freespace of the database, plus freespace of the actual disk dedicated to the database. *in this example the unix system is checked for freespace of the */sapdata* filesystems. (Obviously all the white space is my attempt at hiding the real data)

The way to get the charts is with GuiXT and cURL

Again as with previous attempts to get google and yahoo static maps onto SAPGUI screen links here and here, then all the following is setup in a similar way.

All the following is found in c:\guixt check out the other links for more of the same.

To note the code below is cut and pasted into blogger and most of it is on one line and I have left a gap between each  line.

1) The GuiXT control script is called RSORA060.e0110.txt and relates to the DB02 transaction on our systems.

Set  V[db]   “&V[_database]"

Pushbutton (toolbar) "&V[_database] update charts"  "/0"

if V[_database=SID]

image (4,83)  "C:\guixt\img\chart&F[TD110-TSFRP].gif"  -NoBuffer  -Transparent  -Plain  exe="c:\guixt\ch_copy.bat &F[TD110-TSFRP]"  start="c:\guixt\ch_copy.bat &F[TD110-TSFRP]"

image (13,83)  "C:\guixt\img\chart&V[_database].gif"  -NoBuffer -Transparent  -Plain   exe="c:\guixt\chdisk_copy.bat ""&V[_database]"""  start="c:\guixt\chdisk_copy.bat ""&V[_database]"""


*Note in the code above SID needs to be changed to the system to be monitored. Each SID may have different scripts to check and create the charts.
To update the charts then the second chart is configured to use the GuiXT command start this enables the DOS BAT files to run. After the scripts complete then the pushbutton should be used to refresh the charts. The date is used in the chart to enable detection of out of date data :)

2) DOS script c:\guixt\ch_copy.bat generates the DB freespace google "GOM" chart. The dos bat file is below.


@del "C:\guixt\img\chart%1.gif"

cd "C:\guixt"

@del ".\img\chart%1.gif"

.\curlbin\curl -s -xPROXY:PORT  http://chart.apis.google.com/chart? -d chs=225x125 -d cht=gom -d chd=t:%1 -d chl=%1 -d chco=FF0000,00FF00 -d chtt=DB+FreeSpace -d chof=gif    -G > .\img\chart%1.gif


In the script above PUTTY command PLINK is used to connect to the unix system with ssh keys exchanged.

3) The second DOS script c:\guixt\ch_copy.bat generates the google chart for the free space in the unix system's sapdata filesystems


cd "C:\guixt"

del ".\img\chart%1.gif"

rem %% required for batch % ok for command line

for /f %%A in ('plink -l USERID HOST "df -k|grep sapdata |awk '{n=n+$2}{s=s+$4} END{print s*100/n}'"') DO ( SET DISK=%%A )

echo %DISK%

.\curlbin\curl -s -xPROXY:PORT  http://chart.apis.google.com/chart? -d chs=225x125 -d cht=gom -d chd=t:%

DISK% -d chl=%DISK% -d chco=FF0000,00FF00 -d chtt=%DATE%+Disk+FreeSpace -d chof=gif  -G >.\img\chart%1.gif

*Note in both of the DOS BAT files then the PROXY:PORT settings need to be adapted to the correct settings.

Monday, February 14, 2011

MAC: Creating a basic barcode web service with php

A new part time project for me is to  learn and use Apache and PHP.

First project is to generate barcodes from supplied url parameters.

Here is version 0.1, with no error checking or user input validation - maybe in the next version.
My Mac = Imac running snow leopard 10.6.6

Get Barcode Writer in Pure Postscript here.

1) Install Xcode and MacPorts

Links to both can be found here http://www.macports.org/install.php

Then install both ghostscript and imagemagick with macports.

2) Install ghostscript
sudo port install ghostscript

check its installed with the command gs -h

3) Install imagemagick

sudo port install imagemagick

check its installed with the command convert -h

4) Apache activate PHP and start Apache

uncomment out the php5_module line.

cd /etc/apache2
sudo vi httpd.conf

LoadModule alias_module libexec/apache2/mod_alias.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule bonjour_module     libexec/apache2/mod_bonjour.so
LoadModule php5_module        libexec/apache2/libphp5.so
#LoadModule fastcgi_module     libexec/apache2/mod_fastcgi.so

Start Apache web server - system preferences - sharing - web sharing

5) php code (really simple :) run a bash script to generate the barcodes from the supplied url parameters.
I called the php c.php





//echo $bt;

system("/Users/robert/Sites/barc.bash $bc $bt");


//echo $_GET["texto"];

echo "<img src=\"$barcpath\">";


6) BASH script (really simple :) no error checking nor options for the barcodes at the moment so text etc will be checked in the future.
I called the bash script barc.bash



#build postscript code

echo "gsave" >/Users/robert/Sites/www/hello1.ps

echo "50 50 translate" >>/Users/robert/Sites/www/hello1.ps

echo "2 2 scale" >>/Users/robert/Sites/www/hello1.ps

echo "50 50 moveto (${*}) () /${BC} /uk.co.terryburton.bwipp findresource exec" >> /Users/robert/Sites/www/hello1.ps

echo "grestore" >> /Users/robert/Sites/www/hello1.ps

echo "showpage" >> /Users/robert/Sites/www/hello1.ps

#run ghostscript to generate EPS

/opt/local/bin/gs -dBATCH -dNOPAUSE -q -sDEVICE=epswrite -sOutputFile=/Users/robert/Sites/www/$$.eps /Users/robert/Sites/www/barcode.ps /Users/robert/Sites/www/hello1.ps

#imagemagick to convert from EPS to jpg

/opt/local/bin/convert -density 72 /Users/robert/Sites/www/$$.eps /Users/robert/Sites/www/$$.jpg

#copy the file to barcode.jpg

cp /Users/robert/Sites/www/$$.jpg /Users/robert/Sites/www/barcode.jpg

7) Before running create www directory in the users home directory, here the home dir is ~robert

mkdir www
cd www

move/copy barcode.ps to the www directory.

change the owner and group of barcode.ps and www directory
sudo chown _www:_www barcode.ps
himbleton:Sites robert$ ls -ld www
drwxr-xr-x  49 _www  _www  1666 Feb 14 23:26 www


Sunday, February 13, 2011

SAP: generate images for Excel with BSPs

Following on from the post "More Barcodes with Barcode Writer in Pure Postscript"

This post covers a straight forward BSP and a simple Excel Macro to generate barcodes. By straight forward and simple I mean that I dont go into any error checking :) Also as I have covered Yahoo maps previously then its a case of changing the Excel Macro slightly to pick up the Yahoo static map images, however as Yahoo terms of use are involved then its only a demo. The barcodes covered first are free though :).

First the barcode related BSP.

Goto Se80 and create a BSP called ZBWIPP

Create a page bc.htm with the following

Attributes, barcodes, options and text as shown below.

The Event Handler "OnInitialization" code takes the url parameters and generates the barcodes. See the "More Barcodes with Barcode Writer in Pure Postscript" post for the requirements for ghostscript and imagemagick. The external commands line CALL FUNCTION 'SXPG_COMMAND_EXECUTE' uses the same code in the SDN blog for the script ZBWIPP. Also all the scripts and barcode.ps code are in the work directory of my SAP demo 702 server.

navigation->set_parameter( 'text' ).

navigation->set_parameter( 'barcode' ).

navigation->set_parameter( 'options' ).

*DATA bitmap TYPE REF TO zcl_abap_bitmap.

DATA okcode TYPE syucomm.

DATA filed type string.

DATA: t_result         TYPE STANDARD TABLE OF btcxpm.

DATA psX type string.

DATA psY type string.

data scaleX type i.

data scaleY type i.

data bit TYPE  XSTRING.

data display_url type string.

*the scale values could be placed in the excel call also, not done it yet

scaleX = 2.

scaleY = 2.

data  p_name type string.

if text is initial.

  text = 'no text detected'.


if barcode is initial.

barcode = 'datamatrix'.


psX = scaleX.

psY = scaleY.

DATA: psnm  TYPE string VALUE 'f:\usr\sap\NSP\DVEBMGS00\work\zsapbarcode.ps'.

DATA: barc  TYPE string VALUE 'f:\usr\sap\NSP\DVEBMGS00\work\rob.bmp'.

DATA: TEXT1(255),


      LENG TYPE I.

* 0 moveto (2310844"Japan") () /qrcode /uk.co.terryburton.bwipp findresource exec


*concatenate '50 50 moveto (' text  ') (' options ') /' barcode ' /uk.co.terryburton.bwipp findresource exec' into TEXT1.

concatenate '50 50 moveto (' text ') (' options ') /' barcode ' /uk.co.terryburton.bwipp findresource exec' into TEXT1.

concatenate psX pSY  ' scale' into TEXT2.


*RANSFER '50 50 moveto' to psnm.

TRANSFER 'gsave' to psnm.

TRANSFER '50 50 translate' to psnm.



TRANSFER 'grestore' to psnm.

TRANSFER 'showpage' to psnm.




    commandname                   = 'ZBWIPP'

*   additional_parameters         = v_dir_input

*   operatingsystem               = c_oper


    exec_protocol                 = t_result


    no_permission                 = 1

    command_not_found             = 2

    parameters_too_long           = 3

    security_risk                 = 4

    wrong_check_call_interface    = 5

    program_start_error           = 6

    program_termination_error     = 7

    x_error                       = 8

    parameter_expected            = 9

    too_many_parameters           = 10

    illegal_command               = 11

    wrong_asynchronous_parameters = 12

    cant_enq_tbtco_entry          = 13

    jobcount_generation_error     = 14

    OTHERS                        = 15.




IF  XSTRLEN( bit ) > 0.

  DATA: cached_response TYPE REF TO if_http_response.

  CREATE OBJECT cached_response TYPE CL_HTTP_RESPONSE EXPORTING add_c_msg = 1.

cached_response->set_data( bit ).

  cached_response->set_header_field( name  = if_http_header_fields=>content_type

                                      value = 'image/bmp' ).

    cached_response->set_status( code = 200 reason = 'OK' ).

    cached_response->server_cache_expire_rel( expires_rel = 180  ).

    DATA: guid TYPE guid_32.


    CONCATENATE runtime->application_url '/' guid '.bmp' INTO display_url.

    cl_http_server=>server_cache_upload( url      = display_url

                                         response = cached_response ).

***response->redirect( display_url ).

response->redirect( display_url ).


  response->redirect( 'http://himble.test.com:8000/sap/bc/bsp/sap/zbwipp/zbwipperror.bmp' ).


The excel macro is really simple in that it uses the current selection and uses offsets to pick up the required barcode, options and text. So for example the required barcode would be in column A, the options in column B and the text for the barcode would be in column C. The range to select would be in column E, my choice.

Sub zbwipp()

    Dim barcode As String

    Dim text As String

    Dim options As String

For Each Cell In Selection


    barcode = Cell.Offset(0, -4).Value

    options = Cell.Offset(0, -3).Value

    text = Cell.Offset(0, -2).Value

'    MsgBox barcode

    ActiveSheet.Pictures.Insert( _

        "http://himble.test.com:8000/sap/bc/bsp/sap/zbwipp/bc.htm?barcode=" + barcode + "&options=" + options + "&text=" + text).Select

Next Cell

End Sub

The result in a YouTube Video 

Yahoo Static Map Images

If the Excel command  ActiveSheet.Pictures.Insert is used to point to the Yahoo BSP in this post then static maps can be generated as shown below. The same location is used in my home town of Birmingham and the width of the image is changed and the static maps generated.

Google +