home *** CD-ROM | disk | FTP | other *** search
/ Merciful 3 / Merciful_Release_3.bin / software / f / finalwriter / finalwriterv5.03h.dms / finalwriterv5.03h.adf / FWMacros.lha / FWListMerge next >
Text File  |  1994-09-28  |  7KB  |  199 lines

  1. /* ======================================================================= */
  2. /* Final Writer, Final Copy_II ARexx Macro                                 */
  3. /* FWListMerge - Perfoms a list merge from a Final Data database.          */
  4. /* $VER: FWListMerge 1.0 (27.7.94)                                         */
  5. /* © 1994 SoftWood, Inc.                                                   */
  6. /* Use of this macro is strictly at the user's risk.                       */
  7. /*                                                                         */
  8. /* This ARexx macro is to be run from FinalCopy_II or FinalWriter.         */
  9. /* It performs a "List Merge" from a FinalData database. It will create    */
  10. /* a list of data from a database and put it into your FC or FW document.  */
  11. /* To run the macro you need to have your FC or FW document and your       */
  12. /* Final Data database file both open.                                     */
  13. /*                                                                         */
  14. /* Select the rows in the database that you wish to have included in the   */
  15. /* list. If no rows are selected then all the rows in the database will be */
  16. /* included in the list. (Note that this macro will use the first database */
  17. /* port that it can find, so it is wise to have only one database window   */
  18. /* open when you run this macro. Otherwise the wrong database may be used).*/
  19. /*                                                                         */
  20. /* In the document window you need to set up a line of text that will tell */
  21. /* the macro which columns of the database you want in the list. This line */
  22. /* of text should include at least one column name from the database,      */
  23. /* surrounded by the delimiter characters. (The delimiter charaters are    */
  24. /* '<' and '>', but can be set to whatever characters you want by changing */
  25. /* the "begDelim =" and "endDelim =" lines below.) For example, you might  */
  26. /* have a line such as:                                                    */
  27. /*       <First Name>         <Last Name>      <Phone>                     */
  28. /* Before you run the macro, place the insertion point somewhere on the    */
  29. /* this line of text. When the macro is run it will extract the column     */
  30. /* data for each row in the database and substitute the column data for    */
  31. /* the column name. You might end up with something like:                  */
  32. /*       <First Name>         <Last Name>      <Phone>                     */
  33. /*       John                 Doe              555-1111                    */
  34. /*       Jane                 Smith            555-1212                    */
  35. /*       Fred                 Flintstone       555-2222                    */
  36. /* Note that only the column names will have data substituted. Anything    */
  37. /* else on the line will remain in each row of data. This allows you to    */
  38. /* separate the columns with tabs or have other text appear on all lines.  */
  39. /* ======================================================================= */
  40. OPTIONS RESULTS
  41.  
  42. /* ---- Load the rexxsupport library ---- */
  43. IF ( ADDLIB("rexxsupport.library", 0, -30, 0) = FALSE) THEN EXIT 20
  44.  
  45. /* ---- Delimiters for column names. ---- */
  46. begDelim = "<"
  47. endDelim = ">"
  48.  
  49. /* ---- Get the address of the port we were called from. ---- */
  50. WPPort = ADDRESS()
  51. HostId = LEFT(WPPort, 7)
  52. IF ( HostId ~= "FINALC." & HostId ~= "FINALW." ) THEN DO
  53.    IF ( HostId = "FINALD." ) THEN
  54.       ShowMessage 1 1 '"This macro must be run from" "FinalCopy_II or FinalWriter!" "" "OK" "" ""'
  55.    EXIT 10
  56. END
  57.  
  58. /* ---- Find the address of the Final Data port. ---- */
  59. /* ---- This will use the first port it can find ---- */
  60. /* ---- with the base name of FinalD. --------------- */
  61. FDPortBase = "FINALD."
  62. found = 0
  63. DO p = 1 TO 50
  64.    IF ( SHOWLIST('P', FDPortBase || p) ) THEN DO
  65.       FDPort = FDPortBase || p
  66.       found = 1
  67.       LEAVE
  68.    END
  69. END
  70.  
  71. IF ( ~ found ) THEN DO
  72.    ShowMessage 1 1 '"Can not find Final Data ARexx port!" "" "" "OK" "" ""'
  73.    EXIT 10
  74. END
  75.  
  76. /* ---- Determine the number of columns and rows in the database. ---- */
  77. ADDRESS VALUE FDPort
  78. NumRows
  79. nrows = RESULT
  80. NumColumns
  81. ncols = RESULT
  82. IF ( nrows = 0 ) THEN DO
  83.    ShowMessage 1 1 '"No rows of data in database!" "" "" "OK" "" ""'
  84.    EXIT 10
  85. END
  86. IF ( ncols = 0 ) THEN DO
  87.    ShowMessage 1 1 '"No columns defined in database!" "" "" "OK" "" ""'
  88.    EXIT 10
  89. END
  90.  
  91. /* ---- Determine which rows will get transferred. If ---------- */
  92. /* ---- selection is ROWS then only use the selected rows. ----- */
  93. /* ---- Otherwise, use all the rows in the database. ----------- */
  94. firstRow = 1
  95. lastRow = nrows
  96. SelectionInfo
  97. PARSE VAR RESULT selType selFrom selTo
  98. IF ( selType = "ROWS" ) THEN DO
  99.    firstRow = selFrom
  100.    lastRow = selTo
  101. END
  102.  
  103. /* ---- Select and extract the data of the current wp line. ---- */
  104. ADDRESS VALUE WPPort
  105.  
  106. CtrlDown
  107. AltDown
  108. Cursor Left
  109. ShiftDown
  110. Cursor Right
  111. Extract
  112. OrigText = RESULT
  113. ShiftUp
  114. Cursor Right
  115. CtrlUp
  116. AltUp
  117. NewParagraph
  118.  
  119. /* ---- Determine indexes into original text ------- */
  120. /* ---- Set up the Data structure for the merge ---- */
  121. ADDRESS VALUE FDPort
  122. numFlds = 0
  123. txtlen = LENGTH(OrigText)
  124. start = 1
  125. err = 0
  126. DO FOREVER
  127.    /* ---- Search for beginning delimiter. ------- */
  128.    /* ---- If we don't find one, leave the ------- */
  129.    /* ---- loop, as there are no more fields. ---- */
  130.    begidx = INDEX(OrigText, begDelim, start)
  131.    IF ( begidx = 0 ) THEN LEAVE
  132.  
  133.    /* ---- Search for ending delimiter. ---- */
  134.    /* ---- If we can't find one, leave ----- */
  135.    /* ---- the loop with an error. --------- */
  136.    endidx = INDEX(OrigText, endDelim, begidx)
  137.    IF ( endidx = 0 ) THEN DO
  138.       err = 1
  139.       LEAVE
  140.    END
  141.  
  142.    start = endidx
  143.    /* ---- We found beginning and ending delimiters. ---- */
  144.    /* ---- Save some data needed for the merge. --------- */
  145.    Data.numFlds.fldbeg = begidx
  146.    Data.numFlds.fldend = endidx
  147.    cname = SUBSTR(OrigText, begidx+1, endidx-begidx-1)
  148.    cname = '"' || cname || '"'   /* need quotes in case name has spaces */
  149.    GetColumnPosition NAME cname
  150.    IF ( RC ~= 0 ) THEN DO
  151.       err = 2
  152.       LEAVE
  153.    END
  154.    Data.numFlds.colpos = RESULT
  155.    numFlds = numFlds + 1
  156. END  /* end forever loop */
  157.  
  158. IF ( err = 0 & numFlds = 0 ) THEN err = 3
  159.  
  160. IF ( err ~= 0 ) THEN DO
  161.    SELECT
  162.       WHEN ( err = 1 ) THEN DO
  163.          ShowMessage 1 1 '"Ending delimiter not found!" "" "" "OK" "" ""'
  164.          END
  165.       WHEN ( err = 2 ) THEN DO
  166.          ShowMessage 1 1 '"Column name not found in database:"' cname '"" "OK" "" ""'
  167.          END
  168.       WHEN ( err = 3 ) THEN DO
  169.          ShowMessage 1 1 '"No columns found to merge!" "" "" "OK" "" ""'
  170.          END
  171.       OTHERWISE DO
  172.          END
  173.    END   /* end select */
  174.    EXIT 10
  175. END
  176.  
  177. /* ---- We don't want the newline character at the end of the text. ---- */
  178. lastChar = RIGHT(OrigText, 1)
  179. IF ( lastChar = D2C(10) ) THEN
  180.    OrigText = DELSTR(OrigText, LENGTH(OrigText), 1)
  181.  
  182. /* ---- Do the actual merging of the data. ---- */
  183. DO r = firstRow TO lastRow
  184.    ADDRESS VALUE FDPort
  185.    newText = OrigText
  186.    DO i=numFlds-1 TO 0 BY -1
  187.       CellData Data.i.colpos r
  188.       newData = RESULT
  189.       newText = DELSTR(newText, Data.i.fldbeg, Data.i.fldend - Data.i.fldbeg + 1)
  190.       newText = INSERT(newData, newText, Data.i.fldbeg-1)
  191.    END
  192.  
  193.    ADDRESS VALUE WPPort
  194.    Type newText
  195.    IF ( r ~= lastRow ) THEN
  196.       NewParagraph
  197. END /* end row */
  198.  
  199. /* END OF MACRO FILE */