2 # -----------------------------------------------------------------------------
5 gi
.require_version('Gnm', '1.12')
6 from gi
.repository
import Gnm
9 # A context for reporting errors to stderr
10 cc
= Gnm
.CmdContextStderr
.new()
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
39 cr
= Gnm
.clipboard_copy_range(sheet
,src
)
42 pt
= Gnm
.PasteTarget
.new (sheet
,dst
,Gnm
.PasteFlags
.DEFAULT
)
43 Gnm
.clipboard_paste_region(cr
,pt
,None)
51 sheet
.apply_style(r
,st
)
55 st
.set_format_text("0.0000")
58 sheet
.apply_style(r
,st
)
60 # Recalculate all cells that need it
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)
69 print(sheet
.cell_get_value(0,i
).get_as_string())
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 "")))