你笑的时候全世界陪你一起笑,你哭的时候只有你一个人哭

使用Robot从txt文件中读取不同行的内容并显示

上一篇 / 下一篇  2006-12-14 21:55:17 / 个人分类:Rational Robot 技术

上一篇文章中介绍了如何使用Robot从Excel中读取不同行和列的内容,但其中涉及到很多较复杂的语句,所以对于阅读和理解都不是很方便,这里我实验了一下使用Robot从txt文件中读取不同行的内容并显示(txt里没有列的概念),最后将代码“封装”成两个函数:ReturnRowNum,ReturnCell。一个是返回指定文件的行数,一个是返回指定文件不同行的内容。

脚本

Declare Function ReturnRowNum(Dircetory as string) as integer
Declare Function ReturnCell(Dircetory as string,RowNum as integer,ReturnRowCell as integer) as string

Sub main                  '主脚本
    Dim x as integer
    Dim Location as string
    Dim msgtext as string
   
    Location = "C:\qingd.txt"    '指定一个文件
   
    x = returnrownum(location)    '调用ReturnRowNum函数,返回这个文件中的行数
   
    msgtext = ReturnCell(location,x,1)    '调用ReturnCell函数,返回指定文件,指定行的内容
    msgbox msgtext

End sub

Function ReturnRowNum(Dircetory as string) as integer    '返回指定文件的行数
   Dim testscore as String
   Dim x,i,y as integer
  
   if IsMissing(Dircetory) then
    msgbox "no Dircetory"
   End if
  
   Open Dircetory for Input as #1
   x=0
   i=0
   Do Until x=Lof(1)
      Line Input #1, testscore
      x=x+1
      i=i+1
      y=Seek(1)
      If y>Lof(1) then
         x=Lof(1)
      Else
         Seek 1,y
      End If
   Loop
   Close #1
   ReturnRowNum = i
End Function  

Function ReturnCell(Dircetory as string,RowNum as integer,ReturnRowCell as integer) as string  '返回指定文件,制定行的内容
   Dim testscore as String
   Dim x,i,y as integer
   Dim cell()                     '这里用到一个动态数组,存放各个行的内容
   if IsMissing(Dircetory) then
    msgbox "no Dircetory"
   End if
   if IsMissing(RowNum) then
    RowNum = 0
   End if
   if IsMissing(ReturnRowCell) then
    ReturnRowCell = 0
   End if
      
   Redim cell(RowNum-1)
   Open Dircetory for Input as #1
   x=0
   Do Until x=Lof(1)
      Line Input #1, testscore
      cell(x) = testscore
      x=x+1
      y=Seek(1)
      If y>Lof(1) then
         x=Lof(1)
      Else
         Seek 1,y
      End If 
   Loop
   ReturnCell = cell(ReturnRowCell-1)
   Close #1
 
End Function

这个脚本中既包含Function又包含Main,使用过程中我们可以把Function的实现过程移到其他脚本里,这样其他脚本要使用某一个Function时Declare一下即可(怎样跨脚本使用Declare可以看之前的一篇文章《Rational Robot中跨脚本调用函数的注意点—Declare语句解释》)


TAG:

 

评分:0

我来说两句

Open Toolbar