1<?xml version="1.0" encoding="UTF-8"?> 2 3 4<!-- 5 *********************************************************************** 6 * 7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8 * 9 * Copyright 2000, 2010 Oracle and/or its affiliates. 10 * 11 * OpenOffice.org - a multi-platform office productivity suite 12 * 13 * This file is part of OpenOffice.org. 14 * 15 * OpenOffice.org is free software: you can redistribute it and/or modify 16 * it under the terms of the GNU Lesser General Public License version 3 17 * only, as published by the Free Software Foundation. 18 * 19 * OpenOffice.org is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * GNU Lesser General Public License version 3 for more details 23 * (a copy is included in the LICENSE file that accompanied this code). 24 * 25 * You should have received a copy of the GNU Lesser General Public License 26 * version 3 along with OpenOffice.org. If not, see 27 * <http://www.openoffice.org/license.html> 28 * for a copy of the LGPLv3 License. 29 * 30 ************************************************************************ 31 --> 32 33<helpdocument version="1.0"> 34<meta> 35<topic id="textscalcguideuserdefined_functionxml" indexer="include" status="PUBLISH"> 36<title id="tit" xml-lang="en-US">User-Defined Functions</title> 37<filename>/text/scalc/guide/userdefined_function.xhp</filename> 38</topic> 39<history> 40<created date="2003-10-31T00:00:00">Sun Microsystems, Inc.</created> 41<lastedited date="2006-02-07T11:28:53">FPE: Deleted Screenshot and cleaned up 42yj - checked</lastedited> 43</history> 44</meta> 45<body> 46<bookmark xml-lang="en-US" branch="index" id="bm_id3155411"><bookmark_value>functions; user-defined</bookmark_value> 47<bookmark_value>user-defined functions</bookmark_value> 48<bookmark_value>Basic IDE for user-defined functions</bookmark_value> 49<bookmark_value>IDE; Basic IDE</bookmark_value> 50<bookmark_value>programming;functions</bookmark_value> 51</bookmark> 52<paragraph role="heading" id="hd_id3155411" xml-lang="en-US" level="1" l10n="U" oldref="1"><variable id="userdefined_function"><link href="text/scalc/guide/userdefined_function.xhp" name="Defining Functions Yourself">User-Defined Functions</link> 53</variable></paragraph> 54<paragraph role="paragraph" id="par_id3153969" xml-lang="en-US" l10n="U" oldref="2">You can apply user-defined functions in $[officename] Calc in the following ways:</paragraph> 55<list type="unordered"> 56<listitem> 57<paragraph role="listitem" id="par_id3145366" xml-lang="en-US" l10n="CHG" oldref="4">You can define your own functions using the Basic-IDE. This method requires a basic knowledge of programming.</paragraph> 58</listitem> 59<listitem> 60<paragraph role="listitem" id="par_id3153768" xml-lang="en-US" l10n="CHG" oldref="3">You can program functions as <link href="text/scalc/01/04060111.xhp" name="add-ins">add-ins</link>. This method requires an advanced knowledge of programming.</paragraph> 61</listitem> 62</list> 63<paragraph role="heading" id="hd_id3149260" xml-lang="en-US" level="2" l10n="CHG" oldref="6">Defining A Function Using %PRODUCTNAME Basic</paragraph> 64<list type="ordered"> 65<listitem> 66<paragraph role="listitem" id="par_id3148456" xml-lang="en-US" l10n="CHG" oldref="7">Choose <item type="menuitem">Tools - Macros - Organize Macros - %PRODUCTNAME Basic</item>.</paragraph> 67</listitem> 68<listitem> 69<paragraph role="listitem" id="par_id3154510" xml-lang="en-US" l10n="U" oldref="8">Click the <emph>Edit</emph> button. You will now see the Basic IDE.</paragraph> 70</listitem> 71<listitem> 72<paragraph role="listitem" id="par_id3150327" xml-lang="en-US" l10n="CHG" oldref="9">Enter the function code. In this example, we define a <item type="literal">VOL(a; b; c)</item> function that calculates the volume of a rectangular solid with side lengths <item type="literal">a</item>, <item type="literal">b</item> and <item type="literal">c</item>:</paragraph> 73<paragraph role="code" id="par_id9797426" xml-lang="en-US" l10n="NEW" localize="false">Function VOL(a, b, c)<br/>VOL = a*b*c<br/>End Function</paragraph> 74</listitem> 75<listitem> 76<paragraph role="listitem" id="par_id3155443" xml-lang="en-US" l10n="U" oldref="10">Close the Basic-IDE window.</paragraph> 77<paragraph role="listitem" id="par_id3150043" xml-lang="en-US" l10n="CHG" oldref="11">Your function is automatically saved in the default module and is now available. If you apply the function in a Calc document that is to be used on another computer, you can copy the function to the Calc document as described in the next section.</paragraph> 78</listitem> 79</list> 80<paragraph role="heading" id="hd_id3147340" xml-lang="en-US" level="2" l10n="U" oldref="18">Copying a Function To a Document</paragraph> 81<paragraph role="paragraph" id="par_id3145232" xml-lang="en-US" l10n="CHG" oldref="19">In stage 2 of "Defining A Function Using %PRODUCTNAME Basic", in the <emph>Macro</emph> dialog you clicked on <emph>Edit </emph>. As the default, in the <emph>Macro from</emph> field the <emph>My Macros - Standard - Module1</emph> module is selected. The <emph>Standard</emph> library resides locally in your user directory.</paragraph> 82<paragraph role="paragraph" id="par_id3154022" xml-lang="en-US" l10n="U" oldref="20">If you want to copy the user-defined function to a Calc document:</paragraph> 83<list type="ordered"> 84<listitem> 85<paragraph role="listitem" id="par_id3150304" xml-lang="en-US" l10n="CHG" oldref="21">Choose <item type="menuitem">Tools - Macros - Organize Macros - %PRODUCTNAME Basic</item> .</paragraph> 86</listitem> 87<listitem> 88<paragraph role="listitem" id="par_id3150086" xml-lang="en-US" l10n="CHG" oldref="22">In the <emph>Macro from</emph> field select <emph>My Macros - Standard - Module1</emph> and click <emph>Edit</emph>.</paragraph> 89</listitem> 90<listitem> 91<paragraph role="listitem" id="par_id3166430" xml-lang="en-US" l10n="CHG" oldref="23">In the Basic-IDE, select the source of your user-defined function and copy it to the clipboard.</paragraph> 92</listitem> 93<listitem> 94<paragraph role="listitem" id="par_idN1081D" xml-lang="en-US" l10n="NEW">Close the Basic-IDE.</paragraph> 95</listitem> 96<listitem> 97<paragraph role="listitem" id="par_id3150517" xml-lang="en-US" l10n="CHG" oldref="24">Choose <item type="menuitem">Tools - Macros - Organize Macros - %PRODUCTNAME Basic</item> .</paragraph> 98</listitem> 99<listitem> 100<paragraph role="listitem" id="par_id3145384" xml-lang="en-US" l10n="CHG" oldref="25">In the <emph>Macro from</emph> field select <emph>(Name of the Calc document) - Standard - Module1</emph>. Click <emph>Edit</emph>.</paragraph> 101</listitem> 102<listitem> 103<paragraph role="listitem" id="par_id3148699" xml-lang="en-US" l10n="U" oldref="26">Paste the clipboard contents in the Basic-IDE of the document.</paragraph> 104</listitem> 105</list> 106<paragraph role="heading" id="hd_id3153305" xml-lang="en-US" level="2" l10n="U" oldref="12">Applying a User-defined Function in $[officename] Calc</paragraph> 107<paragraph role="paragraph" id="par_id3148869" xml-lang="en-US" l10n="CHG" oldref="13">Once you have defined the function <item type="literal">VOL(a; b; c)</item> in the Basic-IDE, you can apply it the same way as the built-in functions of $[officename] Calc.</paragraph> 108<list type="ordered"> 109<listitem> 110<paragraph role="listitem" id="par_id3148606" xml-lang="en-US" l10n="CHG" oldref="14">Open a Calc document and enter numbers for the function parameters <item type="literal">a</item>, <item type="literal">b</item>, and <item type="literal">c</item> in cells A1, B1, and C1.</paragraph> 111</listitem> 112<listitem> 113<paragraph role="listitem" id="par_id3156019" xml-lang="en-US" l10n="CHG" oldref="15">Set the cursor in another cell and enter the following:</paragraph> 114<paragraph role="code" id="par_id3155264" xml-lang="en-US" l10n="U" oldref="16">=VOL(A1;B1;C1)</paragraph> 115</listitem> 116<listitem> 117<paragraph role="listitem" id="par_id3146776" xml-lang="en-US" l10n="CHG" oldref="17">The function is evaluated and you will see the result in the selected cell.</paragraph> 118</listitem> 119</list> 120<section id="relatedtopics"> 121<embed href="text/scalc/guide/calculate.xhp#calculate"/> 122<embed href="text/scalc/guide/formula_enter.xhp#formula_enter"/> 123</section> 124</body> 125</helpdocument> 126