Interesting pieces of code

Post your Gambas programming questions here.
Post Reply
monteiro
Posts: 10
Joined: Monday 26th September 2016 11:40am

Interesting pieces of code

Post by monteiro »

Ok, I know some of you have some piece of code or ideas that is/was really a problem solver. Let's see some!
CREATING EDITABLE DOCUMENTS FOR AN OFFICE
Problem: The office produces some huge reports using LibreOffice. To much text to type!
Solution: These reports are water sample analysis.
1) The lab feeds the results into a postgresql database.
2) When finished the work on the water sample by the lab, an office software alerts its user.
3) This software has a odt report template, which is copied to another directory for edition and uncompressed there.
4) All the data is in the content.xml file. So, we could use a script to edit some placeholders we have in our template. Here is an example:

Code: Select all

#!/bin/bash
cd "$1"
sed -i "s~$2~$3~" content.xml
5) After editing, we can compress the files again, rename it as ".odt" e save it to a FTP server. Here is an example of the FTP transaction:

Code: Select all

#!/bin/bash
ftp -n <<EOF
open ftp://IP
user name password
put $1
EOF
Once in the FTP server, the file is available to the customer.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Interesting pieces of code

Post by cogier »

Well I was pleased with this: -

Code: Select all

Public Sub Form_Open()
Dim sTemp As String = "This              is     a          very                  badly                         spaced                                         string"
Dim sCheck As String

Repeat 
  sCheck = sTemp
  Print sTemp
  sTemp = Replace(sTemp, "  ", " ")
Until sCheck = sTemp

sTemp = Replace(sTemp, " is", " WAS")
Print "\n" & sTemp

End
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Interesting pieces of code

Post by cogier »

This is another piece of code I liked. I had a database stored in a .csv file but I wanted to display the contents in a particular order. This code is not exactly as used but simplified to show the principle.

Code: Select all

Public Sub Form_Open()
Dim sInputString As String[] = ["One ", "Three ", "Zero ", "Five", "Four ", "Two "]
Dim sOrder As Short[] = [2, 0, 5, 1, 4, 3]
Dim siCount As Short

For sicount = 0 To 5
  Print sInputString[sOrder[siCount]];
Next

End
monteiro
Posts: 10
Joined: Monday 26th September 2016 11:40am

Re: Interesting pieces of code

Post by monteiro »

This one is useful to export html or php page directly to pdf from firefox command line:
1 - Install cmdlnprint complement for firefox
From your code:
2 - Shell "firefox -print " & URL & " -print-mode pdf -print-file " & /file_path/file_name.pdf

A software I wrote creates a bank slip using php. The document is saved as pdf in order to be sent to the customer by email.
Post Reply