Changeset 2048518 in tol


Ignore:
Timestamp:
Aug 18, 2010, 12:32:01 PM (16 years ago)
Author:
Jorge Pérez <josp.jorge@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
7d960bd
Parents:
7d97465
Message:

closes #965, implement Excel.ReadRange

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@2479 be4846ae-f051-483a-831f-b64e7d08527f

Location:
tol/contrib/excel
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tol/contrib/excel/Jamfile

    r7d97465 r2048518  
    55exe test01 : test01.cpp ExcelFormat.cpp BasicExcel.cpp ;
    66
    7 explicit test01 ;
     7exe test02 : test02.cpp ;
     8
     9explicit test01 test02 ;
  • tol/contrib/excel/test.tol

    r7d97465 r2048518  
    4141               [[ cell_05_01, cell_05_02 ]] ]], "Std" )
    4242};
     43
     44WriteLn( "Lectura de rango ..." );
     45Set {
     46  Set range1 = Excel.ReadRange( id, "A1", "B5" );
     47  Set View( range1, "Std" )
     48};
    4349 
    4450Real status = Excel.Destroy( id );
  • tol/contrib/excel/tolExcel.cpp

    r7d97465 r2048518  
    3434#include <tol/tol_btxtgra.h>
    3535#include <tol/tol_bspfun.h>
     36#include <tol/tol_bsetgra.h>
    3637
    3738class TolExcel {
    … …  
    202203    return nHour / nHiD + nMinute / nMiD + nSecond / nSiD;
    203204  }
     205
     206  static size_t decodeColumn( char * cellName, size_t length );
     207  static bool decodeCoordinates( char * cellName,
     208                                 size_t &row, size_t &column );
     209  static bool getCellCoordinates( const BText & name,
     210                                  BSyntaxObject *cell,
     211                                  size_t &row, size_t &column );
    204212
    205213protected:
    … …  
    243251  BasicExcelWorksheet *m_ptrActiveWS;
    244252};
     253
     254size_t TolExcel::decodeColumn( char * cellName, size_t length )
     255{
     256  if ( !length ) return 0;
     257  char c = cellName[ --length ];
     258  if ( c < 'A' || c > 'Z')
     259    return 0;
     260  return (c - 'A' + 1) + TolExcel::decodeColumn( cellName, length ) * 26;
     261}
     262
     263bool TolExcel::decodeCoordinates( char * cellName, size_t &row, size_t &column )
     264{
     265  size_t i = 0;
     266
     267  // decode column A-based
     268  char ch = cellName[ i ];
     269  while ( ch >= 'A' && ch <= 'Z' ) { ch = cellName[ ++i ]; }
     270  column = TolExcel::decodeColumn( cellName, i );
     271  if ( !column ) {
     272    return false;
     273  }
     274  // decode row 1-based
     275  row = 0;
     276  while ( ch >= '1' && ch <= '9' ) {
     277    row = row * 10 + ch - '0';
     278    ch = cellName[ ++i ];
     279  }
     280  if ( !row ) {
     281    return false;
     282  }
     283  // cell coordinates are 0-based internally
     284  --row;
     285  --column;
     286  return true;
     287}
     288
     289bool TolExcel::getCellCoordinates( const BText & name, BSyntaxObject *cell,
     290                                   size_t &row, size_t &column )
     291{
     292  bool status;
     293 
     294  if ( cell->Grammar() == GraText() ) {
     295    BText & cellName = Text( cell );
     296   
     297    status = TolExcel::decodeCoordinates( cellName.Buffer(),
     298                                               row, column );
     299    if ( !status ) {
     300      Error( name + ": " +
     301             I2( "invalid cell coordinates ",
     302                 "coordenadas de celda invalidas " ) + cellName );
     303    }
     304  } else if ( cell->Grammar() == GraSet() ) {
     305    BSet &_cell = Set( cell );
     306    if ( _cell.Card() != 2 ) {
     307      Error( name + ": " +
     308             I2( "invalid cell coordinates, must be a Set of two numbers ",
     309                " coordenadas de celda invalidas, debe ser un conjunto de 2 "
     310                 "numeros" ) );
     311      status = false;
     312    } else {
     313      BDat &datRow = Dat( _cell[1] );
     314      BDat &datCol = Dat( _cell[2] );
     315      if ( !datRow.IsKnown() || datRow.Value() < 1 ||
     316           !datCol.IsKnown() || datCol.Value() < 1 ) {
     317        char buffer[256];
     318        snprintf( buffer, 256, "(%f,%f)", datRow.Value(), datCol.Value() );
     319        Error( name + ": " +
     320               I2( "invalid cell coordinates ",
     321                   "coordenadas de celda invalidas " ) + buffer );
     322        status = false;
     323      }
     324      // Cell coordinates are 0-based internally
     325      row = size_t( datRow.Value( ) - 1 );
     326      column = size_t( datCol.Value( ) - 1 );
     327      status = true;
     328    }
     329  } else {
     330    Error( name + ": " +
     331           I2( "invalid cell type, must be Text or Set ",
     332               "referencia de celda invalida debe ser de tipo Text o Set " ));
     333    status = false;
     334  }
     335  return status;
     336}
    245337
    246338TolExcel::TolExcel( )
    … …  
    561653
    562654//---------------------------------------------------------------------------
    563 DeclareContensClass(BDat, BDatTemporary, BDatExcelGetReal);
    564 DefExtOpr(1, BDatExcelGetReal, "Excel.ReadReal", 3, 3, "Real Real Real",
     655DeclareContensClass(BDat, BDatTemporary, BDatExcelReadReal);
     656DefExtOpr(1, BDatExcelReadReal, "Excel.ReadReal", 3, 3, "Real Real Real",
    565657          "(Real ExcelHandler, Real Row, Real Column)",
    566658          I2("Return the contents of the given cell as a Real. If the contents "
    … …  
    569661          BOperClassify::System_);
    570662//----------------------------------------------------------------------------
    571 void BDatExcelGetReal::CalcContens()
     663void BDatExcelReadReal::CalcContens()
    572664{
    573665  static BText _name_( "Excel.ReadReal" );
    … …  
    593685
    594686//---------------------------------------------------------------------------
    595 DeclareContensClass(BText, BTxtTemporary, BTxtExcelGetText);
    596 DefExtOpr(1, BTxtExcelGetText, "Excel.ReadText", 3, 3, "Real Real Real",
     687DeclareContensClass(BText, BTxtTemporary, BTxtExcelReadText);
     688DefExtOpr(1, BTxtExcelReadText, "Excel.ReadText", 3, 3, "Real Real Real",
    597689          "(Real ExcelHandler, Real Row, Real Column)",
    598690          I2("Return the contents of the given cell as a Text. If the contents "
    … …  
    601693          BOperClassify::System_);
    602694//----------------------------------------------------------------------------
    603 void BTxtExcelGetText::CalcContens()
     695void BTxtExcelReadText::CalcContens()
    604696{
    605697  static BText _name_( "Excel.ReadText" );
    … …  
    625717
    626718//---------------------------------------------------------------------------
    627 DeclareContensClass(BDate, BDteTemporary, BDteExcelGetDate);
    628 DefExtOpr(1, BDteExcelGetDate, "Excel.ReadDate", 3, 3, "Real Real Real",
     719DeclareContensClass(BDate, BDteTemporary, BDteExcelReadDate);
     720DefExtOpr(1, BDteExcelReadDate, "Excel.ReadDate", 3, 3, "Real Real Real",
    629721          "(Real ExcelHandler, Real Row, Real Column)",
    630722          I2("Return the contents of the given cell as a Date. If the contents "
    … …  
    633725          BOperClassify::System_);
    634726//----------------------------------------------------------------------------
    635 void BDteExcelGetDate::CalcContens()
     727void BDteExcelReadDate::CalcContens()
    636728{
    637729  static BText _name_( "Excel.ReadDate" );
    … …  
    653745  } else {
    654746    contens_ = BDate::Unknown( );
     747  }
     748}
     749
     750//---------------------------------------------------------------------------
     751DeclareContensClass(BSet, BSetTemporary, BSetExcelReadRange);
     752DefExtOpr(1, BSetExcelReadRange, "Excel.ReadRange", 3, 3, "Real Anything Anything",
     753          "(Real ExcelHandler, Anything CornerLT, Anything CornerRB)",
     754          I2("Return the contents of the cells contained given cell's range. "
     755             "The cell corners delimiting the range can be given as Text or "
     756             "Set. For instance you can use either \"A1\" or [[1,1]].",
     757             "Retorna el contenido de las celdas contenidas en el rango de "
     758             "celdas dado. Las esquinas del rango pueden especificarse como "
     759             "Text o Set. Por ejemplo se puede usar indistintamente "
     760             "\"A1\" o [[1,1]]."),
     761          BOperClassify::System_);
     762//----------------------------------------------------------------------------
     763void BSetExcelReadRange::CalcContens()
     764{
     765  static BText _name_( "Excel.ReadRange" );
     766  double addr = Dat( Arg( 1 ) ).Value();
     767  BSyntaxObject *cornerLT = Arg(2);
     768  BSyntaxObject *cornerBR = Arg(3);
     769  size_t c0, r0, c1, r1;
     770 
     771  TolExcel *xls = TolExcel::decode_addr( addr );
     772  if ( xls ) {
     773    // bool status1 = ;
     774    // bool status2 = ;
     775    if ( TolExcel::getCellCoordinates( _name_, cornerLT, r0, c0 ) &&
     776         TolExcel::getCellCoordinates( _name_, cornerBR, r1, c1 ) ) {
     777      BList*    result = NIL;
     778      BList*    aux    = NIL;
     779      size_t    r,c;
     780      for( r = r0; r <= r1; r++ ) {
     781        BList* lsta       = NIL;
     782        BList* auxa       = NIL;
     783        for( c = c0; c <= c1; c++ ) {
     784          LstFastAppend( lsta, auxa, xls->GetCellAnything( _name_, r, c ) );
     785        }
     786        LstFastAppend( result, aux, NewSet( "", "", lsta, NIL, BSet::Generic) );
     787      }
     788      contens_.RobStruct( result, NIL, BSet::Table );
     789    }
     790  } else {
     791    Error( _name_ +
     792           I2(": invalid excel handler",
     793              ": identificador de objeto excel invalido") );
    655794  }
    656795}
Note: See TracChangeset for help on using the changeset viewer.