home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Product / Product.zip / csvexp.cmd < prev    next >
OS/2 REXX Batch file  |  1997-09-19  |  3KB  |  59 lines

  1. Here is the script I wrote to export a range to a CSV file.  Basically, 
  2. select a range and then run the script. Enjoy! J. Daniel Kulp Sundial Systems
  3.  
  4.  
  5.  
  6. /* Export CSV Script                                                */ /* Written by J. Daniel Kulp                                        */ /* jdkulp@ibm.net                                                   */ /* October 30, 1996                                                 */
  7. /* hard code the filename for now.  If you have VXRexx or other     */ /* visual REXX product, pop up a dialog to ask for this.            */ file = "ascii.csv"
  8. /* Load the utilities so we can set the Extended Attribute          */ call RxFuncAdd 'SysPutEA' , 'RexxUtil' , 'SysPutEA'
  9. /* the first thing we need to do is to convert the current range to */ /* values that we can iterate with                                  */ parse value currentrange() with ul ':' lr
  10.  
  11. /* Pulling the layer off is easy                                    */ ulLayer = 1 if (left(ul,1) == "[") then do
  12.     parse value ul with '[' ulLayer ']' ul
  13.     ulLayer = toOffset(ulLayer) end
  14.  
  15. lrLayer = 1 if (left(lr,1) == "[") then do
  16.     parse value lr with '[' lrLayer ']' lr
  17.     lrLayer = toOffset(lrLayer) end
  18.  
  19. /* separating the column and row is a bit more difficult            */ pos = 1 do while substr(ul,pos,1) > '9'
  20.     pos = pos + 1 end ulCol = toOffset(left(ul,pos-1)) ulRow = right(ul,length(ul)-pos+1)
  21.  
  22. pos = 1 do while substr(lr,pos,1) > '9'
  23.     pos = pos + 1 end lrCol = toOffset(left(lr,pos-1)) lrRow = right(lr,length(lr)-pos+1)
  24.  
  25. /* reset file pointer to start of file so we overwrite it             */ 'del 'file charout(file,'',1)
  26.  
  27. /* we now have the range, iterate over it */ do curLayer = ulLayer to lrLayer
  28.     do curRow = ulRow to lrRow
  29.         do curCol = ulCol to lrCol
  30.             value = getv(cell(curRow,curCol,curLayer))
  31.             charout(file,value,)
  32.             if \(curCol = lrCol)  then
  33.                 charout(file,',',)
  34.         end
  35.         lineout(file,'',)
  36.     end end
  37.  
  38. /* Close the stream so we can add the extended attribute              */ type = 'Plain Text' typeinfo = "DFFF00000100FDFF"x || d2c(length(type)) || "00"x || type || "0"x stream(file,'c','close') SysPutEA(file,'.TYPE',typeinfo) /* we're done!                                                        */ exit
  39.  
  40. /* convert a STRING representation of the layer or column             */ /* (example: AX) to a numeric offset                                  */ toOffset:
  41.     parse arg instr
  42.     ret = length(instr)
  43.     select
  44.         when ret = 1 then
  45.             ret = C2D(instr)-C2D("A")+1
  46.         when ret = 2 then do
  47.             ret = C2D(right(instr,1))-C2D("A")+1
  48.             ret = ret + (C2D(left(instr,1))-C2D("A")+1)*26
  49.             end
  50.         when ret = 3 then do
  51.             ret = C2D(right(instr,1))-C2D("A")+1
  52.             ret = ret + (C2D(left(instr,1))-C2D("A")+1)*26
  53.             ret = ret + (C2D(substr(instr,2,1))-C2D("A")+1)*26*26
  54.             end
  55.     otherwise
  56.          ret = 0
  57.     end
  58.     return ret
  59.