By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,149 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,149 IT Pros & Developers. It's quick & easy.

Formulas/converting from JS?

P: n/a
Hi Guys,

I have a program which converts Excel spreadsheets to Javascript and
allows interactivity. However it can't convert it to PHP, which is
obviously better for users to view (in case J/S is turned off).

How would I go about converting some of this created code (example JS
below), or is there an easier way to get PHP to do the calculations
itself? I am aware of an excel-server product but this is too expensive
and doesnt actually produce any code.

I'm not suggesting someone convert this (also as its only an example),
but the code seems quite complicated for a junior PHP person. Any
thoughts on what I could do?

Thanks

(code created by spreadsheet converter)
<script language="javascript">

var co = new Object;
function recalc_onclick(ctl) {
if (true) {

co.pA7D=eeparseFloat(document.formc.pA7D.value);ca lc(co);document.formc.pA7E.value=eedisplayFloat(co .pA7E);
};};

var eeisus=0;var eetrue="TRUE";var eefalse="FALSE";var eedec=".";var
eeth=",";var eedecreg=new RegExp("[.]","g");var eethreg=new
RegExp(",","g");

var row1xD2D5=new Array(4);for(var jj=0;jj<4;jj++){row1xD2D5[jj]=0};var
row1xE2E5=new Array(4);for(var
jj=0;jj<4;jj++){row1xE2E5[jj]=0};function calc(data){var
cA7D=data.pA7D;row1xD2D5[0]=(10);row1xE2E5[0]=(1);row1xD2D5[1]=(20);row1xE2E5[1]=(2);row1xD2D5[2]=(30);row1xE2E5[2]=(3);row1xD2D5[3]=(40);row1xE2E5[3]=(4);var
cA7E=(lookup3vv((cA7D),row1xD2D5,0,3,row1xE2E5,0,3 ));data.pA7E=cA7E;};

function myIsNaN(x){return(isNaN(x)||(typeof
x=='number'&&!isFinite(x)));};function
eeparseFloat(str){str=String(str).replace(eedecreg ,".");var
res=parseFloat(str);if(isNaN(res)){return 0;}else{return
res;}};function eedisplayFloat(x){if(myIsNaN(x)){return
Number.NaN;}else{return String(x).replace(/\./g,eedec);}};function
lookup3vv(key,kvect,kfrom_start,kto_start,vvect,vf rom_,vto_){var
current=0;var from_=kfrom_start;var
to_=kto_start+1;while(true){current=(from_+to_)>>1 ;if(kvect[current]==key)break;if(from_==to_-1)break;if(kvect[current]<key){from_=current;}else{to_=current;}};while(cur rent<kto_start){if(kvect[current]==kvect[current+1]){current++;}else{break;};};if(key<kvect[current])return
Number.NaN;return vvect[vfrom_+current-kfrom_start]};
</script>

Feb 2 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"UKuser" <sp********@yahoo.co.uk> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi Guys,

I have a program which converts Excel spreadsheets to Javascript and
allows interactivity. However it can't convert it to PHP, which is
obviously better for users to view (in case J/S is turned off).

How would I go about converting some of this created code (example JS
below), or is there an easier way to get PHP to do the calculations
itself? I am aware of an excel-server product but this is too expensive
and doesnt actually produce any code.

I'm not suggesting someone convert this (also as its only an example),
but the code seems quite complicated for a junior PHP person. Any
thoughts on what I could do?
you would probably need to start with this (below), wich I found in the user
notes of the CHM manual of COM and .NET (Windows) section.:
you will also need to write a tokenizer and recursive-descent parser to
parse the function strings, which can be pretty involved. maybe try using
strtok?
this may be why they charge money for the product. You will need to write
at least a BNF grammar for the formula language, and it can be tricky
getting this right, especially with all those optional parameters. And excel
has a lot of functions, so the grammar is going to be at >2 pages long. A
railroad diagram may also help the process.
....and then again there is a simpler way to do it if they are all your
spreadsheets and you know them by heart.

If you want to search an Excel file and don't connect with ODBC, you can try
the function I provide. It will search a keyword in the Excel find and
return its sheet name, text, field and the row which found the keyword.
<?php
// The example of print out the result
$result = array();
searchEXL("C:/test.xls", "test", $result);
foreach($result as $sheet => $rs){
echo "Found at $sheet";

echo "<table width=\"100%\" border=\"1\"><tr>";

for($i = 0; $i < count($rs["FIELD"]); $i++)
echo "<th>" . $rs["FIELD"][$i] . "</th>";

echo "</tr>";

for($i = 0; $i < count($rs["TEXT"]); $i++) {
echo "<tr>";

for($j = 0; $j < count($rs["FIELD"]); $j++)
echo "<td>" . $rs["ROW"][$i][$j] . "</td>";

echo "</tr>";
}
echo "</table>";
}
/**
* @param $file string The excel file path
* @param $keyword string The keyword
* @param $result array The search result
*/
function searchEXL($file, $keyword, &$result) {
$exlObj = new COM("Excel.Application") or Die ("Did not connect");
$exlObj->Workbooks->Open($file);
$exlBook = $exlObj->ActiveWorkBook;
$exlSheets = $exlBook->Sheets;

for($i = 1; $i <= $exlSheets->Count; $i++) {
$exlSheet = $exlBook->WorkSheets($i);

$sheetName = $exlSheet->Name;

if($exlRange = $exlSheet->Cells->Find($keyword)) {
$col = 1;
while($fields = $exlSheet->Cells(1, $col)) {
if($fields->Text == "")
break;

$result[$sheetName]["FIELD"][] = $fields->Text;
$col++;
}

$firstAddress = $exlRange->Address;
$finding = 1;
$result[$sheetName]["TEXT"][] = $exlRange->Text;

for($j = 1; $j <= count($result[$sheetName]["FIELD"]); $j++) {
$cell = $exlSheet->Cells($exlRange->Row ,$j);
$result[$sheetName]["ROW"][$finding - 1][$j - 1] = $cell->Text;
}
while($exlRange = $exlRange->Cells->Find($keyword)) {
if($exlRange->Address == $firstAddress)
break;

$finding++;
$result[$sheetName]["TEXT"][] = $exlRange->Text;

for($j = 1; $j <= count($result[$sheetName]["FIELD"]); $j++) {
$cell = $exlSheet->Cells($exlRange->Row ,$j);
$result[$sheetName]["ROW"][$finding - 1][$j - 1] = $cell->Text;
}

}

}

}

$exlBook->Close(false);
unset($exlSheets);
$exlObj->Workbooks->Close();
unset($exlBook);
$exlObj->Quit;
unset($exlObj);
}
?>
For more information, please visit my blog site (written in Chinese)
http://www.microsmile.idv.tw/blog/index.php?p=77


Thanks

(code created by spreadsheet converter)
<script language="javascript">

var co = new Object;
function recalc_onclick(ctl) {
if (true) {

co.pA7D=eeparseFloat(document.formc.pA7D.value);ca lc(co);document.formc.pA7E.value=eedisplayFloat(co .pA7E);
};};

var eeisus=0;var eetrue="TRUE";var eefalse="FALSE";var eedec=".";var
eeth=",";var eedecreg=new RegExp("[.]","g");var eethreg=new
RegExp(",","g");

var row1xD2D5=new Array(4);for(var jj=0;jj<4;jj++){row1xD2D5[jj]=0};var
row1xE2E5=new Array(4);for(var
jj=0;jj<4;jj++){row1xE2E5[jj]=0};function calc(data){var
cA7D=data.pA7D;row1xD2D5[0]=(10);row1xE2E5[0]=(1);row1xD2D5[1]=(20);row1xE2E5[1]=(2);row1xD2D5[2]=(30);row1xE2E5[2]=(3);row1xD2D5[3]=(40);row1xE2E5[3]=(4);var
cA7E=(lookup3vv((cA7D),row1xD2D5,0,3,row1xE2E5,0,3 ));data.pA7E=cA7E;};

function myIsNaN(x){return(isNaN(x)||(typeof
x=='number'&&!isFinite(x)));};function
eeparseFloat(str){str=String(str).replace(eedecreg ,".");var
res=parseFloat(str);if(isNaN(res)){return 0;}else{return
res;}};function eedisplayFloat(x){if(myIsNaN(x)){return
Number.NaN;}else{return String(x).replace(/\./g,eedec);}};function
lookup3vv(key,kvect,kfrom_start,kto_start,vvect,vf rom_,vto_){var
current=0;var from_=kfrom_start;var
to_=kto_start+1;while(true){current=(from_+to_)>>1 ;if(kvect[current]==key)break;if(from_==to_-1)break;if(kvect[current]<key){from_=current;}else{to_=current;}};while(cur rent<kto_start){if(kvect[current]==kvect[current+1]){current++;}else{break;};};if(key<kvect[current])return
Number.NaN;return vvect[vfrom_+current-kfrom_start]};
</script>

Feb 9 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.