2015-01-22

Using standard actions:

← Older revision

Revision as of 15:14, 22 January 2015

(4 intermediate revisions by one user not shown)

Line 151:

Line 151:

* Enter multi-lined text - you can enforce a lineending in the CellEdit by holding the {{keypress|Ctrl}} key down when you press {{keypress|ENTER}}. The cell displays only one line of the text. Drag the horizontal splitter underneath the second toolbar down - the CellEdit shows all lines. Another way to see all lines of the text, is to adjust the cell height. You must have activated the grid <code>Option</code> <code>goRowSizing</code>. Then you can drag the lower dividing line of the row with the multi-line cell down to increase the row height - the missing lines now appear in the cell!

* Enter multi-lined text - you can enforce a lineending in the CellEdit by holding the {{keypress|Ctrl}} key down when you press {{keypress|ENTER}}. The cell displays only one line of the text. Drag the horizontal splitter underneath the second toolbar down - the CellEdit shows all lines. Another way to see all lines of the text, is to adjust the cell height. You must have activated the grid <code>Option</code> <code>goRowSizing</code>. Then you can drag the lower dividing line of the row with the multi-line cell down to increase the row height - the missing lines now appear in the cell!



===
Formatting of cells
===

+

===
Formateado de las celdas
===

In addition to entering data the user usually wants to apply some formatting to the cells in order to enhance or group them. The worksheet grid is set up in such a way that its cells display the formats taken from the workbook. In addition, the visual FPSpreadsheet controls are able to store formatting attributes into the cell. Because of the notification mechanism via the WorkbookSource these formats are returned to the WorksheetGrid for display.

In addition to entering data the user usually wants to apply some formatting to the cells in order to enhance or group them. The worksheet grid is set up in such a way that its cells display the formats taken from the workbook. In addition, the visual FPSpreadsheet controls are able to store formatting attributes into the cell. Because of the notification mechanism via the WorkbookSource these formats are returned to the WorksheetGrid for display.



====
Adding
comboboxes
for font name
,
font size
,
and font
color ====

+

====
Añadiendo
comboboxes
para nombre de fuente
,
tamaño de fuente
,
y
color
de fuente
====

[[file:sCellFontCombobox.png‎|right|400px]]

[[file:sCellFontCombobox.png‎|right|400px]]



In this section
,
we want to provide the possibility to modify the font of the cell texts by selecting its name, size and
/
or
color.
The visual
FPSpreadsheet
provide the
flexible '''TsCellCombobox'''
for this purpose
.
It has the property
<code>CellFormatItem</code>
which defines which attribute it controls
:

+

En esta sección cubrimos la necesidad de modificar la fuente del texto de la celda a través de una selección de nombre
,
tamaño y
/
o
color.
El
FPSpreadsheet
visual aporta el
flexible '''TsCellCombobox'''
para este propósito
.
Tiene la propiedad
<code>CellFormatItem</code>
, la cual define que atributo controla
:



* <code>cfiFontName</code>:
This option populates the
combobox
with all fonts found in the current system
.
The selected item is used for the type face in the selected cells
.

+

* <code>cfiFontName</code>:
Esta opción rellena el
combobox
con todas las fuentes disponibles en el sistema
.
El elemento seleccionado se utilza como tipo de fuente en las celdas seleccionadas
.



* <code>cfiFontSize</code>
fills the
combobox
with the mostly used font sizes
(
in points
).
Again
,
the selected item defines the font size of the selected cells
.

+

* <code>cfiFontSize</code>
rellena el
combobox
con los tamaños de fuente más utilizadas
(
tamaño en puntos
).
De nuevo
,
el elemento seleccionado define el tamaño de fuente de las celdas seleccionadas
.



* <code>cfiFontColor</code>
adds all
pre-
defined colors
("
palette
")
of the workbook to the
combobox
to set the text
color
of the selected cells
.
The
combobox
items consist of
a
little color box along with the
color
name
. If the <code>ColorRectWidth</code> is set to <code>-1</code> the color name is dropped.

+

* <code>cfiFontColor</code>
añade todos los colores
pre-
definidos
("
la paleta de colores
")
del libro al
combobox
para establecer el
color
del texto de las celdas seleccionadas
.
Los elementos del
combobox
consisten en un pequeño recuadro de colores junto
a
su correspondiente nombre de
color. If the <code>ColorRectWidth</code> is set to <code>-1</code> the color name is dropped.

* <code>cfiBackgroundColor</code>, the same with the background color of the selected cells.

* <code>cfiBackgroundColor</code>, the same with the background color of the selected cells.

* <code>cfiCellBorderColor</code>, the same with the border color of the selected cells - this feature is currently not yet supported.

* <code>cfiCellBorderColor</code>, the same with the border color of the selected cells - this feature is currently not yet supported.

Line 176:

Line 176:

* '''TsWorksheetDeleteAction''': deletes the active worksheet from the workbook after a confirmation dialog. The last worksheet cannot be deleted.

* '''TsWorksheetDeleteAction''': deletes the active worksheet from the workbook after a confirmation dialog. The last worksheet cannot be deleted.



* '''TsWorksheetRenameAction''':
renames the active worksheet
.

+

* '''TsWorksheetRenameAction''':
renombra la hoja activa
.



* '''TsCopyAction''':
Copies the currently selected cells to an internal list
("CellClipboard")
from where they can be pasted back into the spreadsheet to another location
.
The process can occur in a clipboard-manner
("
copy
"/"
cut
",
then
"
paste
")
or in the way of the
"copy brush"
of the Office applications
.
The property
<code>CopyItem</code>
determines whether the entire cell, or only cell values
,
cell formulas
,
or cell formats are transferred
.

+

* '''TsCopyAction''':
Copia las celdas actualmente seleccionadas a un listado interno
("CellClipboard")
desde donde pueden volverse a pegar dentro de la hoja de cálculo en otra ubicación
.
El proceso puede realizarse al estilo del portapapeles
("
copia
"/"
corta
",
luego
"
pega
")
o como
"copy brush"
de las aplicaciones ofimáticas
.
La propiedad
<code>CopyItem</code>
determina la manera en que se transfiere la celda entera
,
o solamente valores de la celda
,
o el formato de las celdas
.



* '''TsFontStyleAction''':
Modifies the font style of the selected cells
. The property <code>FontStyle</code> defines whether the action makes the font bold, italic, underlined or striked-out. Normally each font style is handles by its own action. See the example below.

+

* '''TsFontStyleAction''':
Modifica el estilo de la fuente de la(s) celda(s) seleccionada(s)
. The property <code>FontStyle</code> defines whether the action makes the font bold, italic, underlined or striked-out. Normally each font style is handles by its own action. See the example below.

* '''TsHorAlignmentAction''': Can be used to modify the horizontal alignment of text in the selected cells. Select <code>HorAlignment</code> to define which kind of alignment (left, center, right) is covered by the action. Like with the TsFontStyleAction, several actions should be provided to offer all available alignments. They are grouped in a mutually exclusive way like radiobuttons.

* '''TsHorAlignmentAction''': Can be used to modify the horizontal alignment of text in the selected cells. Select <code>HorAlignment</code> to define which kind of alignment (left, center, right) is covered by the action. Like with the TsFontStyleAction, several actions should be provided to offer all available alignments. They are grouped in a mutually exclusive way like radiobuttons.

Line 272:

Line 272:

* '''Toolbar''': Add a TToolButton to the first toolbar and drag it to its left edge. Assign the FileSaveAs action to its <code>Action</code> property.

* '''Toolbar''': Add a TToolButton to the first toolbar and drag it to its left edge. Assign the FileSaveAs action to its <code>Action</code> property.

* '''Menu''': The "Save" command is usually in a submenu called "File". Therefore, double click on the TMainMenu, right-click on the "Format" item and insert a new item "before" the current one. Name it "File". Add a submenu to it. Click at the default menu item and assign the FileSaveAs action to its <code>Action</code> property.

* '''Menu''': The "Save" command is usually in a submenu called "File". Therefore, double click on the TMainMenu, right-click on the "Format" item and insert a new item "before" the current one. Name it "File". Add a submenu to it. Click at the default menu item and assign the FileSaveAs action to its <code>Action</code> property.

+

+

=== Reading from file ===

+

+

What is left is '''reading of a spreadsheet file''' into our application. Of course, FPSpreadsheet is well-prepared for this task. The operations are very similar to saving. But instead of using a TFileSaveAs standard action, we use a '''TFileOpen''' standard action. Again, this standard action has a built-in file dialog where we have to set the <code>DefaultExtension</code> (".xls" or ".xlsx", most probably) and the format <code>Filter</code>:

+

+

:<code>All spreadsheet files|*.xls;*.xlsx;*.ods;*.csv|All Excel files (*.xls, *.xlsx)|*.xls;*.xlsx|Excel XML spreadsheet (*.xlsx)|*.xlsx|Excel 97-2003 spreadsheets (*.xls)|*.xls|Excel 5 spreadsheet (*.xls)|*.xls|Excel 2.1 spreadsheets (*.xls)|*.xls|LibreOffice/OpenOffice spreadsheet (*.ods)|*.ods|Comma-delimited files (*.csv)|*.csv</code>

+

+

(Copy this string into the field <code>Filter</code> of the action's <code>Dialog</code>). As you may notice the <code>Filter</code> contains selections which cover various file formats, such as "All spreadsheet files", or "All Excel files". This is possible because the TsWorkbookSource has a property <code>AutoDetectFormat</code> for automatic detection of the spreadsheet file format. In the other cases, like "Libre/OpenOffice", we can specify the format, <code>sfOpenDocument</code>, explicitly. Evaluation of the correct file format and reading of the file is done in the <code>OnAccept</code> event handler of the action:

+

+

<syntaxhighlight>

+

{ Loads the spreadsheet file selected by the FileOpen standard action }

+

procedure TForm1.FileOpen1Accept(Sender: TObject);

+

begin

+

sWorkbookSource1.AutodetectFormat := false;

+

case FileOpen1.Dialog.FilterIndex of

+

1: sWorkbookSource1.AutoDetectFormat := true;        // All spreadsheet files

+

2: sWorkbookSource1.AutoDetectFormat := true;        // All Excel files

+

3: sWorkbookSource1.FileFormat := sfOOXML;            // Excel 2007+

+

4: sWorkbookSource1.FileFormat := sfExcel8;          // Excel 97-2003

+

5: sWorkbookSource1.FileFormat := sfExcel5;          // Excel 5.0

+

6: sWorkbookSource1.FileFormat := sfExcel2;          // Excel 2.1

+

7: sWorkbookSource1.FileFormat := sfOpenDocument;    // Open/LibreOffice

+

8: sWorkbookSource1.FileFormat := sfCSV;              // Text files

+

end;

+

sWorkbookSource1.FileName :=FileOpen1.Dialog.FileName;  // This loads the file

+

end;

+

</syntaxhighlight>

+

+

In order to see this action in the toolbar and menu, add a TToolButton to the '''toolbar''' and assign the TFileOpenAction to its <code>Action</code> property. In the '''menu''', add a new item before the "Save as" item, and assign its <code>Action</code> accordingly.

+

+

{{Note|You can see a spreadsheet file even at designtime if you assign its name to the <code>Filename</code> property of the TsWorkbookSource. But be aware that the file probably cannot be found at runtime if it is specified by a relative path and if the application is to run on another computer with a different directory structure! }}

+

+

== Summary ==

+

If you followed us through the steps of this tutorial you have programmed a complex spreadsheet gui application almost without having written any line of code (with the exception of the loading and saving routines). If you did not, have a look at the demo "fps_ctrls" in the ''examples'' folder of the FPSpreadsheet installation; it shows the result of this tutorial with some add-ons.

+

+

[[Category:Tutorials]]

+

[[Category:Data import and export]]

Show more