issue89:libreoffice
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue89:libreoffice [2015/02/03 17:23] – d52fr | issue89:libreoffice [2015/02/07 11:25] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
In time, I decided, since I was such a fallible, flawed human being, I needed to reduce the number of steps necessary to complete this task in order to lessen my chances for mistakes. The result was a macro where all I have to do is give the sheet a new name. The macro handles all the rest, making sure it is copied and placed at the end of the tab list. The task fits perfectly into the idea behind macros, a repeatable process that automation can speed up, or prevent mistakes. Today, I share it with you.** | In time, I decided, since I was such a fallible, flawed human being, I needed to reduce the number of steps necessary to complete this task in order to lessen my chances for mistakes. The result was a macro where all I have to do is give the sheet a new name. The macro handles all the rest, making sure it is copied and placed at the end of the tab list. The task fits perfectly into the idea behind macros, a repeatable process that automation can speed up, or prevent mistakes. Today, I share it with you.** | ||
- | Retour en arrière sur les parties 8 à 12 (FCM numéros, 53, 55-58) : Je vous ai amené d'une feuille de tableau blanc à un tableau de budget pleinement opérationnel. | + | Retour en arrière sur les parties 8 à 12 (FCM numéros, 53, 55-58) : je vous ai amené d'une feuille de tableau blanc à un tableau de budget pleinement opérationnel. |
- | A temps, j'ai décidé, | + | En temps voulu et puisque |
**The Manual Method | **The Manual Method | ||
Ligne 14: | Ligne 14: | ||
I know, you're thinking that it's not that bad, but after a few times getting it wrong, your inner Hulk comes out and you want to smash things. Okay, maybe that's just me. Trying to balance the home budget is frustrating enough without the bonus of mistakes while copying the spreadsheet.** | I know, you're thinking that it's not that bad, but after a few times getting it wrong, your inner Hulk comes out and you want to smash things. Okay, maybe that's just me. Trying to balance the home budget is frustrating enough without the bonus of mistakes while copying the spreadsheet.** | ||
+ | |||
+ | La méthode manuelle | ||
+ | |||
+ | Pour pouvoir apprécier une macro, vous devez bien comprendre ce qu' | ||
+ | |||
+ | Faites un clic droit sur l' | ||
+ | |||
+ | Je sais ; vous allez dire que ce n'est pas si dur, mais après vous être trompé plusieurs fois, votre nature brute ressort et vous voulez tout balancer. D' | ||
+ | |||
**The CopySheet Macro | **The CopySheet Macro | ||
Ligne 30: | Ligne 39: | ||
End If | End If | ||
** | ** | ||
+ | |||
+ | La macro CopySheet | ||
+ | |||
+ | La macro n'est pas très longue (page suivante, en haut à droite) et vous pouvez aisément la saisir. Vous pouvez aussi la copier sur : http:// | ||
+ | |||
+ | La macro commence par la déclaration de 3 variables utilisées dans le corps de la macro. « Sheet1 » et « Sheet2 » sont des chaînes de caractères qui contiendront les noms de la feuille courante (Sheet1) et de la nouvelle feuille (Sheet2). La variable « Doc » contiendra une référence au document en cours. Elle est déclarée comme un type Object parce que la référence est celle d'un objet API LibreOffice. | ||
+ | |||
+ | Doc = ThisComponent | ||
+ | |||
+ | ThisComponent est le document courant actif dans LibreOffice. Dans ce cas, la macro cherchera un tableur Calc. | ||
+ | |||
+ | If NOT Doc.supportsService(" | ||
+ | MsgBox "This Macro Only Works with Calc Spreadsheets" | ||
+ | Exit Sub | ||
+ | End If | ||
+ | |||
** | ** | ||
The “If” statements check to make sure that the current document is a Calc spreadsheet. It checks to see whether the document supports the SpreadsheetDocument service, identifying it as a Calc spreadsheet and not some other document type. If it is not a spreadsheet, | The “If” statements check to make sure that the current document is a Calc spreadsheet. It checks to see whether the document supports the SpreadsheetDocument service, identifying it as a Calc spreadsheet and not some other document type. If it is not a spreadsheet, | ||
Ligne 43: | Ligne 68: | ||
• The title of the InputBox window (“Copy Sheet”). | • The title of the InputBox window (“Copy Sheet”). | ||
• The default text (it just uses the name of the current sheet [Sheet1] as the default text).** | • The default text (it just uses the name of the current sheet [Sheet1] as the default text).** | ||
+ | |||
+ | L' | ||
+ | |||
+ | Sheet1 = Doc.CurrentController.ActiveSheet.Name | ||
+ | |||
+ | La macro utilise l' | ||
+ | |||
+ | Sheet2 = InputBox(" | ||
+ | |||
+ | Pour récupérer le nom de la nouvelle feuille, la macro utilise la fonction InputBox. InputBox contient 3 arguments : | ||
+ | • Une invite destinée à l' | ||
+ | • Le titre de la fenêtre InputBox (« CopySheet »). | ||
+ | • Le texte par défaut (le nom de la feuille courante [Sheet1] est le texte par défaut). | ||
**If the user clicks the OK button, the InputBox will return the string entered in the text box or the default text when no changes are made. If the user clicks the Cancel button, a blank string is returned. | **If the user clicks the OK button, the InputBox will return the string entered in the text box or the default text when no changes are made. If the user clicks the Cancel button, a blank string is returned. | ||
Ligne 55: | Ligne 93: | ||
Now, the macro must use some logic to determine how to proceed. The “If” checks to see if the returned string is blank. If so, that means that the Cancel button was pressed, therefore the “Exit Sub” is executed. “Exit Sub” exits the macro without running any more of the remaining code. ** | Now, the macro must use some logic to determine how to proceed. The “If” checks to see if the returned string is blank. If so, that means that the Cancel button was pressed, therefore the “Exit Sub” is executed. “Exit Sub” exits the macro without running any more of the remaining code. ** | ||
+ | |||
+ | Si l' | ||
+ | |||
+ | If Sheet2 = "" | ||
+ | |||
+ | Do While Doc.Sheets.hasByName(Sheet2) | ||
+ | Sheet2 = InputBox(Sheet2 + _ | ||
+ | " | ||
+ | If Sheet2 = "" | ||
+ | Loop | ||
+ | |||
+ | Maintenant, la macro doit utiliser un raisonnement logique pour déterminer comment procéder. Le « If » teste si la chaîne de caractères est vide. Si c'est le cas, ça signifie que le bouton Annuler a été cliqué, donc la commande Exit Sub est exécutée. Exit Sub termine la macro sans exécution de code supplémentaire. | ||
**The Do While...Loop checks to see if another sheet in the Calc document has the same name. The hasByName method checks the supplied name against the names of all the sheets in the collection. If a match is detected, the macro uses an InputBox to prompt the user for a new, unmatched name. The Do While...Loop will loop until the name in Sheet2 does not match the name of any other sheet. The “If” inside the loop exits the macro if Cancel is pressed. If the name is unmatched from the start, the loop never runs. This prevents two sheets from having the same name. | **The Do While...Loop checks to see if another sheet in the Calc document has the same name. The hasByName method checks the supplied name against the names of all the sheets in the collection. If a match is detected, the macro uses an InputBox to prompt the user for a new, unmatched name. The Do While...Loop will loop until the name in Sheet2 does not match the name of any other sheet. The “If” inside the loop exits the macro if Cancel is pressed. If the name is unmatched from the start, the loop never runs. This prevents two sheets from having the same name. | ||
Ligne 62: | Ligne 112: | ||
Doc.Sheets.CopyByName(Sheet1, | Doc.Sheets.CopyByName(Sheet1, | ||
+ | |||
+ | La boucle Do While... vérifie si une autre feuille du document Calc a le même nom. La méthode hasByName compare le nom aux noms des autres feuilles du classeur. Si une correspondance est détectée, la macro utilise une fonction InputBox pour demander un nouveau nom différent à l' | ||
+ | |||
+ | NOTE : Le trait de soulignement (_) dans la déclaration InputBox est utilisé pour couper une longue ligne de texte en morceaux plus courts. Le Basic de LibreOffice exige que le trait de soulignement soit le dernier caractère de la ligne. Rien, pas même une espace, ne peut le suivre. Quand des lignes sont reliées par ce biais, LibreOffice les considère comme une seule ligne. | ||
+ | |||
+ | Doc.Sheets.CopyByName(Sheet1, | ||
**The last line of the macro brings all this preparatory work together to finally execute the copy. Sheets is a reference to the collection of sheets in the document. CopyByName is the method that actually copies the sheet and moves it to the end of the sheet tabs. The CopyByName method has 3 parameters: | **The last line of the macro brings all this preparatory work together to finally execute the copy. Sheets is a reference to the collection of sheets in the document. CopyByName is the method that actually copies the sheet and moves it to the end of the sheet tabs. The CopyByName method has 3 parameters: | ||
Ligne 73: | Ligne 129: | ||
When I developed and wrote the CopySheet macro, I created a macro module for Calc. Here I could store this macro as well as any future macros designed for the Calc program. It's a good idea to group like macros together.** | When I developed and wrote the CopySheet macro, I created a macro module for Calc. Here I could store this macro as well as any future macros designed for the Calc program. It's a good idea to group like macros together.** | ||
+ | |||
+ | La dernière ligne de la macro regroupe tout le travail préparatoire pour enfin exécuter la copie. Sheets est un appel à l' | ||
+ | • Le nom de la feuille à copier (Sheet1) ; | ||
+ | • Le nom de la feuille copiée (Sheet2) ; | ||
+ | • La position de la nouvelle feuille (Doc.Sheets.Count). | ||
+ | |||
+ | Count est le nombre total de feuilles du classeur (Sheets). Comme les feuilles sont comptées à partir de 0, l' | ||
+ | |||
+ | Créer le module de macro Calc | ||
+ | |||
+ | Quand j'ai développé et écrit la macro CopySheet, j'ai créé un module de macro dans Calc. Ici je pouvais sauvegarder la macro ainsi que toute autre macro que je développerai pour le programme Calc. C'est une bonne pratique de regrouper les macros ainsi. | ||
**To create the module, Tools > Macros > Organize Macros > LibreOffice Basic. The LibreOffice Basic Macros dialog opens. Click Organizer to get the LibreOffice Basic Macro Organizer dialog. Under My Macros > Standard, there is a default module named Module 1. Select it and click Delete. With Standard selected, click New. Name the new Module “Calc” and click OK. Click Close. | **To create the module, Tools > Macros > Organize Macros > LibreOffice Basic. The LibreOffice Basic Macros dialog opens. Click Organizer to get the LibreOffice Basic Macro Organizer dialog. Under My Macros > Standard, there is a default module named Module 1. Select it and click Delete. With Standard selected, click New. Name the new Module “Calc” and click OK. Click Close. | ||
Now back in the LibreOffice Basic Macros dialog, select the new “Calc” module you just created and click Edit, opening the LibreOffice Macro Editor. Delete the automatically created “Sub Main” and “End Sub”. Type in or copy and paste the SheetCopy macro into the editor. Save the module and close the editor.** | Now back in the LibreOffice Basic Macros dialog, select the new “Calc” module you just created and click Edit, opening the LibreOffice Macro Editor. Delete the automatically created “Sub Main” and “End Sub”. Type in or copy and paste the SheetCopy macro into the editor. Save the module and close the editor.** | ||
+ | |||
+ | Pour créer le module Outils > Macros > Gérer les macros > LibreOffice Basic... La boîte de dialogue Macros LibreOffice Basic s' | ||
+ | |||
+ | De retour dans la boîte de dialogue LibreOffice Basic, sélectionnez le module « Calc » que vous venez de créer et cliquez sur Éditer, ce qui ouvre l' | ||
**Test The Macro | **Test The Macro | ||
Ligne 82: | Ligne 153: | ||
After typing in the macro and saving it, you will want to test it to make sure you typed everything correctly. First, open a Calc document or create a new one. You can then test the macro by going to Tools > Macros > Run. Under Library, select My Macros > Standard > Calc. Under “Macro Name” select CopySheet and click Run. Enter a name for the sheet like “New Sheet.” Click OK. If all goes well, a new sheet is created with the name you gave it. You will want to repeat the test and not change the name to see if the macro prompts you to change the name. Also, test to make sure the macro stops when you click on Cancel rather than OK. For the final test, open a text document and run the macro. You should get the message telling you that the macro works only on spreadsheets.** | After typing in the macro and saving it, you will want to test it to make sure you typed everything correctly. First, open a Calc document or create a new one. You can then test the macro by going to Tools > Macros > Run. Under Library, select My Macros > Standard > Calc. Under “Macro Name” select CopySheet and click Run. Enter a name for the sheet like “New Sheet.” Click OK. If all goes well, a new sheet is created with the name you gave it. You will want to repeat the test and not change the name to see if the macro prompts you to change the name. Also, test to make sure the macro stops when you click on Cancel rather than OK. For the final test, open a text document and run the macro. You should get the message telling you that the macro works only on spreadsheets.** | ||
+ | Tester la macro | ||
+ | |||
+ | Après avoir saisi et sauvé la macro, vous voulez la tester pour être sûr que toute la saisie est correcte. D' | ||
**NOTE: Back in Full Circle issue 64 (LibreOffice Part 17: Macros), I showed you how to create a menu shortcut to a macro. This is a good candidate for such a shortcut. Create the menu and shortcut in Calc. | **NOTE: Back in Full Circle issue 64 (LibreOffice Part 17: Macros), I showed you how to create a menu shortcut to a macro. This is a good candidate for such a shortcut. Create the menu and shortcut in Calc. | ||
Ligne 87: | Ligne 161: | ||
Macros like CopySheet can speed up your processes and prevent you from making mistakes, which is the whole idea behind macros. This is just an example of something you can do with macros, but you can write your own macros that can expand the use or capabilities of any of the LibreOffice programs, or just reduce a task you do often. A Google search for “LibreOffice Basic” is a good place to start learning more.** | Macros like CopySheet can speed up your processes and prevent you from making mistakes, which is the whole idea behind macros. This is just an example of something you can do with macros, but you can write your own macros that can expand the use or capabilities of any of the LibreOffice programs, or just reduce a task you do often. A Google search for “LibreOffice Basic” is a good place to start learning more.** | ||
+ | |||
+ | NOTE : Dans l' | ||
+ | |||
+ | Les macros comme CopySheet peuvent accélérer vos processus et vous éviter de faire des erreurs, ce qui est toute l' |
issue89/libreoffice.1422980606.txt.gz · Dernière modification : 2015/02/03 17:23 de d52fr