Structure2Text Tool

Version: 1.01.01 - last Update: Monday, September 28, 2008, 23:10:00

Previous ChapterToolbox Home (TOC)Next Entry


This is a useful tool your may want to add to your toolbox

Introduction

Use this helper to create nicely formatted structure information of your tables (see figure #1). The tool uses TABs for column layout which makes it a perfect fit for Copy&Paste your documentation to Excel or Word.

Figure #1: One Key-Press Formatted Table Structure

Check out the comments in the code below for more details!

LPARAMETERS tcTableAlias, tcDestination
LOCAL lcProg AS STRING
lcProg = SYS(16)
SET PROCEDURE TO (m.lcProg) ADDITIVE
ON KEY LABEL F12 DO &lcProg
*******************************************************
*
* (C)2002: burkhard.stiller@freenet.de
* Version 1.2b
*
* Struct2Txt.prg
*
* Generates a text stream
* (like DISPLAY STRUCTURE TO FILE does) and
* copies it into the windows clipboard.
* Columns are TAB delimited! Very handy for pasting
* the text into (Excel or Winword) tables which
* require tab delimited data by default!
*
* Parameters:
* tcTableAlias [character, optional]
* := If empty, or not passed the table opened
* in current workarea is used, otherwise
* the table passed in gets selected and
* will be used.
*
* tcDestination [character, optional]
* := If parameter is empty/false: (because it
* was not passed in) generated text will
* be written to _CLIPTEXT [default]. If
* parameter is empty: (but was passed in
* !by reference!) generated text will
* passed back in this parameter.
* If parameter is NOT empty: generated
* text will be written to a file (with
* the filename passed in as a string).
*
* Returns:
* llok [boolean]
* If TRUE is returned everything went right.
* If any error occurred FALSE will be returned.
*
* Comments:
* another (shorter/manual) way capturing table
* structure goes like this:
* 1.) SELECT <workarea> && table that should be
* documented
* 2.) COPY STRUCTURE EXTENDED TO <tempDBFname>
* 3.) USE <tempDBFname> IN 0
* 4.) _VFP.DataToClip("<tempDBFname>",,3)
* 5.) USE IN <tempDBFname>
* 6.) ERASE <tempDBFname>.*
*
*******************************************************
*
LOCAL laFields[1,16], lnFields, laIndices[1,6],;
  lnIndices, lnSelected, lnLoop, lnHit, lnBytes,;
  llOK
STORE "" TO laFields, laIndices
STORE 0 TO lnFields, lnSelected, lnLoop, lnIndices,;
  lnHit
STORE 1 TO lnBytes
STORE .F. TO llOK
*\\
IF VARTYPE(tcTableAlias) == "C"
  tcTableAlias = UPPER(ALLTRIM(tcTableAlias))
ELSE
  tcTableAlias = ALIAS()
ENDIF
*//
*\\ prepare for clipboard output
_PRETEXT = ""
SET TEXTMERGE ON NOSHOW
*\\ check where to send the textmerge output to
IF VARTYPE(tcDestination) == "C"
  IF EMPTY(tcDestination)
    *\\ output to variable passed by reference
    SET TEXTMERGE TO MEMVAR tcDestination
  ELSE
    *\\ output to text file with the given filename
    SET TEXTMERGE TO (ALLTRIM(tcDestination))
  ENDIF
ELSE
  *\\ send output to windows clipboard by default
  SET TEXTMERGE TO MEMVAR _CLIPTEXT
ENDIF
*//
IF NOT EMPTY(tcTableAlias)
  *\\ open table / select workarea
  m.lnSelected = SELECT(0)
  IF NOT USED(tcTableAlias)
    SELECT 0
    USE (LOCFILE(tcTableAlias) AGAIN ;
      ALIAS (tcTableAlias) SHARED
  ELSE
    SELECT (tcTableAlias)
  ENDIF
  IF ALIAS() == tcTableAlias
    *\\
    * ---------------------------------------
    *// Output: table header information:
\Table structure: <<DBF()>>
\Records: <<TRANSFORM(RECCOUNT())>>
\Last Update: <<TRANSFORM(LUPDATE())>>
    IF NOT SYS(2012) == "0"
\Block size of Memo file: <<SYS(2012)>>
    ENDIF
\Codepage: <<TRANSFORM(CPDBF())>>
    * Every word in next line is separated by TAB [CHR(9)]
\No FldName Typ W D Idx Ord Nul NoCpTrans
    *\\ ---------------------------------------
    *\\ List fields
    *\\ Note: Not all available attributs
    *\\ (<laFields> array has 16 columns!)
    *\\ are processed here
    m.lnFields = AFIELDS(laFields)
    IF m.lnFields > 0
      *\\ retrieve index information
      m.lnIndices = ATAGINFO(laIndices)
      *\\ ---------------------------------------
      *\\ process all fields in the structure
      *\\ and create a tab delimited output:
      FOR m.lnLoop = 1 TO m.lnFields
\<<TRANSFORM(m.lnloop)>>
\\<<CHR(9) + laFields[m.lnLoop,1]>>
\\<<CHR(9) + laFields[m.lnLoop,2]>>
\\<<CHR(9) + TRANSFORM(laFields[m.lnLoop,3])>>
\\<<CHR(9) + TRANSFORM(laFields[m.lnLoop,4])>>
        *\\
        *\\ m.lnHit holds a <row_number> OR <0>
        m.lnHit = ASCAN(laIndices,laFields[m.lnLoop,1],-1,-1,3,15)
        IF m.lnHit > 0
          *\\ write out index type and sort order
\\<<CHR(9) + ALLTRIM(laIndices[m.lnHit, 2])>>
\\<<CHR(9) + ALLTRIM(laIndices[m.lnHit, 5])>>
        ELSE
          *\\ just fill up with "-" if there is no index tag
\\<<CHR(9) + "-">>
\\<<CHR(9) + "-">>
        ENDIF
\\<<CHR(9)+TRANSFORM(laFields[m.lnLoop,5])>>
\\<<CHR(9)+TRANSFORM(laFields[m.lnLoop,6])>>
        *\\
        *\\ count the bytes for record size total:
        m.lnBytes = m.lnBytes + laFields[m.lnLoop,3]
      NEXT
      *\\ set flag for success
      llOK = .T.
      * ---------------------------------------
    ELSE
      *\\ Oups - no fields found! Structure is invalid.
\** Error: Requested table structure is invalid **
      m.lnBytes = 0
    ENDIF
    *\\ ---------------------------------------
    *\\ Output: table footer information:
\** Total ** <<TRANSFORM(m.lnBytes)>>
    *\\ ---------------------------------------
  ELSE
    *\\ Table couldn't be opened!
\** Error: Table couldn't be opened! **
  ENDIF
  SELECT (m.lnSelected)
ELSE
  *\\ No alias passed + empty current workarea
\** Error: No alias **
ENDIF
*
SET TEXTMERGE TO
SET TEXTMERGE OFF
*
RETURN m.llOK

Previous ChapterToolbox Home (TOC)Next Entry

No comments:

Post a Comment