SpaceDesign Custom Report Rows

Here is a sample report with VAT added:
(Ignore the $ signs - the currency signs, and decimal point symbol are determined by your Windows settings)
The Total row was added by the report. The VAT and Total Cost rows were added by the ruby script, based on the calculated total cost.

SpaceDesign Custom Report Rows

By adding some ruby code to your report definition, you can perform calculations based on totals and add additional rows to the report.

Calculations

See the Default Ruby Code below for examples.

Total Values

You can set a ruby variable equal to a value on the totals row, using the name of the column. (not the description). e.g.:

total_cost = total_row["extended_cost"]  # replace "extended_cost" with the name of your extended_cost column

To make sure that it is treated as a floating point value, add: .to_f (this helps if you have misspelled the column name.)


total_cost = total_cost.to_f # make sure this has a value if nil

Calculated values

You can calculate new values, based on the total values:

vat = total_cost * 0.175

Formatting values

You can use the ruby command sprintf(sformat, value) to format numbers, e.g. (to display a value with 2 decimal places)

svalue = sprintf("%.2f", vat) # format to two decimal places

There is also a command to add currency symbols and international formatting for decimal points:

svalue2 = dll_call_format_currency(svalue) # add currency formatting

Adding report rows

You add a report row by giving it a description, ( passed to start_custom_row() ), and by setting values in the columns you want to set.

The basic format for this is:

custom_row = start_custom_row("Description")
custom_row["extended_cost"] = svalue
add_custom_row

A similar format can be used to add divider rows:

custom_row = start_custom_row("----------") # start a new custom row with dashes in the description line
custom_row["extended_cost"] = "----------" # add a divider line in extended_cost column
add_custom_row

More Help

if you add trace commands to the ruby code, they will display on the ruby console when the report is generated. This can be very helpful while debugging:

trace("TOTAL COST: %s", total_cost)
trace("VAT: %s", svalue2) # display on ruby console

For otther help, add a discussion to our Help Forum.

Modifying the Ruby Script

Custom report setup.jpg

The custom cuby script is saved in your SketchUp model. If you want to make changes to it, you can save a copy on disk, edit it, and reload it into the model.

  • Save or edit ruby data - after clicking this button, the location of the saved ruby data (in a .txt file), will be displayed and you can edit it in a ASCII editor.

    Edit ruby script.jpg
  • Load edited Ruby data - after editing the ruby script, you need to reload it and save it back into the model.
  • Reset to default ruby data - This resets the ruby script saved in the SketchUp model to the default Default Ruby Code.


Errors in the ruby script

Here I have made an intentional error in the ruby script.

In the line after getting the total cost, I have calculated a salesman's commission, but misspelled commission on the next line when calculating the new_total.

#get total_cost
total_cost = total_row["extended_cost"]  # replace "extended_cost" with the name of your extended_cost column
total_cost = total_cost.to_f # make sure this has a value if nil
trace("TOTAL COST: %s", total_cost)        
#there is an error in the second line below
commission = total_cost * 0.25
new_total = total_cost + comision # misspelled

Error messages

When running the report, I get an error message like this:

Note: that the line number is displayed in the error to help you find it and fix it.

Spdes error1.png

Ruby Console

Also, if you open the Ruby Console, before starting you report, the traces (e.g. :

trace("TOTAL COST: %s", total_cost)

will be displayed and the error message will be reported on the ruby console.

Here is some of the ruby console output for this script:

ADD CUSTOM ROWS
TOTAL COST: 0.0
ERROR during add_custom_rows: undefined local variable or method `comision' for #<SpaceDesign:0x95a0e70>
CUSTOM ROWS:36:in `add_custom_rows'
AFTER ADD CUSTOM ROWS

(The phrases ADD CUSTOM ROWS and AFTER ADD CUSTOM ROWS are displayed before and after your script is called.)

Default Ruby Code

   # ruby file to generate custom report data
   # copyright 2011 Render Plus Systems	
   # For use with SpaceDesign
   # for documentation see: 
   
   # replace "extended_cost" with the name of your total cost column.
   # calculate new values and add to new rows.
   
   def add_custom_rows
       trace("ADD CUSTOM ROWS") # traces are displayed on the ruby console.
       
       #get total_cost
       total_cost = total_row["extended_cost"]  # replace "extended_cost" with the name of your extended_cost column
       total_cost = total_cost.to_f # make sure this has a value if nil
       trace("TOTAL COST: %s", total_cost)
       
       # calculate VAT from total cost
       vat = total_cost * 0.175
       
       # add a divider line in extended_cost column
       # custom_row is a hash array. You can add values to if for any attributes included in the report
       # start a new custom row
       # ---------- will appear in the name or description column
       custom_row = start_custom_row("----------") # start a new custom row
       custom_row["extended_cost"] = "----------" # add a divider line in extended_cost column
       # add the custom row to report
       add_custom_row
       
       # display VAT calculation
       custom_row = start_custom_row("VAT") # start a new custom row
       svalue = sprintf("%.2f", vat) # format to two decimal places
       svalue2 = dll_call_format_currency(svalue) # add currency formatting
       trace("VAT: %s", svalue2) # display on ruby console
       custom_row["extended_cost"] = svalue2 # this adds the currency value to the extended_cost column
       add_custom_row # add the custom row to report
       
       # add a divider line in extended_cost column
       custom_row = start_custom_row("===========") # start a new custom row
       custom_row["extended_cost"] = "===========" # add a divider line in extended_cost column
       add_custom_row # add the custom row to report
       
       # display new total cost
       custom_row = start_custom_row("Total Cost") # start a new custom row
       svalue = sprintf("%.2f", vat + total_cost) # format to two decimal places
       svalue2 = dll_call_format_currency(svalue) # add currency formating
       custom_row["extended_cost"] = svalue2
       add_custom_row # add the custom row to report
   end#def



See also

SpaceDesign: