preload
Jun 04

Situation:
Imagine your customer has a bunch of Excel-files which he wanted to display on a website in html-form.

Problem:
What can you do here? You can write an importer to a database, or let the customer upload the excel file on a webserver and write a parser for the excel files etc. These are all non-trivial solutions for the problem that you want to easily display your information from the csv on a webpage into a table.

Solution:
I found a plugin called jquery csv2table. This is very simple to use. A CSV-file will be read asynchrounosly and a HTML-table will be created of it. So you can simply let your customer load his csv file up and the table will be created automatically. Optional support of sorting, highlighting etc. Link: http://plugins.jquery.com/project/csv2table

Example:

?View Code JAVASCRIPT
1
2
3
4
5
6
<div id="view1"></div>
<script type="text/javascript">
$(function(){
    $('#view1').csv2table('./data/Book12.csv');
});
</script>

This code will load the data from Book12.csv into the div “view1″.

Tagged with:
Apr 01

Why jquery or why use a js-lib at all?

There are many Javascript libraries on the market, e.g. script.aculo.us, jquery, dojo, Prototype and so on. For most of the tasks there is no special reason why jquery. It ist mostly a matter of actual knowledge of the developers to keep learning time low when there is a basic knowledge available. Some JS-libs have an easier handling e.g. of json than other, in detail using json in jquery is simpler than in prototype.

Why should you use a js-lib at all? There are several reasons:

Simpler syntax and less code

Imagine you want to modify every div-element which has class “payment” assigned and want to set the background color to “#cccccc” to these elements.
You have something like this:

1
2
3
4
5
6
<div id="list">
  <div class="payment">some text</div>
  <div>some text</div>
  <div class="payment">some text</div>
  <div class="payment">some text</div>
</div>

With normal JS you could do the job like this:

?View Code JAVASCRIPT
1
2
3
4
5
6
var list = document.getElementById('list');
for (var i = 0; i &lt; list.childNodes.length; i++) {
    if (list.childNodes.item(i).className == "payment") {
    	list.childNodes.item(i).style.backgroundColor = '#cccccc';
    }
}

Ok, six lines of code and moderate readable but what with all browser support? Umpf. And imagine this is a simple example.

With jquery you do:

?View Code JAVASCRIPT
1
$('div.payment').css('background-color' , '#cccccc');

And this runs in all browsers.

Better readability

Isn’t the example above better readable? You use css-selectors for selecting elements in javascript.

E.g. you want to remove the class from the div elements which are childs of the div with id “list”.

?View Code JAVASCRIPT
1
2
3
$('div#list div.payment').each(function() {
	$(this).removeClass('payment');
})

Better and easier extendability

You can simply create own plugins.

Multi-Browser compatibility

Nearly all functions will run in every browser.

There are often some cons why you should not use js-libs:

Big libray included

Yes it can be but e.g. with jquery you can use the compressed version for the live version. This will have only round about ~50k. And browser will cache the js file.

Higher loading times

Travering can be a costy thing, true. But with newer version it will become faster.

Conclusion

So I recommend using jquery because of the better readability, extendability and the separation of concerns.

Tip of the day:

Do not put JS-code inside html document when not needed. Use event-handling in outsourced js-file.

More infos about jquery you can find under http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/

Tagged with:
Feb 08

Imagine you have a pdf-file you want to make ocr-recognition. Take a scenario where you want to automatically let your linux pc do the job, e.g. in a folder.

I choose tesseract-ocr as ocr-programm. Easy to install and use and ok for my use. Unfortunately it takes only tif as input file type so that we have to convert the pdf to tif first.

To create a tif file with Ghostscript from pdf:

1
gs -q -r300 -dBATCH -dNOPAUSE -sDEVICE=tiff24nc  -sOutputFile=Dokument2.tif Dokument1.pdf


Now start OCR-recognition with tesseract-ocr (maybe you have to install it).

1
 tesseract Dokument2.tif doc.txt -l deu

-l deu means “Deutsch” for German language recognition.
-r300 means 300 DPI

Now it is easy to create a script which will automatically check a folder for new files and start ocr etc.

E.g. you can create a shell-script and start this shell script via cronjob. Maybe I will write an example here later.

Tagged with:
Nov 26

It is not possible in Outlook to send an email blind carbon copy to a special email address automatically. E.g. you would like to send a copy of each email you send to watcher@mymail.com automatically.

A small Visual Basic Script can do the job. Open Outlook 2007 and press Alt+F11 for the Visual Basic Editor and put there the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim objRecip As Recipient
Dim strMsg As String
Dim res As Integer
Dim strBcc As String
 
Dim objItem As MailItem
Dim myRecipients As Recipients
Dim myRecipient As Recipient
 
If Not ActiveInspector Is Nothing Then
If TypeOf ActiveInspector.CurrentItem Is MailItem Then
 
strBcc = "watcher@myemail.com"
End If
End If
 
On Error Resume Next
Set objRecip = Item.Recipients.Add(strBcc)
' handle case of user canceling Outlook security dialog
If Err = 287 Then
strMsg = "Could not add a Bcc recipient " &amp; _
"because the user said No to the security prompt." &amp; _
" Do you want still to send the message?"
res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
"Security Prompt Cancelled")
If res = vbNo Then
Cancel = True
Else
objRecip.Delete
End If
Err.Clear
Else
objRecip.Type = olBCC
objRecip.Resolve
 
End If
 
Set objRecip = Nothing
 
End Sub

Here is another version where you can filter the recipients and send mails to a special email address. E.g. you would like to send a bcc automatically to company1@mymail.com if you send mails to *@company1.com and to company2@mymail.com if you send mails to *@company2.com:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim objRecip As Recipient
Dim strMsg As String
Dim res As Integer
Dim strBcc As String
 
Dim objItem As MailItem
Dim myRecipients As Recipients
Dim myRecipient As Recipient
 
If Not ActiveInspector Is Nothing Then
If TypeOf ActiveInspector.CurrentItem Is MailItem Then
Set objItem = ActiveInspector.CurrentItem
Set myRecipients = objItem.Recipients
myRecipients.ResolveAll
For Each myRecipient In myRecipients
 
strBcc = ""
 
If InStr(myRecipient.Address, "company1.com") Then
strBcc = "company1@mymail.com"
End If
If InStr(myRecipient.Address, "company2.com") Then
strBcc = "company2@mymail.com"
End If
 
Next
End If
End If
 
On Error Resume Next
Set objRecip = Item.Recipients.Add(strBcc)
' handle case of user canceling Outlook security dialog
If Err = 287 Then
strMsg = "Could not add a Bcc recipient " &amp; _
"because the user said No to the security prompt." &amp; _
" Do you want still to send the message?"
res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
"Security Prompt Cancelled")
If res = vbNo Then
Cancel = True
Else
objRecip.Delete
End If
Err.Clear
Else
objRecip.Type = olBCC
objRecip.Resolve
 
End If
 
Set objRecip = Nothing
 
End Sub
Tagged with: