home *** CD-ROM | disk | FTP | other *** search
- /* ========================================================== */
- /* Final Data ARexx Macro. */
- /* Text_To_Amount - Convert text column to amount column. */
- /* $VER: Text_To_Amount 1.1 (21.3.95) */
- /* © 1995 SoftWood, Inc. */
- /* Use of this macro is strictly at the user's risk. */
- /* */
- /* This macro will take a TEXT column and convert its data */
- /* into a new AMOUNT column. */
- /* To use the macro, first select one and only one TEXT */
- /* column. When the macro is executed a new AMOUNT column */
- /* will be created to the right of the TEXT column. The macro */
- /* will then proceed to transfer data from the TEXT column to */
- /* the new AMOUNT column. If any data in the TEXT column can */
- /* not be converted, then the corresponding cell in the */
- /* AMOUNT column will remain empty. If this happens, a */
- /* requester will be presented to warn you that some data */
- /* could not be converted. */
- /* ========================================================== */
- OPTIONS RESULTS
-
- NumRows
- num_rows = RESULT
- from_row = 1
- to_row = num_rows
-
- /* ----------------------------------------- */
- /* Get selection information. Make sure that */
- /* one and only one text column is selected. */
- /* ----------------------------------------- */
- SelectionInfo
- PARSE VAR RESULT selType col_pos toCol
-
- IF ( selType ~= 'COLUMNS' ) THEN DO
- ShowMessage 1 1 '"You must select a column first." "" "" "OK" "" ""'
- EXIT 10
- END
-
- IF ( col_pos ~= toCol ) THEN DO
- ShowMessage 1 1 '"Please select only one column." "" "" "OK" "" ""'
- EXIT 10
- END
-
- GetColumnID POSITION col_pos
- col_id = RESULT
- GetColumnDef col_id TYPE
- col_type = RESULT
-
- IF ( col_type ~= 'Text' ) THEN DO
- ShowMessage 1 1 '"The selected column must be a TEXT column!" "" "" "OK" "" ""'
- EXIT 10
- END
-
- /* ------------------------- */
- /* Ask for decimal character */
- /* ------------------------- */
- ShowMessage 1 0 '"What is your decimal character?" "" "" "Period" "Comma" ""'
- IF ( RESULT = 1 ) THEN DO
- dchar = "Period"
- decimal_point = "."
- thousand_sep = ","
- END
- ELSE DO
- dchar = "Comma"
- decimal_point = ","
- thousand_sep = "."
- END
-
- SelectOff
-
- /* ----------------------------------------------------- */
- /* Initialize, define,and add the new amount column */
- /* to the database. Check for any errors that may occur. */
- /* ----------------------------------------------------- */
- GetColumnName COLUMNID col_id
- col_name = RESULT
- new_col_name = col_name || "_amt"
- new_col_pos = col_pos + 1
-
- col_ok = 0
- InitNewColumn "Amount" new_col_name
- IF ( RC = 0 ) THEN DO
- DefineColumn POSITION new_col_pos DECIMAL dchar DECIMALPLACES 4
- IF ( RC = 0 ) THEN DO
- AddNewColumn
- IF ( RC = 0 ) THEN
- col_ok = 1
- END
- END
-
- IF ( ~ col_ok ) THEN DO
- ShowMessage 1 1 '"Cannot create new column!" "" "" "OK" "" ""'
- EXIT 10
- END
-
- /* -------------------------------------------------------- */
- /* We have successfully created the new amount column. */
- /* Now we need to extract data from the text column and */
- /* place it into the new column. We need to properly format */
- /* the data for data entry and at the same time check for */
- /* invalid data. */
- /* -------------------------------------------------------- */
- invalid_rows = 0
- DO the_row = from_row TO to_row
- /* ------------------------------ */
- /* Get the cell data for this row */
- /* ------------------------------ */
- CellData col_pos the_row EDITFORMAT
- IF ( RC ~= 0 ) THEN DO
- ShowMessage 1 1 '"Error extracting cell data." "" "" "OK" "" ""'
- EXIT 10
- END
-
- buff = RESULT
- len = LENGTH(buff)
- data = ""
- digit = 0
- invalid_data = 0
-
- /* --------------------------------------------------------- */
- /* Scan the data from the cell and format it for data entry. */
- /* --------------------------------------------------------- */
- DO i=1 TO len
- chr = SUBSTR(buff, i, 1)
-
- IF ( chr = thousand_sep ) THEN
- ITERATE
-
- IF ( chr = "-" | chr = decimal_point ) THEN DO
- data = data || chr
- END
- ELSE DO
- IF ( chr >= "0" & chr <= "9" ) THEN DO
- data = data || chr
- digit = 1
- END
- ELSE DO
- IF ( digit ) THEN DO
- invalid_data = 1
- LEAVE
- END
- END
- END
- END
-
- /* ----------------------------------- */
- /* If we had invalid data, ignore this */
- /* row and continue with the next row. */
- /* ----------------------------------- */
- IF ( invalid_data | (data = "" & len > 0) ) THEN DO
- invalid_rows = invalid_rows + 1
- ITERATE
- END
-
- SelectCell new_col_pos the_row
- IF ( RC > 0 ) THEN DO
- ShowMessage 1 1 '"Error converting cell." "" "" "OK" "" ""'
- EXIT 10
- END
-
- IF ( data ~= "" ) THEN DO
- Insert data
- EditCell
- IF ( RESULT = 0 ) THEN DO
- /* ------------------------------------ */
- /* Invalid data. Leave this cell empty. */
- /* ------------------------------------ */
- invalid_rows = invalid_rows + 1
- len = LENGTH(data)
- IF ( len > 0 ) THEN DO
- SetCaret 0 TO len
- Delete
- END
- END
- END
- END
-
- /* ---------------------------------- */
- /* Turn off the last cell we were in. */
- /* ---------------------------------- */
- SelectOff
-
- /* --------------------------------------------- */
- /* Display requester if we had any invalid rows. */
- /* --------------------------------------------- */
- IF ( invalid_rows > 0 ) THEN DO
- line = "'" || invalid_rows "invalid row(s) found." || "'"
- ShowMessage 1 1 line '"" "" "OK" "" ""'
- END
-