rebol [ Title: "Excel" File: %excel.r Date: 29-Jun-2006 Version: 1.0.2 Progress: 0.75 Status: "working, tested on View 1.3.2" Needs: [] Author: "Anton Rolls" Language: "English" Purpose: {Create an Excel spreadsheet with data and a chart.} Usage: {} History: [ 1.0.0 [6-Jun-2006 {First version} "Anton"] 1.0.1 [18-Jun-2006 {revised for reengineered COMLib.r} "Anton"] 1.0.2 [29-Jun-2006 {revised for simpler do/args comlib usage} "Anton"] ] ToDo: { - } Notes: { This example is based on excel.c from the DispHelper distribution: http://disphelper.cvs.sourceforge.net/*checkout*/disphelper/disphelper/samples_c/excel.c } ] if error? set/any 'error try [ ; catch all errors do/args %../COMLib.r [ ; Use the COMLib API functions here szHeadings: ["Mammals" "Birds" "Reptiles" "Fishes" "Plants"] xlApp: CreateObject "Excel.Application" PutValue [xlApp ".DisplayFullScreen = %b" true] PutValue [xlApp ".Visible = %b" true] CallMethod [xlApp ".Workbooks.Add"] ;wait 0.125 ; This delay might be necessary PutValue [xlApp ".ActiveSheet.Name = %s" "Critically Endangered"] ;wait 0.125 ; This delay might be necessary ; Ben's old note: "ActiveSheet.Name" does not get set if you comment the delay at least on my pc... repeat n 5 [ PutValue [xlApp ".ActiveSheet.Cells(%d,%d) = %s" 1 n szHeadings/:n] ] ; This is equivalent to what WITH does in excel.c (just uses GetObject, then release afterwards automatically) xlSheet: GetObject [xlApp ".ActiveSheet"] foreach blk reduce [ [".Range(%s).Interior.Color = %d" "A1:E1" (to-integer to-binary reverse 255.128.0)] [".Range(%s).Interior.Pattern = %d" "A1:E1" 1] ; xlSolid [".Range(%s).Font.Size = %d" "A1:E1" 13] [".Range(%s).Borders.LineStyle = %d" "A1:E1" 1] ; xlContinuous [".Range(%s).Borders.Weight = %d" "A1:E1" 2] ; xlThin [".Range(%s).Borders.Color = %d" "A1:E1" (to-integer to-binary reverse 128.0.0)] [".Range(%s).Value = %d" "A2" 184] [".Range(%s).Value = %d" "B2" 182] [".Range(%s).Value = %d" "C2" 57] [".Range(%s).Value = %d" "D2" 162] [".Range(%s).Value = %d" "E2" 1276] ][ PutValue compose [xlApp (compose blk)] ] release xlSheet ; Output data source CallMethod [xlApp ".ActiveSheet.Range(%s).Merge" "A4:E4"] PutValue [xlApp ".ActiveSheet.Range(%s).Value = %s" "A4" "Source: IUCN Red List 2003 (http://www.redlist.org/info/tables/table2.html)"] ; Apply a border around everything. Note '%m' means missing. CallMethod compose [xlApp ".ActiveSheet.Range(%s).BorderAround(%d, %d, %m, %d)" "A1:E2" 1 2 (to-integer to-binary reverse 128.0.0)] ; Set column widths PutValue [xlApp ".ActiveSheet.Columns(%s).ColumnWidth = %d" "A:E" 12] xlRange: GetObject [xlApp ".ActiveSheet.Range(%s)" "A1:E2"] xlChart: GetObject [xlApp ".ActiveWorkbook.Charts.Add" ""] ; Set up the chart CallMethod [xlChart ".ChartWizard(%o, %d, %d, %d, %d, %d, %b, %s)" xlRange -4100 7 1 1 0 0 "Critical"] PutValue [xlChart ".HasAxis(%d) = %b" 3 false] ; Put the chart on our worksheet CallMethod [xlChart ".Location(%d, %s)" 2 "Critically Endangered"] release xlChart release xlRange release xlApp ] ][ print mold disarm error ]