Shutdown: help the style leak printer out a bit.
[gnumeric.git] / test / t3001-introspection-simple.py
blobad07dd295e28559e4e72a2da6345140178fd4d8c
1 #!/usr/bin/python
2 # -----------------------------------------------------------------------------
4 import gi
5 gi.require_version('Gnm', '1.12')
6 from gi.repository import Gnm
7 Gnm.init()
9 # A context for reporting errors to stderr
10 cc = Gnm.CmdContextStderr.new()
12 # Load plugins
13 Gnm.plugins_init(cc)
15 # -----------------------------------------------------------------------------
17 # Create a workbook with one sheet
18 wb = Gnm.Workbook.new_with_sheets(1)
20 # Get sheet. Index starts at 0
21 sheet = wb.sheet_by_index(0)
22 print("Name: {}".format(sheet.props.name))
23 print("Number of columns: {}".format (sheet.props.columns))
24 print("Number of rows: {}".format (sheet.props.rows))
26 # Store values and expressions is some cells. Coordinates are (col,row)
27 # both starting at 0. (So what the gui sees as row 1 is 0 here.)
28 sheet.cell_set_value(0,0,Gnm.Value.new_int(10))
29 sheet.cell_set_value(0,1,Gnm.Value.new_float(101.25))
30 sheet.cell_set_text(0,2,"=A1+A2")
31 sheet.cell_set_text(0,3,"'01")
32 sheet.cell_set_text(0,4,"zzz")
33 sheet.cell_set_value(0,5,Gnm.Value.new_string("abc"))
34 sheet.cell_set_value(0,6,Gnm.Value.new_bool(1))
36 # Copy A1:A7, paste to C1:C7
37 src = Gnm.Range()
38 src.init(0,0,0,6)
39 cr = Gnm.clipboard_copy_range(sheet,src)
40 dst = Gnm.Range()
41 dst.init(2,0,2,6)
42 pt = Gnm.PasteTarget.new (sheet,dst,Gnm.PasteFlags.DEFAULT)
43 Gnm.clipboard_paste_region(cr,pt,None)
46 # Make A1:A2 bold
47 st = Gnm.Style.new()
48 st.set_font_bold(1)
49 r = Gnm.Range()
50 r.init(0,0,0,1)
51 sheet.apply_style(r,st)
53 # Set a format for A2
54 st = Gnm.Style.new()
55 st.set_format_text("0.0000")
56 r = Gnm.Range()
57 r.init(0,1,0,1)
58 sheet.apply_style(r,st)
60 # Recalculate all cells that need it
61 wb.recalc()
63 # Resize column A to fit values in it
64 pixels = sheet.col_size_fit_pixels(0,0,sheet.props.rows-1,1)
65 sheet.col_set_size_pixels(0,pixels,1)
67 print("\nAs string:")
68 for i in range(7):
69 print(sheet.cell_get_value(0,i).get_as_string())
71 print("\nAs int:")
72 for i in range(7):
73 print(sheet.cell_get_value(0,i).get_as_int())
75 print("\nFormatted value:")
76 c = sheet.cell_fetch(0,1)
77 print("Value: {} Format: {} Rendered: {}".format (c.get_value().get_as_string(),
78 c.get_format().as_XL(),
79 c.get_rendered_text()))
82 print("\nList of cells in sheet:")
83 for c in sheet.cells(None):
84 st = sheet.style_get (c.pos.col,c.pos.row)
85 bold = st.get_font_bold()
86 print("{}: {} {}".format(c.name(), c.get_entered_text(), ("[bold]" if bold else "")))