Problems of executing JScript

Use this forum to post Sable related questions.

Moderator: Administrator

Post Reply
cphan
Junior Member
Posts: 16
Joined: Mon Jun 16, 2014 3:31 pm
Vensim version: DSS

Problems of executing JScript

Post by cphan » Tue Jul 01, 2014 2:47 pm

I've tried to run the following script in Sable but it did not work. I've got the error message "One of the commands on the button failed to execute. XCommand: Execute Script:JScript.test()". Would you please help me solve this problem? Thanks!

Code: Select all

function test()
{
    var rangeCount = GetNumberOfSubscriptRanges();
	for (var i=0;i<rangeCount;i++){
		var name = GetSubscriptRangeName(i);
		name = name.replace(/"/g,'\'');
		var elementCount = GetNumberOfSubscriptElements(name);
		for(var j = 0;j<elementCount;j++){
			var elementName = GetSubscriptElement(name,j);
			elementName = elementName.replace(/"/g,'\'');
		}
	}
}

Administrator
Super Administrator
Posts: 3421
Joined: Wed Mar 05, 2003 3:10 am

Re: Problems of executing JScript

Post by Administrator » Tue Jul 01, 2014 4:59 pm

The following does not look right

Code: Select all

name = name.replace(/"/g,'\'');
Shouldn't it be something like

Code: Select all

name = name.replace("g","a'');
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559

cphan
Junior Member
Posts: 16
Joined: Mon Jun 16, 2014 3:31 pm
Vensim version: DSS

Re: Problems of executing JScript

Post by cphan » Wed Jul 02, 2014 7:36 am

According to the manual of Microsoft Jscript (http://msdn.microsoft.com/en-us/library ... 90%29.aspx), name.replace(/"/g,'\''); replaces " by ', am i wrong?
By the way, i remove 2 lines with "replace" methods, it does not work either.

Administrator
Super Administrator
Posts: 3421
Joined: Wed Mar 05, 2003 3:10 am

Re: Problems of executing JScript

Post by Administrator » Wed Jul 02, 2014 8:47 am

The function works fine. But you don't return any value from it, so Sable assumes it must have failed. Try the following.

Code: Select all

function test()
{
	var rangeCount = GetNumberOfSubscriptRanges();
	for (var i=0;i<rangeCount;i++)
	{
		var name = GetSubscriptRangeName(i);
		name = name.replace(/"/g,'\'');
		var elementCount = GetNumberOfSubscriptElements(name);
		for(var j = 0;j<elementCount;j++)
		{
			var elementName = GetSubscriptElement(name,j);
			elementName = elementName.replace(/"/g,'\'');
		}
	}
	return 1;
}
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559

cphan
Junior Member
Posts: 16
Joined: Mon Jun 16, 2014 3:31 pm
Vensim version: DSS

Re: Problems of executing JScript

Post by cphan » Wed Jul 02, 2014 9:20 am

It works like a charm in Sable Developer!
However, it does not work with Sable Runtime, i have the error
"Error: Microsoft JScript runtime error
Error executing : x= test()
Reason: expected object
Text:
Line:3"

Thanks a lot for your help!

Administrator
Super Administrator
Posts: 3421
Joined: Wed Mar 05, 2003 3:10 am

Re: Problems of executing JScript

Post by Administrator » Wed Jul 02, 2014 2:48 pm

In Runtime, are you opening a SPK or the VNP file?
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559

cphan
Junior Member
Posts: 16
Joined: Mon Jun 16, 2014 3:31 pm
Vensim version: DSS

Re: Problems of executing JScript

Post by cphan » Wed Jul 02, 2014 2:56 pm

Neither of them works.

Administrator
Super Administrator
Posts: 3421
Joined: Wed Mar 05, 2003 3:10 am

Re: Problems of executing JScript

Post by Administrator » Thu Jul 03, 2014 8:49 am

Can you send me the script file? The code works fine for me.
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559

cphan
Junior Member
Posts: 16
Joined: Mon Jun 16, 2014 3:31 pm
Vensim version: DSS

Re: Problems of executing JScript

Post by cphan » Thu Jul 03, 2014 12:09 pm

This is my project. Thanks for your help!
Attachments
test.zip
(281.63 KiB) Downloaded 172 times

Administrator
Super Administrator
Posts: 3421
Joined: Wed Mar 05, 2003 3:10 am

Re: Problems of executing JScript

Post by Administrator » Fri Jul 04, 2014 10:07 am

What happens for you in Runtime? I click the button and the code executes correctly. The function you've written doesn't do anything that would show on the screen.
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559

cphan
Junior Member
Posts: 16
Joined: Mon Jun 16, 2014 3:31 pm
Vensim version: DSS

Re: Problems of executing JScript

Post by cphan » Fri Jul 04, 2014 12:53 pm

I reinstall Sable, it works now. Thanks again for your help!

Post Reply