Quattro Pro Formulas, Functions, and Macros

Preview:

Citation preview

Quattro Pro Formulas,Functions, and Macros

Contents

Introduction iThe reason for this book . . . . . . . . . . . . . . . . . . . . . . . . . . . iConventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iiQP quirks, generally

The GetCellValue command . . . . . . . . . . . . . . . . . . . . . . . . . 237The Eval command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238Importing a QP block into PerfectScript array . . . . . . . . . . . . . . . 238Running PerfectScript macros: {PlayPerfectScript}, {Exec}

ii Introduction

program that displayed everything on a screen containing 24 rows of up to 80 char-acters. The program started well, but changes in the ownership, programmers, op-

Part I

Chapter 1

Basic Formulas

1. Basic Formulas 5

A shortcut method of making absolute or partially absolute references involvesusing the [F4] key. While typing a formula referencing cell A2, for instance, whilethe insertion point is in or just after A2, pressing [F4] will convert A2 into $A:$A$2.Pressing [F4]

1. Basic Formulas 7

How to set up a database showing interest compoundingmonthly

The task is to demonstrate how interest accumulates on a specific sum ($100)at a specific annual rate (18%) over 48 monthly periods. Table 1.10 shows one wayto set it up.

Table 1.10: Compound interest table

A B C D

Chapter 2

Block (Array) Formulas

QP has a possibly unique shorthand way for making a formula that operateson a block (or array) of data. The formula can populate another block of datawith calculations using the same formula on each cell in the block, or all of those

12 2. Block Formulas

@SUM($A$1..$A$100=A1)), place that formula in B1 or some other parallel with A1,and then copy it down in parallel with A1..A100.

How to count or sum all cells that meet two conditions

In this sample (Table 2.3), we will first count the number of rows that are Homeand Even, Home and Odd, Away and Even, or Away and Odd.

2. Block Formulas 13

C column that parallel “Home” in the A column. The way to do this is to place thefollowing formula in E1 and copy it into E2..E8:

@SUM(($A$1..A1="Home")*($C$1..C1))

Table 2.4: Running Totals

A B C D E

2. Block Formulas 17

Instead of 10, a more generic method would be to substitute @MAX(B1..B9)+1 for10.

How to mark possible duplicate entries within a time range

In this example (Table 2.9), the task is to mark entries that may be duplicates,

Part II

Quattro Pro Functions

18

Chapter 4

The Properties of a Cell, Page, etc.

Thebasichefunctionshefoterdeaptmininghet(The)29(contenthe)29(oter)29(p(Pr5(erties)29(ofes)29(ahe)29(c(Cehe)29(oter)30(somec.)] -16.936 -13.54913 Td ot(Trop)-5(divisionop)-6(ofes)-5(QPop)-6(arec.)]TJ513.o.909871 T11 24.5Tf 3 Td @CELLc.)]TJ/F3.o.909871 T28.186Tf 3 Td ,c.)]TJ513.o.909871 T549.9Tf 3 Td @CELLPOINTERc.)]TJ/F3.o.909871 T67.647Tf 3 Td ;est(Th.)]TJ513.o.909871 T3 2205Tf 3 Td @ISc.

TsThefunctionshereturnheinfotmationopaboutopc5(eainhe)45(attributties)44(ofes)44(aop)44(c5ll.)-544([(The)45(usTro.)] -16.936 -13.5513 Td speci�ties)-6(t(The)55(attributtes)-6(inhe)-5(ahe)-6(texthe)-5(argument.e.)]TJ513.o.909871 T3o7.438Tf 3 Td @CELLPOINTER(e.)]TJ523.o.909871 T73.284Tf 3 Td Attributte.

32 4. Properties of Cells, Pages, etc.

5. Functions for Coordinates 37

Page The Page

5. Functions for Coordinates 39

Table 5.4: Using @@ in lieu of named data ranges

40 5. Functions for Coordinates

Table 5.6: @BLOCKNAMES illustration

A B C D E

44 6. Text Functions

@LENGTH(A1)-@LENGTH(@SUBSTITUTE(A1,"+",""))

This formula subtracts from the length of the original string the length of a substitutestring where + is replaced by nothing (the empty string). That should equal thenumber of occurrences of the character in question.

How to count the number of words in a particular cell

6. Text Functions 45

returns the field or an ERR, @IF functions would first test whether the @FIELD func-tion for the largest number returns an ERR; if not, it returns that field; if so, it nestsan @IF function that does the same for the next largest number, and so on until onlyone field is left. Table 6.3 is a set of @IF functions that test for 2 to 5 words. Theadditional coloration shows what is added for each additional word. Though con-ceptually straightforward, and quick and relatively easy for 2–3 words, this methodobviously becomes more cumbersome as the number of words increases.

Table 6.3: Nesting if-tests

Words Formula (broken into parts for clarity)

2 @IF(@ISERR(@FIELD(A1,2," ")),@FIELD(A1,1," "),@FIELD(A1,2," "))

3 @IF(@ISERR(@FIELD(A1,3," ")),

@IF(@ISERR(@FIELD(A1,2," ")),@FIELD(A1,1," "),@FIELD(A1,2," "))

,@FIELD(A1,3,"

Chapter 7

Math Functions

QP has a tremendous number of functions for advanced math, geometry, statis-tics, engineering, finance, and more, but this text will focus only on more basicfunctions.

Adding: @SUM, @TOTAL, @SUBTOTAL, @SUMIF

Adding sets of numbers is perhaps the most basic function of a spreadsheet, and@SUM(Block ) is therefore probably the most basic function in QP.

@SUMNEGATIVE and @SUMPOSITIVE total only those items in the block that arenegative or positive, respectively.

@TOTAL appears to be a variety of @SUM that totals all numbers in a block exceptthose created by an @SUBTOTAL function, which appears to differ from @SUM only in

7. Math Functions 53

Table 7.5: Comparison of @INT, @ROUND, and @TRUNC

A B C D E F G

Chapter 8

60 8. Date and Time Functions

Table 8.1: Values returned by @DATEINFO

A B

Chapter 9

Converting between Text and Numbers

There are many reasons why one might want to convert numbers (includingdates, times, and currency) into text, and vice versa. QP supplies a number offunctions for doing so.

Converting numbers to generic number-like text:@STRING, @FIXED

Before dealing with functions that convert a number to text, it may be sufficient

70 9. Functions for Converting Text and Numbers

ment 1 will do so. Thus @FIXED(1234.567,2,1) will yield 1234.57. As far as I cantell, you might as well use @STRING for this.

@CONCATENATE(Number ) converts the number in its general format into text. If

76 9. Functions for Converting Text and Numbers

How to add one hour to a given time

If you want to create a number representing the time one hour later than a giventime in a text field containing a date and time, you will need to convert the dateand time to a number, and add 1/24 (one hour, out of a 24-hour day). For example,if

9. Functions for Converting Text and Numbers 77

Appendix: Custom numeric formats

Creating custom numeric formats

To create a custom numeric format for a spreadsheet, follow these steps.

1. Open the Selection Properties dialog box. That can be done by pressingformats

Chapter 10

Getting information about and from adatabase

The functions in this section assist with using databases, which I understand as

82

84 10. Database Functions

function returns the desired information about the unique entity that is the subjectof the database. The index column need not be sorted in numerical order.

If ExactMatch? is 1 (the default), QP expects the index column to be sortednumerically, and when it is, the function selects the first row with an exact matchin the index column if there is an exact match, but if there is no exact match inthe index column, it selects the row with the next lowest value in the index column(which is the row above the point where IndexValue would be, if it were in theindex column). Thus, if the index column has items 1 and 2, and the IndexValue

argument is 1.5, QP will use the row with item 1 in the index column. The functionreturns ERR only if the

90

thefunctioninit1Bymodifyingtheoffsetsinthisformula(the-1intheRowOffset#

(second)argumentandthe-2intheColumnOffset#

containingtheformula.

TheCelltotheleft,withcircularitythesamething1IfthefunctionisincellD6,thisworks:

@CELL("col",D6)-2)Andlikewise,ifwedonotknowthehostcellinadvance,QPwilladjustiffromthis:

@CELL("col",c(0)r(0))-2)Aversionusing@ADDRESSisalsoavailable.

@CELL("col",c(0)r(0)))Howtoretrievedatafromcellsrelativetothecurrentcellgettingtheircoordinates,because@INDEXaccepts@ROW

Because@INDEXneedsaBlock

describedinthelastapplication1Sinceitistext,itwillneedtobewrappedinan@@

functiontouseascoordinatesfor@INDEX:@@(@OFFSET(@@("A1..A1"),0,0,@ROW,@COLUMN))SameRow,ColumnAcoordinatesofthebasicblockinsidean@INDEXfunctionasfollows:

0,@ROW-1)Wehavetosubtract1fromthelast@ROW

with1,not 1QPwrapsthisfunctionin@ARRAY

works:@@(@ADDRESS(@CELL("row",D6),1))Thatfunctioncanbecreatedwithoutknowingthecell,again,bythis:@@(@ADDRESS(@CELL("row",c(0)r(0)),1))SameColumn,Row1

10. Database Functions 95

Table 10.5: Getting the last five values in a column

A B

98 10. Database Functions

@IF(@NOT(@ISERR(@MATCH(A2,$A$1..A1,0))),

"Match!!","")

This tests each cell from A2 down to see if it matches an earlier one, and if it does,it shows Match!! in the cell.

How to return the (first) matching row of data in adatabase

A common task is for functions to find an item in a database and return associ-

10. Database Functions 99

that returned later matches with an additional parameter, as exists in functions like@LARGEST.) What if we want to return every match, not just the first one?

Notebook queries can be set up to get all matches fairly painlessly, but theydo require their own set-up and manual operation, even if that is made easier bymacros. The QuickFilter can also be used to get useful information. Each requiresmanual operation.

In this section, we want to do it with @Functions, which can be used, but itrequires some careful work.

We will use random data (4 people, 3 instances each, randomly arranged) asshown in A1..B13 of Table 10.7. There are at least three strategies for gettingmatches beyond the first one.

• One method is to restrict the block that @MATCH searches in each row, so as to

10. Database Functions

10. Database Functions 103

How to find the top three scores in a database

Taking the last application and going one step further, we now use functions toreturn the top 3 values in a database with random scores. There are again at leasttwo ways to do it, depending on whether we use the technique of restricting therange (method 1) or assigning a unique value (method 2).

10. Database Functions 105

Part III

Quattro Pro Macros

106

11011.IntroductiontoQuattroProMacros

TheTestargumenttestsapropositionthateitherevaluatesastrue(1,orap-

parentlyanynon-zeronumber)orfalse(0).TheTestargumentusuallyinvolves

equationsandinequalities(suchas=,>,<,>=,

11. Introduction to Quattro Pro Macros 113

Stop# stops the {For} command from executing the macro at MacroCell, if thevalue in CounterCell has passed the Stop#. Thus, if the CounterCell merelyequals the Stop#

114 11. Introduction to Quattro Pro Macros

2. Use an {If}

11. Introduction to Quattro Pro Macros 115

124 13. Navigation and Selection Macros

EditGoto

{EditGoto CellorBlock<,ExtendSelection> } selects the Cell or Block spe-cified as the first argument. If you select more than one cell in the CellorBlock

argument, the active cell will be the topmost, leftmost cell in the Block.The unique feature of {EditGoTo}

13. Navigation and Selection Macros 125

How to move to the leftmost cell on the row

There are several possible ways to move to the leftmost cell in the same row.{Left @CELLPOINTER("col")-1}

{SelectBlock @OFFSET(A1,@CELLPOINTER("row")-1,0)}

{Left 1000}

The last one works if you will never have more than 1000 columns to the right; thedefault maximum is 256).

How to move to the first blank cell at the bottom of acolumn of data

Here are some ways to move to the first blank cell at the bottom of data in thatA column.

128 13. Navigation and Selection Macros

{Shift+Right 3}

To do the same with @OFFSET, the programmer can navigate to the topmost, left-most cell in the block and use an R1C1 reference to anchor the block. For example,to select the 1x4 block consisting of the current cell and three cells to the right, use:

{SelectBlock @OFFSET(c(0)r(0),0,0,1,4)}

How to select the same cells on restarting, after closing thefile

The macro in Table 13.8 illustrates how you can save the selected cell or cells atthe time of closing a file and have QP select the same cells when you re-open thefile. This is true even if you have selected non-contiguous blocks of cells. Cell B2 isnamed _NBStartMacro, and B5 is named _NBExitMacro. Naming the cells that way

132 14. Interface Macros

width of 768, the bottommost ScreenY value that would display a characterwas 45. Again, your monitor and resolution may differ from this.

Expiration This argument is optional. If it is omitted, the message remains untilthe user presses a key. The Expiration argument should contain a number

134 14. Interface Macros

Table 14.2: Running different commands, depending on Yes/No choice

144 15. Cell Property Macros

Table 15.3: Copying Properties

A

1 {SelectBlock SourceCell}

2 {EditCopy}

3 {SelectBlock TargetCell}

4 {PasteSpecial Properties}

How to copy selected cell properties from one cell toanother

One can transfer selected cell properties from a source cell to a target cell byusing either of the {SetProperty}either of the{SetProperty}eith21.5667(of)-337(thObj)-3e

15. Cell Property Macros 147

The macro then drops down and line and loops back to the top. The results are asdepicted in the A and B columns.

Table 15.10: Coloring text based on conditions in other cells

A B C D E

1 a b 1 {If @CELLPOINTER("contents")=""}{Up}{End}{Up}{Quit}

2 a b 2 {If @CELLPOINTER("contents")=1}

15. Cell Property Macros 149

Table 15.13: Zebra-striping a block

A B

154 16. Cell Content Macros

This is a very quick and easy way to replicate particular blocks of data in a newlocation. However, it has a few possible drawbacks:

• It copies all formatting from the source cells, and you may only want numericformatting.

• I have also occasionally sensed that it is slower than other methods, per-haps since {BlockValues}

16. Cell Content Macros 155

option must be set to 0: {BlockCopy Source,Target,1,?,0}. If values areomitted, the target cell would remain unchanged.

In these and the following examples, a question mark is left in the formulawhere the user can choose 1 or 0 for other options. Some choice of earlieroptions must be made in order to effectively choose a later option.

The Formulas? andop436(provi-27843ese)op436(indir278op436(way7843esr)-2436(tes8op436(ifop436((formu-153.463r)]TJ 0 -13.54e)-374(f-327rk)-3727rk-37028((formand)]TJ/F51 10.991 f 377.804 y)-516(Macros)]TJ/F35 10.909.393377.804 t)-278727rwhich37028(k)-3727rr)-37wi70(ch27rlack(follo27rk-37028(QP)90 0 To27rA)10(0-278(o27rclf)-(follo27r(formand)]TJ/F51 10.9-111.433a)]TJ 0 -13.5Source(Macros)]TJ/F35 10.909.029377.804 t)-2omit98be)-t(folormand)]TJ/F51 10.9f 102f 22.775 0 Td [(Formulas?)]TJ/F35 10.905.016 22.775 r)-2794se The

16. Cell Content Macros 159

48-63 % (percent, 0-15 decimals) Same

64-79 , (comma, 0-15 decimals) Same

80-95 not mentioned 42,072; 42,071.6; 42,071.57;42,071.567; 42,071.5670;42,071.56700; etc.This is comma format

160 16. Cell Content Macros

Table 16.3: Search and replace by macro

A B

164 16. Cell Content Macros

Removing Prefixes. If the column contains text beginning with a prefix, a macrocan easily remove the prefix:

{If @CELLPOINTER("type")<>"b"}

{Edit}{Home}{Del}{D}

{Branch c(0)r(0)}

Incrementing cells. If you need to add one to each cell in the column, use this:{If @CELLPOINTER("type")<>"b"}

{Putcell c(0)r(0)+1}

16. Cell Content Macros 165

How to override in-cell formatting

QP displays the properties of the selected cell on its property bar, but the usercan select all or part of the contents of the cell and apply different formatting. Thiscan cause confusion when the apparent format of the cell differs from the format

17. Date and Time Macros 167

1. Click

17. Date and Time Macros 169

C1), to be run oo2me Macros

180 18. Database Retrieval Macros

rows. There appears to be no generic command, so Table 18.16 shows one way todo it, using the techniques discussed above.1

Table 18.16: Transposing data from consecutive pages onto consecutive rows

AAAAAAA

18. Database Retrieval Macros 183

items by placing terms capable of exact matches in E:A2..E5, and that to search forone of two terms in the same category, you need to put both terms in two rowsunder the correct category (e.g., E:D2..D3) and expand the Criteria Table. Thoughthose concepts work, they are neither necessary nor desirable. Instead, a placementlike 4.32 in E:D2 above is really a shorthand for

18. Database Retrieval Macros 185

Table 18.20: Macro alternative to simple Database Queries

186 18. Database Retrieval Macros

field name from the output table, and look for the value at the intersection of thatfield name in the database and the current database row. B12 begins with an if testto determine whether the value at that intersection is blank or not; if not, it printsit to the output, but if it is blank, the macro bypasses it. The reason for doing so is

19. Data Modification Macros 195

Table 19.10: Applying 5% discount to certain values in a column

A B

1 D-Row {For A1,0,@Count(Data:A1..A1000)-1,1,B3}

2

20. Database Structure Macros 197

Table 20.1: Valid Sort Settings for Data in A5..D100

Heading 0 A5..D100

Heading 1 HeadingSize 1 A4..D100

Heading 1 HeadingSize 2 A3..D100

Heading 1 HeadingSize 3 A2..D100

198 20. Database Structure Macros

If the block is more than one cell high, the commands to insert or delete rowswill insert or delete the same number of rows.

If the block is more than one cell wide, the commands to insert or deletecolumns will insert or delete the same number of columns.

EntireOrPartial. This argument requires the programmer to choose whether to

21. Export/Import Macros 201

The HeaderMargin is expressed in a height such as "0.5 in".The FontSettings

21. Export/Import Macros 205

{GetPos Cell} stores the current position of the byte pointer in the open text filein Cell. The byte pointer is the point from which {ReadLn} would read aline of text and to which {WriteLn} would write a line of text. For more

21. Export/Import Macros 207

three columns of entries from Data:A1..C10000 to C:\Temp\MyData.txt, whetherthat file exists or not, and it if exists, it appends the data to the end of the file.

Table 21.4: Appending text to an existing file, or creating a new one

212 22. Exporting to WordPerfect

3. Merging (under Tools > Merge) in several ways: directly from QP, via a different

22. Exporting to WordPerfect 219

currently open spreadsheet. The programmer can obviously set the name byother means. And if the first row of data contains field names, the argumentfor ImportSetFirstRecFieldnames should be Yes!.

Table 22.3: Merging from text files

Application (wp; "WordPerfect"; Default!; "EN")

Application (qp; "QuattroPro"; Default!)

vDataFile=qp.GetCellValue("A:A1")

ImportSetFileName (vDataFile)

ImportSetSource (ASCII!)

ImportSetDestination (MergeData!)

ImportSetSizeToFit (No!)

ImportSetFirstRecFieldnames (No!)

ImportSetAsciiFieldDelimiter ("")

ImportSetAsciiRecordDelimiter ("")

ImportSetAsciiStrip ("")

ImportSetAsciiEncap ("")

ImportSetMacroVariableName ("")

ViewDraft (Yes!)

23. Launching, Linking, and Other Useful Macros 227

notepad (see page 225) or import it into the spreadsheet using some variation ofthe algorithm outlined on page 205.

How to get Windows environmental variables into QP

In a thread at WPU, a user asked how to get the environmental variablebleble2oER-PROFILE in a way so that QP couldaySe-359(page)]TJ0 0 1 rg 0 0 1couat 205

23. Launching, Linking, and Other Useful Macros 229

should display. This will have the effect of causing each file name in column A toinclude the full path.

Finally, the commands in B7 drop the cursor to the next cell below and loop backto B5, to modify each cell in column A in the same way. The result is that each file isa hyperlink that can now be clicked to have the operating system execute that file.

230 23. Launching, Linking, and Other Useful Macros

23. Launching, Linking, and Other Useful Macros 233

How to merge from a QP database without WordPerfect

In WPU 37705, I offered a little spreadsheet that created a series of twenty lettersfrom a QP database of twenty individuals. It compiles the letters from standard partsand paragraphs that contain variable content. This macro combined all the elementsand generated twenty letters in just over one second.

The notebook can be currently found on my website at QPMerge.qpw.

How to save dated versions of a QP notebook

In WPU 38361, a user wanted to know how to have QP save a backup file auto-matically, as WordPerfect does. The consensus was that QP lacks a similar feature(though it has a timed backup in case of crashes), but third party products may bethe best bet for approximating that function.

Chapter 24

Quattro Pro Macros in PerfectScript

PerfectScript is Corel’s macro-writing language for its Office suite, and themacros are saved as .WCM files. This book is not the place to present a primeron PerfectScript, which would be an extensive subject standing alone. Instead, thebook assumes that you know how to compose a PerfectScript macro and provides afew finer points for applying it to QP.

238 24. PerfectScript

The Eval command

To have the macro use a QP function to give it data from or about the spread-sheet, place the function as text within an Eval

244 Index

~(tilde), 108\+0, 117\+Letter, 117Backslash macros, 117Cell content, 150, 157Cell Properties, 142Command buttons, 118, 141Commenting macros, 108Conditional formatting, 146Database

Adding or modifying, 188Retrieving information, 170Structural change, 196

Date and Time, 166Dynamic modification, 108Exporting to WordPerfect, 211Import/Export to text, 200Interface, 131Introduction, 107Keystroke shortcut macro, 117, 167Looping by {For}, 112Looping by branching, 111Naming cells, 117Navigation, 140Navigation and Selection, 122NBExitMacro, 117, 121, 168, 209NBStartMacro, 117, 120, 128Other useful macros, 224Positioning display, 140R1C1 in macros, 107

Recommended