home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Database / FinalData3.DMS / FinalData3.adf / FDMacros / Text_To_Amount < prev   
Encoding:
Text File  |  1995-03-21  |  5.5 KB  |  190 lines

  1. /* ========================================================== */
  2. /* Final Data ARexx Macro.                                    */
  3. /* Text_To_Amount - Convert text column to amount column.     */
  4. /* $VER: Text_To_Amount 1.1 (21.3.95)                         */
  5. /* © 1995 SoftWood, Inc.                                      */
  6. /* Use of this macro is strictly at the user's risk.          */
  7. /*                                                            */
  8. /* This macro will take a TEXT column and convert its data    */
  9. /* into a new AMOUNT column.                                  */
  10. /* To use the macro, first select one and only one TEXT       */
  11. /* column. When the macro is executed a new AMOUNT column     */
  12. /* will be created to the right of the TEXT column. The macro */
  13. /* will then proceed to transfer data from the TEXT column to */
  14. /* the new AMOUNT column. If any data in the TEXT column can  */
  15. /* not be converted, then the corresponding cell in the       */
  16. /* AMOUNT column will remain empty. If this happens, a        */
  17. /* requester will be presented to warn you that some data     */
  18. /* could not be converted.                                    */
  19. /* ========================================================== */
  20. OPTIONS RESULTS
  21.  
  22. NumRows
  23. num_rows = RESULT
  24. from_row    = 1
  25. to_row    = num_rows
  26.  
  27. /* ----------------------------------------- */
  28. /* Get selection information. Make sure that */
  29. /* one and only one text column is selected. */
  30. /* ----------------------------------------- */
  31. SelectionInfo
  32. PARSE VAR RESULT selType col_pos toCol
  33.  
  34. IF ( selType ~= 'COLUMNS' ) THEN DO
  35.     ShowMessage 1 1 '"You must select a column first." "" "" "OK" "" ""'
  36.     EXIT 10
  37. END
  38.  
  39. IF ( col_pos ~= toCol ) THEN DO
  40.     ShowMessage 1 1 '"Please select only one column." "" "" "OK" "" ""'
  41.     EXIT 10
  42. END
  43.  
  44. GetColumnID POSITION col_pos
  45. col_id = RESULT
  46. GetColumnDef col_id TYPE
  47. col_type = RESULT
  48.  
  49. IF ( col_type ~= 'Text' ) THEN DO
  50.    ShowMessage 1 1 '"The selected column must be a TEXT column!" "" "" "OK" "" ""'
  51.     EXIT 10
  52. END
  53.  
  54. /* ------------------------- */
  55. /* Ask for decimal character */
  56. /* ------------------------- */
  57. ShowMessage 1 0 '"What is your decimal character?" "" "" "Period" "Comma" ""'
  58. IF ( RESULT = 1 ) THEN DO
  59.     dchar = "Period"
  60.     decimal_point = "."
  61.     thousand_sep  = ","
  62. END
  63. ELSE DO
  64.     dchar = "Comma"
  65.     decimal_point = ","
  66.     thousand_sep  = "."
  67. END
  68.  
  69. SelectOff
  70.  
  71. /* ----------------------------------------------------- */
  72. /* Initialize, define,and add the new amount column      */
  73. /* to the database. Check for any errors that may occur. */
  74. /* ----------------------------------------------------- */
  75. GetColumnName COLUMNID col_id
  76. col_name = RESULT
  77. new_col_name = col_name || "_amt"
  78. new_col_pos = col_pos + 1
  79.  
  80. col_ok = 0
  81. InitNewColumn "Amount" new_col_name
  82. IF ( RC = 0 ) THEN DO
  83.     DefineColumn POSITION new_col_pos DECIMAL dchar DECIMALPLACES 4
  84.     IF ( RC = 0 ) THEN DO
  85.         AddNewColumn
  86.         IF ( RC = 0 ) THEN
  87.             col_ok = 1
  88.     END
  89. END
  90.  
  91. IF ( ~ col_ok ) THEN DO
  92.    ShowMessage 1 1 '"Cannot create new column!" "" "" "OK" "" ""'
  93.     EXIT 10
  94. END
  95.  
  96. /* -------------------------------------------------------- */
  97. /* We have successfully created the new amount column.      */
  98. /* Now we need to extract data from the text column and     */
  99. /* place it into the new column. We need to properly format */
  100. /* the data for data entry and at the same time check for   */
  101. /* invalid data.                                            */
  102. /* -------------------------------------------------------- */
  103. invalid_rows = 0
  104. DO the_row = from_row TO to_row
  105.     /* ------------------------------ */
  106.     /* Get the cell data for this row */
  107.     /* ------------------------------ */
  108.     CellData col_pos the_row EDITFORMAT
  109.     IF    ( RC ~= 0 ) THEN DO
  110.        ShowMessage 1 1 '"Error extracting cell data." "" "" "OK" "" ""'
  111.         EXIT 10
  112.     END
  113.  
  114.     buff         = RESULT
  115.     len          = LENGTH(buff)
  116.     data         = ""
  117.     digit        = 0
  118.     invalid_data = 0
  119.  
  120.     /* --------------------------------------------------------- */
  121.     /* Scan the data from the cell and format it for data entry. */
  122.     /* --------------------------------------------------------- */
  123.     DO i=1 TO len
  124.         chr = SUBSTR(buff, i, 1)
  125.  
  126.         IF ( chr = thousand_sep ) THEN
  127.             ITERATE
  128.  
  129.         IF ( chr = "-" | chr = decimal_point ) THEN DO
  130.             data = data || chr
  131.         END
  132.         ELSE DO
  133.             IF ( chr >= "0" & chr <= "9" ) THEN DO
  134.                 data = data || chr
  135.                 digit = 1
  136.             END
  137.             ELSE DO
  138.                 IF ( digit ) THEN DO
  139.                     invalid_data = 1
  140.                     LEAVE
  141.                 END
  142.             END
  143.         END
  144.     END
  145.  
  146.     /* ----------------------------------- */
  147.     /* If we had invalid data, ignore this */
  148.     /* row and continue with the next row. */
  149.     /* ----------------------------------- */
  150.     IF ( invalid_data | (data = "" & len > 0) ) THEN DO
  151.         invalid_rows = invalid_rows + 1
  152.         ITERATE
  153.     END
  154.  
  155.     SelectCell new_col_pos the_row
  156.     IF    ( RC > 0 ) THEN DO
  157.        ShowMessage 1 1 '"Error converting cell." "" "" "OK" "" ""'
  158.         EXIT 10
  159.     END
  160.  
  161.     IF ( data ~= "" ) THEN DO
  162.         Insert data
  163.         EditCell
  164.         IF ( RESULT = 0 ) THEN DO
  165.          /* ------------------------------------ */
  166.             /* Invalid data. Leave this cell empty. */
  167.          /* ------------------------------------ */
  168.             invalid_rows = invalid_rows + 1
  169.             len = LENGTH(data)
  170.             IF    ( len > 0 ) THEN DO
  171.                 SetCaret 0 TO len
  172.                 Delete
  173.             END
  174.         END
  175.     END
  176. END
  177.  
  178. /* ---------------------------------- */
  179. /* Turn off the last cell we were in. */
  180. /* ---------------------------------- */
  181. SelectOff
  182.  
  183. /* --------------------------------------------- */
  184. /* Display requester if we had any invalid rows. */
  185. /* --------------------------------------------- */
  186. IF ( invalid_rows > 0 ) THEN DO
  187.     line = "'" || invalid_rows "invalid row(s) found." || "'"
  188.     ShowMessage 1 1 line '"" "" "OK" "" ""'
  189. END
  190.