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 »

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: 4667
Joined: Wed Mar 05, 2003 3:10 am

Re: Problems of executing JScript

Post by Administrator »

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 »

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: 4667
Joined: Wed Mar 05, 2003 3:10 am

Re: Problems of executing JScript

Post by Administrator »

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 »

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: 4667
Joined: Wed Mar 05, 2003 3:10 am

Re: Problems of executing JScript

Post by Administrator »

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 »

Neither of them works.
Administrator
Super Administrator
Posts: 4667
Joined: Wed Mar 05, 2003 3:10 am

Re: Problems of executing JScript

Post by Administrator »

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 »

This is my project. Thanks for your help!
Attachments
test.zip
(281.63 KiB) Downloaded 2328 times
Administrator
Super Administrator
Posts: 4667
Joined: Wed Mar 05, 2003 3:10 am

Re: Problems of executing JScript

Post by Administrator »

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 »

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