home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-04 | 5.5 KB | 193 lines | [TEXT/Moml] |
- (* excel.sml *)
- (* 17 May 1997 e *)
-
- (* using AppleScript to work with Excel spreadsheets *)
-
- (*
- load "Int";
- load "Real";
- load "AppleScript";
- open AppleScript;
- *)
-
-
- fun tell_excel_str s =
- "tell application \"Microsoft Excel\"\n" ^ s ^ "\nend tell";
-
- fun run_excel s = as_run_text( tell_excel_str s );
-
- fun rYcX y x =
- "\"R" ^ (Int.toString y) ^ "C" ^ (Int.toString x) ^ "\""
- ;
-
- fun toStrStr v = "\"" ^ v ^ "\"" ;
-
- fun toIntStr v = "\"" ^ (Int.toString v) ^ "\"" ;
-
- fun toRealStr v = "\"" ^ (Real.toString v) ^ "\"" ;
-
- fun get_cell y x = (* read RyCx *)
- run_excel( "get value of Cell " ^ (rYcX y x) ^ " of Document 1" )
- ;
-
- fun get_cell_as y x ty = (* read RyCx as type ty *)
- run_excel( "get value of Cell " ^ (rYcX y x) ^ " of Document 1 as " ^ ty )
- ;
-
- fun get_cell_int y x = get_cell_as y x "integer";
- fun get_cell_real y x = get_cell_as y x "real";
- fun get_cell_string y x = get_cell_as y x "string";
-
- fun set_cell y x v = (* write v into RyCx *)
- run_excel( "set value of Cell " ^ (rYcX y x) ^ " of Document 1 to " ^ v )
- ;
-
- fun set_cell_int y x v = (* write the integter v into RyCx *)
- set_cell y x (toIntStr v)
- ;
-
- fun set_cell_real y x v = (* write the real v into RyCx *)
- set_cell y x (toRealStr v)
- ;
-
- fun set_cell_string y x v = (* write the string v into RyCx *)
- set_cell y x (toStrStr v)
- ;
-
-
- (* examples
- fun excel_fill rstart rend cstart cend v =
- let fun dorow r = if r > rend then ()
- else
- let fun docol c = if c > cend then ()
- else ( set_cell r c v; docol (c + 1) )
- in docol cstart;
- dorow (r + 1)
- end
- in dorow rstart
- end;
- excel_fill 5 7 5 7 (toStrStr "e");
- excel_fill 6 9 1 5 (toRealStr 3.141592685);
-
- run_excel "the name of Document 1";
- run_excel "the name of Document 2"
- handle AppleScriptErr (n,s) => (Int.toString n ^ "\n" ^ s);
-
- run_excel "get (count document 1 each column) as integer";
- run_excel "get (count document 1 each row) as integer";
- run_excel "delete Column 3";
-
- val ctscrpt = as_compile "get (count document 1 each column) as integer";
- as_run_script ctscrpt
- handle AppleScriptErr (n,s) => s;
-
- as_dispose ctscrpt
- handle AppleScriptErr (n,s) => print s;
-
- as_dispose ctscrpt
- handle AppleScriptErr (n,s) => print s;
-
- val ctscrpt =
- as_compile (tell_excel_str "get (count document 1 each column) as integer");
- as_run_script ctscrpt
- handle AppleScriptErr (n,s) => (Int.toString n ^ s);
-
- *)
-
- (* one liners, thanks to J.P. Adams...
-
- get Range "R1C1:R3C1" of Document 1 as text
- get Range "TOM" of Document 1 as text
- get value of Cell 3 of Row 2 of Document 1
- get value of Cell "Fred" of Document 1
- get value of Cell "R1C1" of Document 1
- set Range "R1C1:R3C1" of Document 1 to {"4", "5", "6"}
- set Range "Tom" of Document 1 to {"4", "5", "6"}
- set value of Cell 3 of Row 2 of Document 1 to "99"
- set value of Cell "Fred" of Document 1 to "99"
- set value of Cell "R1C1" of Document 1 to "99"
-
- make new Window
- exists Window "AE Test"
- exists table "Table_1"
- exists Range "Tom"
- exists Cell "fred"
- exists Document "AE Test"
- delete Cell "Fred" -- No reply, check spreadsheet for deletion
- delete table "Table_1"
- duplicate Cell "Bob" to Cell "Fred"
- duplicate Cell "R1C1" to Cell "R6C1"
- move Cell "R1C1" to Cell "R6C1"
- move Cell "Bob" to Cell "R1C1"
- set the font of Cell "R1C1" to "Times"
-
- set the size of Cell "R1C1" to "36" -- increases height of entire row
- set the name of Cell "R1C1" to "Texas_Cities"
- set the formula of Cell "R1C1" to "=5*5" --cell should show 25
- set the bold of Cell "R1C1" to true
- set the italic of Cell "R1C1" to true
- set the underline of Cell "R1C1" to true
- set the strikethrough of Cell "R1C1" to true
- set the outline of Cell "R1C1" to true
- set the shadow of Cell "R1C1" to true
- set the plain of Cell "R1C1" to true
- --see Number dialog, Format menu
- set the number format of Cell "R1C1" to "0.00%"
- --You must use the index (1-7) for the alignment found in the
- -- Excel Function Reference page 15-16
- set the alignment of Cell "R1C1" to "3"
- --the borders are set using a macro code
- -- Excel Function Reference page 39
- set the left border of Cell "R1C1" to "5"
-
- set the font of Range "Table_1" to "times"
- set the height of Row 6 to "24"
- set the font of Row 7 to "times"
- set the font of Column 1 to "times"
- set the width of Column 2 to "14"
- set the name of Column 2 to "STATES"
-
- get name of Window 1
- the type of Window 1
- the selection of Window 1 as string
- the user selection of Window 1 as string
- the bounds of Window 1
- the titled of Window 1
- the index of Window "AE Test"
- the floating of Window 1
- the modal of Window 1
- the resizable of Window 1
- the zoomed of Window 1
- the visible of Window 1
-
- the pathname of Document "AE Test"
- the type of Document "AE Test"
- the cell protection of Document "AE Test"
- the object protection of Document "AE Test"
- the number of windows of Document "AE Test"
- the selection of Document "AE Test" as string
- the user selection of Document "AE Test" as string
- the protection of Document "AE Test"
- the name of Document 1
- the modified of Document 1
-
- the name
- the Calculation
- the Iteration
- the selection as string
- the clipboard as string
- the frontmost
- the version as string
-
- the number of series of Chart 1
- the type of Chart 1
- the variant of Chart 1
- the has legend of Chart 1
-
- get (count document 1 each row)
- get (count document 1 each column)
- get index of document "[workbook name]worksheet name"
- *)
-
- (* end *)
-