0

I want to use a function and the parameter I am passing should be set to global all the time. How can I do that? Here is my code:

function ReadExcelfunction($FileName,$SheetName,$RowNum,$ColNum,$Parameter)
{
    var $excel = _getExcel($FileName,$SheetName);
    var $excelData=$excel.getData();
    var $Parameter=$excelData[$RowNum][$ColNum];
    //_setGlobal($Parameter,$excelData[$RowNum][$ColNum]) -- Commented
}

Now suppose I pass the parameters as -- File sheet 1 1 Name.

What I want is this: name with the Value is stored as Global value.

Earlier I used _setGlobal($Parameter,$excelData[$RowNum][$ColNum]) which solved the purpose but this API has been removed and now I need to change my script.

I am using the sahi scripting language which is similar to JavaScript.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Mrinal kumar
  • 67
  • 1
  • 1
  • 9
  • 2
    It may be similar to JavaScript, but it *isn't* JavaScript and the mechanism of setting globals is environment-specific. I've removed the `javascript` tag. – T.J. Crowder Jul 17 '15 at 07:01
  • _but this API has been removed and now I need to change my script_ When did that happen? Can't find anything in the release notes – globalworming Jul 20 '15 at 04:56

2 Answers2

0

A probable solution for you is to write value to an existing excel sheet cell from the sahi script on the fly and then retrieve the same value from the corresponding cell of excel sheet from another sahi script.

I have done a similar thing using a simple text file.
First File

\\ script 1
var $str = "Hello World";
var$filePath = "C:\\temp\\mystring.txt";
_writeToFile($str, $filePath);

Second File

\\ script 2
var $filePath = "C:\\temp\\mystring.txt"; 
var $mystr = _readFile($filePath);

Edit: Extended Answer

--------------------------------
| VariableName | VariableValue |
--------------------------------
|    MyVar1    |  MyVar1Value  |
--------------------------------
|    MyVar2    |  MyVar2Value  |
--------------------------------
|    MyVar3    |  MyVar3Value  |
--------------------------------

Create a Sahi Function to creating a new excel sheet:

function _createExcelFile($fileName, $sheetName){
  if($sheetName == null || $sheetName == ""){
    $sheetName = "Sheet1";
  }
  _debug("Excel File Name :: " + $fileName);
  _debug("Excel File Sheet Name :: " + $sheetName);
  var $excelPoi = new Packages.net.sf.sahi.util.ExcelPOI($fileName, $sheetName);
  $excelPoi.createNew();
}

Further you can refer the _getExcel($filePath[, $sheetName]) API from Sahi Pro. You need to do some iterations to get things done. You can read and write data as required.

Note: I have tried this only in the latest version of Sahi Pro i.e. 6.1.0.

rahoolm
  • 733
  • 2
  • 12
  • 22
0

You don't need to use _setGlobal, you only need to declare a var before the function declaration. For example, you can declare a new Array and then set/get values just like _setGlobal.

// www.google.com used as start URL
var $globalVariable = new Array();

function SetGlobalVariable(key, newValue) {
  $globalVariable[key] = newValue;
}

SetGlobalVariable('inputValue', 'stack overflow');
_setValue(_textbox("q"), $globalVariable['inputValue']);
Dave Kidder
  • 1,758
  • 13
  • 16
  • This does not work when you need the values in different scripts like one script needs to write these and one script needs to read. You could _include your function but you can't dynamically set and get values between scripts. – globalworming Jul 24 '15 at 13:29
  • The question doesn't refer to needing values in different scripts. Furthermore, having one test case script depend on data from another test case script is a very bad idea. If you are referring to one test case that uses multiple sah files, the above works just fine by using the global_include.sah. – Dave Kidder Jul 24 '15 at 14:33
  • _having one test case script depend on data from another test case script is a very bad idea_ that's very arguable, but you're right, the question does not state the need for dynamic globals. I will shut up now :) – globalworming Jul 25 '15 at 22:28