issue98:libreoffice
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue98:libreoffice [2015/06/30 11:01] – créée auntiee | issue98:libreoffice [2015/07/19 11:10] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | LibreOffice Calc has hundreds of built-in functions. They cover mathematics, | + | **LibreOffice Calc has hundreds of built-in functions. They cover mathematics, |
- | However, I am aware that the day may come when I will need a highly specialized function, a function that does something that the built-in functions can't, or calculates an extremely complicated formula. Luckily, Calc allows you to use macros as functions. You can create your own functions and call them from within a cell's formula. You can even pass the values of cells or even cell ranges to your functions. | + | However, I am aware that the day may come when I will need a highly specialized function, a function that does something that the built-in functions can't, or calculates an extremely complicated formula. Luckily, Calc allows you to use macros as functions. You can create your own functions and call them from within a cell's formula. You can even pass the values of cells or even cell ranges to your functions.** |
- | Create User-Defined Functions | + | LibreOffice Calc intègre des centaines de fonctions. Elles couvrent les mathématiques, |
+ | |||
+ | Cependant, je sais que le jour peut arriver où j' | ||
+ | |||
+ | **Create User-Defined Functions | ||
In order to create user defined functions, you first need a module for storing the macros. Create a new Calc file and save it as MacroFunctions.ods. Open the LibreOffice Basic Macros dialog: Tools > Macros > Organize Macros > LibreOffice Basic. In the Macro From list box, select the name of your file, MacroFunctions.ods. Click New. In the New Module dialog, give the module the name UserFunctions and click OK. LibreOffice will open your new module in the LibreOffice Basic IDE. The module automatically defines a blank main subroutine. You can leave it or delete it. The choice is yours. You will not use it in this case. The IDE is a mini programming editor. As a built-in macro editor in an office suite, it's actually pretty good. There are tools for testing and tracking macros, but all that is beyond the scope of today' | In order to create user defined functions, you first need a module for storing the macros. Create a new Calc file and save it as MacroFunctions.ods. Open the LibreOffice Basic Macros dialog: Tools > Macros > Organize Macros > LibreOffice Basic. In the Macro From list box, select the name of your file, MacroFunctions.ods. Click New. In the New Module dialog, give the module the name UserFunctions and click OK. LibreOffice will open your new module in the LibreOffice Basic IDE. The module automatically defines a blank main subroutine. You can leave it or delete it. The choice is yours. You will not use it in this case. The IDE is a mini programming editor. As a built-in macro editor in an office suite, it's actually pretty good. There are tools for testing and tracking macros, but all that is beyond the scope of today' | ||
Ligne 13: | Ligne 17: | ||
End Function | End Function | ||
- | The first line is the function definition. It begins with the word “Function” showing it as a function rather than a subroutine (Sub). The main difference between a function and subroutine is that a function returns a value. Next is the function name, NumberFive, followed by parentheses. The parentheses are required, even if they are empty. The second line of this function is the body of the function. The body of a function can contain any number of lines. This function simply returns the number 5. We return a value from a function by setting the function name (without the parentheses) equal to the value we want to return. Finally, we have the line “End Function” which denotes the end of the function. Once you have the new function typed in, click the save button. | + | The first line is the function definition. It begins with the word “Function” showing it as a function rather than a subroutine (Sub). The main difference between a function and subroutine is that a function returns a value. Next is the function name, NumberFive, followed by parentheses. The parentheses are required, even if they are empty. The second line of this function is the body of the function. The body of a function can contain any number of lines. This function simply returns the number 5. We return a value from a function by setting the function name (without the parentheses) equal to the value we want to return. Finally, we have the line “End Function” which denotes the end of the function. Once you have the new function typed in, click the save button.** |
- | To use the macro in our spreadsheet, | + | Créer des fonctions propres à l' |
+ | |||
+ | Pour créer des fonctions propres à l' | ||
+ | |||
+ | Function NumberFive() | ||
+ | |||
+ | NumberFive = 5 | ||
+ | |||
+ | End Function | ||
+ | |||
+ | **To use the macro in our spreadsheet, | ||
=NumberFive() | =NumberFive() | ||
Ligne 37: | Ligne 51: | ||
End Function | End Function | ||
- | The signature of your new function is like the first one except it has a variable x in the parentheses. The x represents the value you are passing to the function. In the body of the function, it checks x to make sure it is a number. If it is a number, the function multiplies x by itself and returning the result. If x is not a number, it returns 0.0. | + | The signature of your new function is like the first one except it has a variable x in the parentheses. The x represents the value you are passing to the function. In the body of the function, it checks x to make sure it is a number. If it is a number, the function multiplies x by itself and returning the result. If x is not a number, it returns 0.0.** |
- | You can use hard-coded numbers as x when you use the function: | + | Pour utiliser la macro dans notre tableur, sélectionner une cellule et taper |
+ | |||
+ | =NumberFive() | ||
+ | |||
+ | Quand vous appuyez sur Entrée, le nombre 5 apparaît dans la cellule où vous avez tapé la formule qui utilise le nom de la fonction. Vous pouvez même utiliser la fonction dans une plus grande formule comme | ||
+ | |||
+ | =2 + NumberFive() - 3 | ||
+ | |||
+ | dont le résultat est 4. | ||
+ | |||
+ | Passer des arguments | ||
+ | |||
+ | C'est bien d' | ||
+ | |||
+ | Function SqrIt(x) | ||
+ | If IsNumeric(x) Then | ||
+ | SqrIt = x * x | ||
+ | Else | ||
+ | SqrIt = 0.0 | ||
+ | End If | ||
+ | End Function | ||
+ | |||
+ | La déclaration de la nouvelle fonction est presque identique à la première, à l' | ||
+ | |||
+ | **You can use hard-coded numbers as x when you use the function: | ||
=SqrIt(3) | =SqrIt(3) | ||
Ligne 53: | Ligne 91: | ||
=NumberFive() + SqrIt(C4) | =NumberFive() + SqrIt(C4) | ||
- | By default in LibreOffice Basic, arguments are passed by value. When you pass a cell reference to a function, the function does not receive a reference to the actual cell. Instead, the function receives the value of the cell or the result of its formula. If the cell A1 contains the number 3, or a formula that results in 3, the function SqrIt receives the value 3 and not a reference to A1. | + | By default in LibreOffice Basic, arguments are passed by value. When you pass a cell reference to a function, the function does not receive a reference to the actual cell. Instead, the function receives the value of the cell or the result of its formula. If the cell A1 contains the number 3, or a formula that results in 3, the function SqrIt receives the value 3 and not a reference to A1.** |
- | Passing Multiple Values | + | Vous pouvez donner des valeurs numériques à x quand vous utilisez la fonction : |
+ | |||
+ | =SqrIt(3) | ||
+ | =SqrIt(2.4) | ||
+ | |||
+ | ou faire référence à des cellules pour x : | ||
+ | |||
+ | =SqrIt(A1) | ||
+ | =SqrIt(B4) | ||
+ | |||
+ | ou utiliser la fonction dans une formule plus grande : | ||
+ | |||
+ | =NumberFive() + SqrIt(C4) | ||
+ | |||
+ | Par défaut dans LibreOffice Basic, les arguments sont passés par valeur. Quand vous passez la référence d'une cellule à une fonction, la fonction ne reçoit pas une référence de la vraie cellule. A la place, la fonction reçoit la valeur de la cellule ou le résultat de la formule qu' | ||
+ | |||
+ | **Passing Multiple Values | ||
While passing one value to a function is useful, sometimes you need to pass multiple values. For example, you create a function that calculates the volume of a cuboid. A simplistic version of this function is represented by the function Vol (shown below). | While passing one value to a function is useful, sometimes you need to pass multiple values. For example, you create a function that calculates the volume of a cuboid. A simplistic version of this function is represented by the function Vol (shown below). | ||
Ligne 62: | Ligne 116: | ||
We can use this function much as we have the others. | We can use this function much as we have the others. | ||
+ | |||
+ | =Vol(2, 3, 4) | ||
+ | =Vol(A1, A2, A3) | ||
+ | =Vol(SqrIt(A1), | ||
+ | =SqrIt(Vol(A1, | ||
+ | |||
+ | Passer des valeurs multiples | ||
+ | |||
+ | Si passer une valeur à une fonction est utile, parfois vous avez besoin de passer plusieurs valeurs. Par exemple, vous créez une fonction qui calcule le volume d'un parallélépipède. Une version simpliste de cette fonction est représentée par la fonction Vol (voir ci-dessous). | ||
+ | |||
+ | Dans Vol, vous passez le trois valeurs nécessaires pour calculer le volume d'un parallélépipède. Les parenthèses contiennent trois variables séparées par des virgules. Dans la déclaration If, la fonction vérifie si chacune des variables est un nombre, et si c'est vrai, elle retourne le résultat du produit des valeurs. Sinon, elle retourne 0.0. | ||
+ | |||
+ | Nous pouvons utiliser cette fonction comme nous l' | ||
=Vol(2, 3, 4) | =Vol(2, 3, 4) | ||
Ligne 68: | Ligne 135: | ||
=SqrIt(Vol(A1, | =SqrIt(Vol(A1, | ||
- | Passing A Cell Range | + | **Passing A Cell Range |
So far you have passed single-cell references to your functions, but you might want to pass a cell range rather than a single-cell reference. A cell range is passed to Basic functions as an array. An array is a collection of values. Cell ranges come across as multidimensional arrays, i.e. x(Row, Column). You can use a function named IsArray to determine whether the value being passed is an array. The function SumIt demonstrates the use of a cell range reference. SumIt will accept a single value or a cell range. If the argument is a single value, it returns the value. If the argument is a cell range, it sums the values of all the cells in the range. Code for this is shown on the next page, right hand side. | So far you have passed single-cell references to your functions, but you might want to pass a cell range rather than a single-cell reference. A cell range is passed to Basic functions as an array. An array is a collection of values. Cell ranges come across as multidimensional arrays, i.e. x(Row, Column). You can use a function named IsArray to determine whether the value being passed is an array. The function SumIt demonstrates the use of a cell range reference. SumIt will accept a single value or a cell range. If the argument is a single value, it returns the value. If the argument is a cell range, it sums the values of all the cells in the range. Code for this is shown on the next page, right hand side. | ||
- | The body of the function defines three variables, TheSum, iRow, and iCol. iRow and iCol are defined as Integers, which are whole numbers, i.e. 1, 2, 3. These two variables are the row and column placeholders. TheSum is defined as a Double, which is a real, floating point number, i.e. 2.34, and will contain our running total. TheSum is assigned an initial value of 0.0. | + | The body of the function defines three variables, TheSum, iRow, and iCol. iRow and iCol are defined as Integers, which are whole numbers, i.e. 1, 2, 3. These two variables are the row and column placeholders. TheSum is defined as a Double, which is a real, floating point number, i.e. 2.34, and will contain our running total. TheSum is assigned an initial value of 0.0.** |
- | You have only one argument in your function definition, x. The user could send a single value, single cell reference, or a cell range. The function tests for this using an If statement. It uses the IsArray function to check if x is an array. If it is, SumIt sets up two loops. The outer loop cycles through the row lower-to-upper values. The inner loop cycles through the column lower-to-upper values. The lower values are obtained through the LBound function, and the upper values are obtained through the UBound function. Both functions take a reference to the array, x, and the dimension of the array, 1 or 2. Remember, it references the values through x(row, column). The row is the first level, and the column is the second level. In the middle of the two loops, SumIt takes the current sum, TheSum, and adds the value of the current cell in the array to it, x(iRow, iColumn). Once the first row is complete and iColumn has reached its UBound limit, the iRow increases by one, and the inner loop starts over again. This procedure continues until both iRow and iColumn have reached their UBound limits. The two Next statements end the two loops once they reach the UBound limit for their range. The Else statement handles the case when x is not an array but a single value. It sets TheSum equal to the value of the single argument. | + | Passer une plage de cellules |
- | Finally, the function returns the value of TheSum. | + | Jusqu' |
+ | |||
+ | Le corps de la fonction définit trois variables, TheSum, iRow, et iCol. iRow et iCol sont définis comme entiers, c' | ||
+ | |||
+ | **You have only one argument in your function definition, x. The user could send a single value, single cell reference, or a cell range. The function tests for this using an If statement. It uses the IsArray function to check if x is an array. If it is, SumIt sets up two loops. The outer loop cycles through the row lower-to-upper values. The inner loop cycles through the column lower-to-upper values. The lower values are obtained through the LBound function, and the upper values are obtained through the UBound function. Both functions take a reference to the array, x, and the dimension of the array, 1 or 2. Remember, it references the values through x(row, column). The row is the first level, and the column is the second level. In the middle of the two loops, SumIt takes the current sum, TheSum, and adds the value of the current cell in the array to it, x(iRow, iColumn). Once the first row is complete and iColumn has reached its UBound limit, the iRow increases by one, and the inner loop starts over again. This procedure continues until both iRow and iColumn have reached their UBound limits. The two Next statements end the two loops once they reach the UBound limit for their range. The Else statement handles the case when x is not an array but a single value. It sets TheSum equal to the value of the single argument.** | ||
+ | |||
+ | Il n'y a qu'un seul argument dans la définition de la fonction, x. L' | ||
+ | |||
+ | **Finally, the function returns the value of TheSum. | ||
This function allows you to actually use a cell range as your argument. You can also use a single value or cell as the argument. You can even use the function as part of a bigger function. | This function allows you to actually use a cell range as your argument. You can also use a single value or cell as the argument. You can even use the function as part of a bigger function. | ||
Ligne 84: | Ligne 159: | ||
=SqrIt(SumIt(A1: | =SqrIt(SumIt(A1: | ||
- | While Calc provides you with hundreds of functions for manipulating the data in your spreadsheets, | + | While Calc provides you with hundreds of functions for manipulating the data in your spreadsheets, |
+ | |||
+ | Enfin, la fonction retourne la valeur de TheSum. | ||
+ | |||
+ | Cette fonction vous permet d' | ||
+ | |||
+ | =SumIt(A3: | ||
+ | =SumIt(A1) | ||
+ | =SqrIt(SumIt(A1: | ||
+ | |||
+ | Bien que Calc vous fournisse des centaines de fonctions pour manipuler des données dans vos feuilles de calcul, vous pouvez avoir besoin occasionnellement d'une fonction spécialisée qu'il n'est pas facile de construire en utilisant les fonctions intégrées de Calc. Une fois que vous avez créé une fonction en Basic, vous pouvez l' |
issue98/libreoffice.1435654879.txt.gz · Dernière modification : 2015/06/30 11:01 de auntiee