JavaScript - Error: ';' (semicolon) expected - Asked By André Filipe on 02-Jun-09 09:17 AM

I know, it may sounds quite obvious but it is not, unfortunately.
I'm creating an Excel Application in runtime via jscript and I'm trying to create a pivot table on it. The problem happens whenever I try to create a pivot table in the pivot cache. By testing this and that I figured out that the problem happens when I declare the range where the pivot table will be placed. It always prompt me an error saying a ';' is expected.
Here is the code so far:

var cnx = "OLEDB;Provider=MSOLAP.3;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=TestCatalog;Data Source=TestDataSource";
            var app = new ActiveXObject("Excel.Application");
            var wbk = app.Workbooks.Add();        
            var con = wbk.Connections.Add("conString", "Conexão de Teste", cnx, " Cotações", 2);           
            var wsh = wbk.ActiveWorksheet;     
            var rng = wsh.Range("A1");   
                                   
            var pc = wbk.PivotCaches().Create(2, con, 1).CreatePivotTable(rng); //here prompts the error message

Santhosh N replied to André Filipe on 02-Jun-09 09:27 AM

I suppose you are doing many things in the same statement...

If you can split up the statement into individual statements then you might identify the probelm...

Can find more info here...

http://msdn.microsoft.com/en-us/library/bb238847.aspx

Indifferent - André Filipe replied to Santhosh N on 02-Jun-09 09:33 AM

Actually it doesn't make any difference if I do it splitting the methods.
I just put it in a whole line because I figured out which method is raising the error (CreatePivotTable).
The Range parameter that is messing the whole thing but I don't know why.
I don't know if it's a matter of syntax (but I don't think so) or something else but I'm getting nuts about it lol.

Use the Javascript debugger in your browser - [)ia6l0 iii replied to André Filipe on 02-Jun-09 09:38 AM

Most of the new browsers have it inbuilt
I'm using it - André Filipe replied to [)ia6l0 iii on 02-Jun-09 09:42 AM
Yeah, I'm using it on IE but this is what the debugger is returning to me.
It doesn't say if it's a problem of syntax, type mismatch or something else. Just ';' is expected, highlighting the range variable.
try this - cool adch replied to André Filipe on 02-Jun-09 09:56 AM

To make it easier to debug, could you add the following PHP code to the very first line of your script:

  header("Content-Type: text/xml");

It's a PHP statement, so it needs to go after your opening PHP tag.

...for example:

<?php
  header
("Content-Type: text/xml");
  ... 
rest of script ...
?>

This will make sure that all browsers interpret the output as XML.


Santhosh N replied to André Filipe on 02-Jun-09 09:56 AM

The destination range must be on a worksheet in the workbook that contains the PivotCache object specified by expression.

For more info..

http://msdn.microsoft.com/en-us/library/aa297858(office.10).aspx

this could be the reason - H K replied to André Filipe on 02-Jun-09 09:57 AM
The error ’semicolon’ expected can result from an text which is not escaped properly ( for example from the usage of & without using the escaped version &

The reason is the dataType “script”. If the dataType is “script” and HTML code is returned, then IE might give an error.
Check whether you have set the type as type="text/jscript" or "text/javascript"

<script type="text/javascript" language="javascript">
try this - cool adch replied to André Filipe on 02-Jun-09 09:59 AM
The reason is the dataType “script”. If the dataType is “script” and HTML code is returned, then IE tries to treat the result as Javascript and produces errors, contrary to the Firefox browser which is a bit smarter in this case. If you use dataType: “text”, it works in both browsers. Or you can use simply $.get instead of $.ajax :
Let's use the add method explicitly as shown below - [)ia6l0 iii replied to André Filipe on 02-Jun-09 10:18 AM
...
var rng = wsh.Range("A1");
...

Instead of
var pc = wbk.PivotCaches().Create(2, con, 1).CreatePivotTable(rng);

Let's try
'Creata pivotcaches which holds the datarange.
var pcs = wb.PivotCaches();
'Use the add method
var pc = pcs.Add(xlDatabase,"range")
'create a variable that holds the name of the pivottable
var pt ='myPivotTable';
'Create the pivot table.
pc.CreatePivotTable( rng, pt);
RE - Ravenet Rasaiyah replied to André Filipe on 02-Jun-09 10:19 AM
Hi

When you are create you do like this way


var cnx = "OLEDB;Provider=MSOLAP.3;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=TestCatalog;Data Source=TestDataSource";
var app = new ActiveXObject("Excel.Application");
var wbk = app.Workbooks.Add();
var con = wbk.Connections.Add("conString", "Conexão de Teste", cnx, " Cotações", 2);
var wsh = wbk.ActiveWorksheet;
var rng = wsh.Range("A1");

var pc = wbk.PivotCaches().Create("xlExternal", con, 1);
var pivotdataTable=pc.CreatePivotTable(rng);

Here more details

http://www.mrexcel.com/forum/showthread.php?t=343966
thank you
One more - Ravenet Rasaiyah replied to André Filipe on 02-Jun-09 10:20 AM
Hi

Here sample code in VB script just check you can get idea to write in  javascript
http://books.google.com.sg/books?id=qjkglmBy_l4C&pg=PA206&lpg=PA206&dq=sample+code+PivotCaches+Object&source=bl&ots=53QLKPf_-B&sig=NurYkdQa_e7-YlLYO8F05wqdKZc&hl=en&ei=qDQlSt-3N9eSkAWk7pCmBw&sa=X&oi=book_result&ct=result&resnum=1#PPA208,M1

thank you
André Filipe replied to [)ia6l0 iii on 02-Jun-09 10:32 AM
No go bro.
The problem is exactly in the last method (CreatePivotTable).
I've tried calling the methods using the less parameters was possible, just to avoid any parameter type mismatching but it's no use.
Btw, javascript doesn't allow me to write enum values that explicit (like xlDatabase). I have to write his count number in its enumerator list instead.
André Filipe replied to cool adch on 02-Jun-09 10:34 AM
No matter how much I modify my debugging way, the output will be always the same (';' is expected).
André Filipe replied to Ravenet Rasaiyah on 02-Jun-09 10:36 AM
This is the annoying thing.
I've just repeated all the vba steps (following a macro generated script from vba) but it stucks when it comes to javascript.
OK one more try - [)ia6l0 iii replied to André Filipe on 03-Jun-09 09:28 AM
Can you try having single quotes for the range instead of double quotes.
Like,
var rng = wsh.Range('A1');
instead of
var rng = wsh.Range("A1");
André Filipe replied to [)ia6l0 iii on 03-Jun-09 09:57 AM
Yeah, I've tried it first time I got the error, no way.
Well, I tried to use that PivotCache object, calling other methods but CreatePivotTable. Guess what? Got the same error. I assume the error is coming from PivotCache object, but it wasn't helpful since the error message is the same over and over.
What I'm trying to do now is reprogramming the method in vbscript. Maybe if I work with a M$ technology things become more friendly...
Aedna replied to André Filipe on 23-Feb-10 07:01 PM
Jee, I copied your code and was stuck with your error. LOL.
F=inally found the problem - it was in defining the connection string!

var


con = wbk.Connections.Add("conString", "testConn", cnx, "Cube1", 1);

4-th parameter: CommandText has to be cube name

5-th parameter: CommandType has to be 1

After these changes - all the rest worked fine.