18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

Want to store Excel worksheet names in variable.

nagarnagar Members
edited November -1 in Sahi - Open Source
Hi All,

I am reading and updating Excel sheets using Sahi. As per my scenario, I want to read/update different different worksheets, by passing their names into a common sahi script.

These are the code lines, where I read two worksheets at a time:
$DB_FileValue1 = $db.select("select * from [Sheet1$]");
$DB_FileValue2 = $db.select("select * from [sheet2$]");

Now, when I run the script next time, I want to read another two worksheets. That's why I need to pass worksheet names, through variables. Is it possible with Sahi?
If I have 4 worksheets in an Excel file, say, A,B,C,D. First time, I read A,B and then next time, C,D.
So instead of changing sheet names, everywhere in the code, is it possible that I assign these names to variables?
$Sheet1 = "A";
$Sheet2 = "B"; (Like this?)

Please help in this matter.

Comments

  • Hi nagar

    why don't you encapsulate your whole script in a function worksheet($sheet1, $sheet2){ } and replace $DB_FileValue1 = $db.select("select * from [Sheet1$]") with $DB_FileValue1 = $db.select("select * from "+$sheet1);
    Now you could run this script with worksheet("A", "B"); worksheet("C", "D");

    Is that what you want?

    Regards
    Wormi
  • nagarnagar Members
    Hi globalworming,

    I tried your suggested approach but that also is not working.

    I am having two worksheets named as, "Action" & "Value".
    I assigned these names to global variables:
    var $sheet1 = "Action";
    var $sheet2 = "Value";

    and replaced the SELECT query with:
    $DB_FileValue1 = $db.select("select * from" + $sheet1);
    $DB_FileValue2 = $db.select("select * from" + $sheet2);


    But it's not working. Kindly help.
  • globalwormingglobalworming Moderators
    edited March 2012
    Hi

    there might be missing a space -> $DB_FileValue1 = $db.select("select * from " + $sheet1)
    are there any errors displayed?
  • nagarnagar Members
    edited March 2012
    Hi globalworming,

    I inserted space also, within SELECT query:
    $DB_FileValue1 = $db.select("select * from " + $sheet1);
    $DB_FileValue2 = $db.select("select * from " + $sheet2);

    Below are the Errors, when I try to: _alert($DB_FileValue1);
    On Log file:
    ERROR
    exception: java.sql.SQLException: [Microsoft][ODBC Excel Driver] The Microsoft Office Access database engine could not find the object 'Action'. Make sure the object exists and that you spell its name and the path name correctly.
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
    at net.sf.sahi.plugin.DBClient.getResult(DBClient.java:101)
    at net.sf.sahi.plugin.DBClient.select(DBClient.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:161)
    at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:247)
    at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3330)
    at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2487)
    at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:398)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3065)
    at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:175)
    at org.mozilla.javascript.Context.evaluateString(Context.java:1104)
    at net.sf.sahi.rhino.RhinoScriptRunner.run(RhinoScriptRunner.java:114)
    at java.lang.Thread.run(Unknown Source) at Mar 14, 2012 3:10:21 PM

    And on console:
    java.sql.SQLException: [Microsoft][ODBC Excel Driver] The Microsoft Office Acces
    s database engine could not find the object 'Action'. Make sure the object exis
    ts and that you spell its name and the path name correctly.
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
    at net.sf.sahi.plugin.DBClient.getResult(DBClient.java:101)
    at net.sf.sahi.plugin.DBClient.select(DBClient.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:161)
    at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:24
    7)
    at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:333
    0)
    at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2487)
    at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.j
    ava:164)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:3
    98)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:306
    5)
    at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.j
    ava:175)
    at org.mozilla.javascript.Context.evaluateString(Context.java:1104)
    at net.sf.sahi.rhino.RhinoScriptRunner.run(RhinoScriptRunner.java:114)
    at java.lang.Thread.run(Unknown Source)
  • nagarnagar Members
    Hi globalworming,

    Many thanks for your kind help. I have got the solution.

    I assigned worksheet names to variables, in this manner:
    var $sheet1 = "[Action$]";
    var $sheet2 = "[Value$]";

    and now, it is working perfectly. :)

    Approach suggested by you is much appreciated.

    regards,
    nagar.
  • Do you have any idea why the way you are assigning the names is working now? That makes no real sense to me :)
  • Do you have any idea why the way you are assigning the names is working now? That makes no real sense to me :)

    Hi globalworming,

    It's because of the worksheet name specified.
    It should be inside a square bracket and should have a dollar sign after the worksheet name. :)
  • I have also got the same error can u pls let me know how to resolve this ,
    I think it is some where missing at DSN creation or in connection string.

    var $Db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:Driver = {Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ = D://myfolder//sample.xls;readOnly = false", "", "");

    I used..

    here and i created a system dsn sample, then where we are using this dsn in connection query.
  • Hi santosh,
    var $Db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:Driver = {Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ = D://myfolder//sample.xls;readOnly = false", "", "");

    I think there is a problem on your file directory. Since you used a backslash, there should only be one slash only for each occurrence,
    D:/myfolder/sample.xls
    
    If you'll use forward slash (same as with the Sahi documentation), there should be two slashes for each usage
    D:\\myfolder\\sample.xls
    


    Thanks and regards.
  • Thanks for your quick response..

    I have replaced the step D:/myfolder/sample.xls with
    D:\\myfolder\\sample.xls

    but it doesn't work , still i am receiving same error. Can u let me know any other solution.

    Thanks and regards,
Sign In or Register to comment.