Problem
You have set up a spreadsheet in How to use GtkSheet - Part 1 - create the spreadsheet?
Now you would like to read the cell contents as shown below:
Solution
- Make sure your version of PHP-GTK2 has the GtkExtra library, and the feature turned on in php.ini. (See notes below.)
- Create the spreadsheet as outlined in How to use GtkSheet - Part 1 - create the spreadsheet?
- Get the number of rows with
GtkSheet::get_rows_count()
. - Get the number of columns with
GtkSheet::get_columns_count()
. - Read the cell content with
GtkSheet::cell_get_text()
.
Important Note: This only works for PHP-GTK2 compliled with the additional library GtkExtra. For linux, you can download the files from http://gtkextra.sourceforge.net/ and do a recompile. For windows, you may use the builds by Elizabeth Smith or the official php-gtk2 beta release available at http://gtk.php.net/download.php. Both contain all the required gtkextra libraries and dll's. In the php.ini
, don't forget to add php-gtk.extensions = php_gtk_extra2.dll
to turn on GtkExtra.
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25 26 27 28 29 30 33 34 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 51 52 | <?php $window = new GtkWindow(); $window->set_title($argv[0]); $window->set_size_request(400, 250); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Using GtkSheet\n". "Part 2 - reading the cell contents"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $title->set_justify(Gtk::JUSTIFY_CENTER); $alignment = new GtkAlignment(0.5, 0, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0); $vbox->pack_start($hbox = new GtkHBox(), 0); $hbox->pack_start($button = new GtkButton('Read Values'), 0); $scrolled_win = new GtkScrolledWindow(); $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $vbox->pack_start($scrolled_win); $sheet = new GtkSheet(6, 12, 'Test 123'); $sheet->set_autoresize(1); $scrolled_win->add($sheet); $button->connect('clicked', 'on_click', $sheet); $window->show_all(); Gtk::main(); function on_click($button, $sheet) { $num_rows = $sheet->get_rows_count(); // note 1 $num_cols = $sheet->get_columns_count(); // note 2 for ($row=0; $row<$num_rows; ++$row) { for ($col=0; $col<$num_cols; ++$col) { $value = $sheet->cell_get_text($row, $col); // note 3 if ($value!='') print "row $row col $col: $value\n"; // note 4 } } } ?> |
Output
As shown above.Explanation
- Get the number of rows.
- Get the number of columns.
- Read the cell content.
- Echo only those cells with values.
Note
In this example, I output the values to the command window. You can, of course, save the values to a file or MySQL database.
Related Links
- How to use GtkSheet - Part 1 - create the spreadsheet?
- How to use GtkSheet - Part 3 - set values?
- How to use GtkSheet - Part 4 - set values with justification?
- How to use GtkSheet - Part 5 - set row and column headers?
- How to use GtkSheet - Part 6 - non editable browser mode?
- How to use GtkSheet - Part 7 - tab to move to next row?
- How to use GtkSheet - Part 8 - alternate row colors?
- How to use GtkSheet - Part 9 - using attach with justification?
- How to use GtkSheet - Part 10 - display image in cell?
Read more...