issue89:libreoffice
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue89:libreoffice [2014/10/31 18:20] – créée andre_domenech | issue89:libreoffice [2015/02/07 11:25] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Back in parts 8-12 (FCM issues 53, 55-58), I took you from a blank spreadsheet to a full working budget spreadsheet. Something similar is what I use twice a month to do my home budget. I keep a whole year of budget spreadsheets in one document, which means twice a month I copy the sheet, rename it, and make sure I get all the settings correct to put a copy of the current sheet at the end of the sheet tabs. Believe me, mistakes have been made. Curses have been breathed. Innocent computers have been threatened. | + | **Back in parts 8-12 (FCM issues 53, 55-58), I took you from a blank spreadsheet to a full working budget spreadsheet. Something similar is what I use twice a month to do my home budget. I keep a whole year of budget spreadsheets in one document, which means twice a month I copy the sheet, rename it, and make sure I get all the settings correct to put a copy of the current sheet at the end of the sheet tabs. Believe me, mistakes have been made. Curses have been breathed. Innocent computers have been threatened. |
- | 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.** |
- | The Manual Method | + | 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. J' |
+ | |||
+ | En temps voulu et puisque je suis un être humain faillible et imparfait, j'ai décidé que je devais réduire le nombre d' | ||
+ | |||
+ | **The Manual Method | ||
In order to appreciate a macro, you really have to understand what it's doing for you, and the mistakes you can make. Therefore, I will start with the description of the manual process that the macro makes simpler. There is actually more than one way to copy a sheet in Calc, but I will show you the dialog method with all the options. | In order to appreciate a macro, you really have to understand what it's doing for you, and the mistakes you can make. Therefore, I will start with the description of the manual process that the macro makes simpler. There is actually more than one way to copy a sheet in Calc, but I will show you the dialog method with all the options. | ||
Ligne 9: | Ligne 13: | ||
Right-click on the tab of the sheet you want to copy and select Move/Copy Sheet. The Move/Copy dialog appears. You then select Copy. One of the mistakes I have made is to forget this. I end up just renaming my sheet. Under “Insert Before” select the option at the end “-move to end position-”. If I forget this one, the sheet is placed before the current sheet rather than at the end where I want it. Finally, I have to rename the sheet. There have been times while trying to remember to get the other settings right, that I have forgotten to actually change the name. When I do, the copied sheet gets the name of the current sheet with a “_2” appended to the end. Click OK to execute the settings. | Right-click on the tab of the sheet you want to copy and select Move/Copy Sheet. The Move/Copy dialog appears. You then select Copy. One of the mistakes I have made is to forget this. I end up just renaming my sheet. Under “Insert Before” select the option at the end “-move to end position-”. If I forget this one, the sheet is placed before the current sheet rather than at the end where I want it. Finally, I have to rename the sheet. There have been times while trying to remember to get the other settings right, that I have forgotten to actually change the name. When I do, the copied sheet gets the name of the current sheet with a “_2” appended to the end. Click OK to execute the settings. | ||
- | 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 |
The macro is not very long (next page, top right) and you can easily type it in. You can also copy it from http:// | The macro is not very long (next page, top right) and you can easily type it in. You can also copy it from http:// | ||
Ligne 25: | Ligne 38: | ||
Exit Sub | Exit Sub | ||
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 37: | Ligne 67: | ||
• The prompt to show the user (“Enter Name for Copied Sheet:”). | • The prompt to show the user (“Enter Name for Copied Sheet:”). | ||
• 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).** |
- | 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. | + | 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 Sheet2 = "" | If Sheet2 = "" | ||
Ligne 49: | Ligne 92: | ||
Loop | Loop | ||
- | 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. ** |
- | 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. | + | 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. | ||
NOTE: The underscore (_) in the InputBox statement is used to break a long line up into shorter lines. LibreOffice Basic requires that the underscore is the last thing on the line. Nothing, not even a space, can follow it. When lines are joined in this way, LibreOffice Basic sees them as one line. | NOTE: The underscore (_) in the InputBox statement is used to break a long line up into shorter lines. LibreOffice Basic requires that the underscore is the last thing on the line. Nothing, not even a space, can follow it. When lines are joined in this way, LibreOffice Basic sees them as one line. | ||
+ | |||
+ | 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, | 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: |
• The sheet to copy from (Sheet1). | • The sheet to copy from (Sheet1). | ||
• The sheet to copy to (Sheet2). | • The sheet to copy to (Sheet2). | ||
Ligne 67: | Ligne 128: | ||
Creating the Calc Macro Module | Creating the Calc Macro Module | ||
- | 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. | ||
+ | |||
+ | 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 | ||
- | To create | + | 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 |
- | 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 | + | Tester la macro |
- | Test The Macro | + | Après avoir saisi et sauvé la macro, vous voulez la tester pour être sûr que toute la saisie est correcte. D' |
- | After typing | + | **NOTE: Back in Full Circle issue 64 (LibreOffice Part 17: Macros), I showed |
- | NOTE: Back in Full Circle issue 64 (LibreOffice Part 17: Macros), I showed | + | Macros |
+ | NOTE : Dans l' | ||
- | Macros like CopySheet | + | Les macros comme CopySheet |
issue89/libreoffice.1414776051.txt.gz · Dernière modification : 2014/10/31 18:20 de andre_domenech