issue98: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 | ||
issue98:libreoffice [2015/07/16 09:09] – d52fr | issue98:libreoffice [2015/07/19 11:10] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
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.** | ||
- | LibreOffice Calc intègre des centaines de fonctions. Elles couvrent les mathématiques, | + | LibreOffice Calc intègre des centaines de fonctions. Elles couvrent les mathématiques, |
- | Cependant, je suis au courant | + | Cependant, je sais que le jour peut arriver où j' |
**Create User-Defined Functions | **Create User-Defined Functions | ||
Ligne 18: | Ligne 18: | ||
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.** | ||
+ | |||
+ | 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, | **To use the macro in our spreadsheet, | ||
Ligne 42: | Ligne 52: | ||
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.** | ||
+ | |||
+ | 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: | **You can use hard-coded numbers as x when you use the function: | ||
Ligne 58: | Ligne 92: | ||
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.** | ||
+ | |||
+ | 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 | **Passing Multiple Values | ||
Ligne 71: | Ligne 121: | ||
=Vol(SqrIt(A1), | =Vol(SqrIt(A1), | ||
=SqrIt(Vol(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(A1, A2, A3) | ||
+ | =Vol(SqrIt(A1), | ||
+ | =SqrIt(Vol(A1, | ||
**Passing A Cell Range | **Passing A Cell Range | ||
Ligne 77: | Ligne 140: | ||
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.** | ||
+ | |||
+ | Passer une plage de cellules | ||
+ | |||
+ | 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.** | **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. | **Finally, the function returns the value of TheSum. | ||
Ligne 89: | Ligne 160: | ||
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.1437030554.txt.gz · Dernière modification : 2015/07/16 09:09 de d52fr