Page 172 - IT_V1.0_Class10
P. 172

Passing Arguments to a Macro

              Argument is a way to provide more information to the macro. We can pass arguments to a macro. Basically,
              arguments are the values that are sent to macro. The called function can modify the value of the argument
              by using its reference passed in.

              To illustrate a function that accepts arguments, we will write a macro function that accepts two arguments
              and returns their multiplication.
              Let us create a function to multiply two cells:


              Function multiplycells(a, b)
              multiplycells =a*b

              End Function

              In the above code a, b written within multiplycells() are two arguments which are passed as values.

              Now, enter 12 in cell A1 and 23 in cell A2 and then, type =multiplycells(A1, A2) in any cell in the sheet.
              As soon as you press enter key you will see the multiplication as 276.











              Arguments Passed as Values
              When the arguments are passed by values, then the compiler copies the values of arguments in the function.
              The parameter in the called function is initialised with the value of the passed argument.

              As long as the parameter has not been declared as constant, the value of the parameter can be changed, but
              the changes are only performed within the scope of the called function only; they have no effect on the value
              of the argument in the calling function.
              Arguments passed to a macro from Calc are always values. It is not possible to know what cells, if any, are
              used. For example, =PositiveSum(A3) passes the value of cell A3 and PositiveSum has no way of knowing that
              cell A3 was used. If we know which cells are referenced rather than the values in the cells, pass the range as
              a string, parse the string and obtain the values in the referenced cells.

              Macros Act Like Built-In Functions

              Although Calc finds and calls macros as normal functions, they do not really behave as built-in functions. For
              example, macros do not appear in the function lists. It is possible to write functions that behave as regular
              functions by writing an Add-In.

              Accessing Cell Directly
              LibreOffice internal objects can be accessed directly to manipulate a Calc spreadsheet. For example, the
              macro in the following coding adds the values in cell A2 from every sheet in the current spreadsheet.
              ‘ThisComponent’ is set by  StarBasic  when  the  macro  starts  to  reference  the  current  spreadsheet.  A  Calc
              spreadsheet contains sheets ‘ThisComponent.getSheets()’. Use ‘getCellByPosition(col, row)’ to return a cell at
              a specific row and column.



                170     Information Technology Play (Ver 1.0)-X
   167   168   169   170   171   172   173   174   175   176   177