Topic: Javascript Help for Case Conversion
Javascript Help for Case Conversion
Hi,
I'm a new user of Datacleaner, and am exploring normalizing & cleaning my set of data.
Was trying to find a way to capitalize all my text in my data fields, and came upon an earlier post in forum [http://datacleaner.eobjects.org/topic/193/Alphabet-case-conversion]
I did the exact same code input, and ran into an error when I preview output
Error as follow:
ReferenceError: "Name" is not defined. (JavaScriptTransformer#4)
I only selected a data field say 'Course Name' . How should i go about declaring the var to use the data in the corresponding field?
A pity there is no documentation on how to use this, as i believe this function coupled with javascript will give a lot more features than current software can provide. I'm just a beginner in javascript as well.. btw..
Thanks again for any help!
PS: I am reposting as it seems a reply to old post will not be reflected in forum!
I'm a new user of Datacleaner, and am exploring normalizing & cleaning my set of data.
Was trying to find a way to capitalize all my text in my data fields, and came upon an earlier post in forum [http://datacleaner.eobjects.org/topic/193/Alphabet-case-conversion]
I did the exact same code input, and ran into an error when I preview output
Error as follow:
ReferenceError: "Name" is not defined. (JavaScriptTransformer#4)
I only selected a data field say 'Course Name' . How should i go about declaring the var to use the data in the corresponding field?
A pity there is no documentation on how to use this, as i believe this function coupled with javascript will give a lot more features than current software can provide. I'm just a beginner in javascript as well.. btw..
Thanks again for any help!
PS: I am reposting as it seems a reply to old post will not be reflected in forum!
Hi wsquare,
To reference a column name that is not possible as a Javascript variable, you can use the "values" array. This will reference the columns in the order you've selected them above, so if you only select "Course name" in the checkboxes above, then you can reference it like this:
To reference a column name that is not possible as a Javascript variable, you can use the "values" array. This will reference the columns in the order you've selected them above, so if you only select "Course name" in the checkboxes above, then you can reference it like this:
var contactName = values[0];
PS: The forum sends out notifications to the datacleaner-notify mailing list as well as to any previous posters of a topic, so your reply was distributed at least to those who has interacted in the other topic and to those who have a general wish to monitor the forum :)
hi,
I'm back to this. If I want to apply the same scripting code to few selected columns, how should it work?
I'm back to this. If I want to apply the same scripting code to few selected columns, how should it work?
var contactName = values[0];
var contactName1 = values[1];
var contactName2 = values[2];
return contactName.toUpper();
How do i do so for the other columns?
I tried repeating the return lines with the different variables.. it didnt work.
Pardon me for this abhorrent logic.
Hi wsquare,
The Javascript transformer can only return a single value, so if you want to return multiple, then you need to add more transformers.
The Javascript transformer can only return a single value, so if you want to return multiple, then you need to add more transformers.
Oh. So basically the multiple columns work, if i only return a single value ie
contactName = values[0]+values[1]..
...
return contactName
?
I have done as what you have proposed, but running foul with a problem.
I tried a workaround for this:
And i have another error arising :
java.lang.IllegalStateException: Could not invoke initializing method public void org.eobjects.analyzer.beans.script.JavaScriptTransformer.init()
Could not invoke initializing method public void org.eobjects.analyzer.beans.script.JavaScriptTransformer.init()
null
missing ( before condition (JavaScriptTransformer#5)
How can i troubleshoot this error :(
Thanks again!
contactName = values[0]+values[1]..
...
return contactName
?
I have done as what you have proposed, but running foul with a problem.
var surname = values[0];Cannot call method "toUpperCase" of null (JavaScriptTransformer#5)as i have null values..
var upper = surname.toUpperCase();
I tried a workaround for this:
if surname.replace(/\s,"")==""
surname ==""
else var upper = surname.toUpperCase();
And i have another error arising :
java.lang.IllegalStateException: Could not invoke initializing method public void org.eobjects.analyzer.beans.script.JavaScriptTransformer.init()
Could not invoke initializing method public void org.eobjects.analyzer.beans.script.JavaScriptTransformer.init()
null
missing ( before condition (JavaScriptTransformer#5)
How can i troubleshoot this error :(
Thanks again!
Hmm the init() issue you are seeing is because the Javascript has syntactic issues.
But coming back to your initial solution, I would recommend just checking for nulls, like this:
But coming back to your initial solution, I would recommend just checking for nulls, like this:
var surname = values[0];
var upper;
if (surname == null) {
upper = null;
} else {
upper = surname.toUpperCase();
}
return upper;
Hi,
Thanks. I've made the necessary recitification and it works!
Generally, i did 3 separate Javascript transformers using the same code.
Everything works fine until I tried to concatenate them together.
ie. null field turn out to have 'null' word stored in the transformed field?
So if i pieced two fields : ALAN null => "ALAN null"
Shouldn't it be empty instead?
Hm, sorry is there something wrong that I've done? Thanks
Thanks. I've made the necessary recitification and it works!
Generally, i did 3 separate Javascript transformers using the same code.
Everything works fine until I tried to concatenate them together.
ie. null field turn out to have 'null' word stored in the transformed field?
So if i pieced two fields : ALAN null => "ALAN null"
Shouldn't it be empty instead?
Hm, sorry is there something wrong that I've done? Thanks
unfortunately, that is just how Javascript is designed when you concat nulls with strings!
Maybe you can make a small utility function like this:
and then use it when getting your values...
Good luck
Maybe you can make a small utility function like this:
function strValue(str) {
if (str == null) {
return "";
}
return str;
}
and then use it when getting your values...
var surname = strValue(values[0]);
Good luck
hi kasper,
Need your expertise advise in this as none of my workarounds work :(
1. I did another script transformer on my concatenated output field ie:
2. Another workaround I tried is your utility function plus a mixture of others. All failed
I tried to assign a new value 'emtpy' if null field is detected, but somehow, it just refused to assign the new value..
Similar to this :
output still appears null all over.. :(
Any idea what's wrong? Thanks again..
Need your expertise advise in this as none of my workarounds work :(
1. I did another script transformer on my concatenated output field ie:
gname=values[0];
return gname.replace(/null/g,"");
2. Another workaround I tried is your utility function plus a mixture of others. All failed
I tried to assign a new value 'emtpy' if null field is detected, but somehow, it just refused to assign the new value..
Similar to this :
var fn = values[0];
var mn = values[1];
var empty = "empty"
if (mn==null) {
mn = empty;
}
return fn +" "+mn;
output still appears null all over.. :(
Any idea what's wrong? Thanks again..
Hi wsquare,
I tried this in the example database (table=EMPLOYEES, columns=OFFICECODE+REPORTSTO) which has a few null values:
I tried this in the example database (table=EMPLOYEES, columns=OFFICECODE+REPORTSTO) which has a few null values:
function eval() {
var fn = strValue(values[0]);
var mn = strValue(values[1]);
return fn +" " + mn;
}
function strValue(str) {
if (str == null) {
return "";
}
return str;
}
eval();
And that works fine
Also through this series of troubleshootings, i discovered that there was abnormality in data import from my excel sheet?
My data rows have no empty spaces, but after import, take a look at the attached picture link :
[http://imageshack.us/photo/my-images/823/mnempty.jpg/]
There are 'null' indication, when the rest are all blank field.
Hm, i'm kinda bewildered. Any help?
Thanks
My data rows have no empty spaces, but after import, take a look at the attached picture link :
[http://imageshack.us/photo/my-images/823/mnempty.jpg/]
There are 'null' indication, when the rest are all blank field.
Hm, i'm kinda bewildered. Any help?
Thanks
Hi wsquare,
This is because there is in data processing a significant difference between NULL and empty string. NULL means "no value" (eg. the spreadsheet file format does not even mention that cell's content) and empty string means ... yea, well, an empty string value!
In the script I posted before, NULLs are being converted to empty strings. Maybe you want to convert further?
This is because there is in data processing a significant difference between NULL and empty string. NULL means "no value" (eg. the spreadsheet file format does not even mention that cell's content) and empty string means ... yea, well, an empty string value!
In the script I posted before, NULLs are being converted to empty strings. Maybe you want to convert further?
Hi kasper,
Thanks for getting me on track. I discovered my field of feeding mn is the output of this code :
weird thing is i did a minor change to the code :
and the code works well by not showing null after concatenation!
Saved my day. :)
Thanks for getting me on track. I discovered my field of feeding mn is the output of this code :
if (name == null ){
punctuationless = null;
}
return punctuationless = name.replace(/\s{2,}/g,"")
weird thing is i did a minor change to the code :
if (name == null ){
punctuationless = "";
}
and the code works well by not showing null after concatenation!
Saved my day. :)
i think i got what you mean regarding null and empty string "" through this series of workarounds i've done!
Happy that you got it working :)
awesome. i'm on track now. thanks alot kasper!
I took me awhile to work out the js expressions such as removing punctuation marks, doing word count of a string etc. Simple ones like converting cases of string
All these were done using javascript transformers. It may be good to consider such string transformers in future release.
And i'm just using all these basic transformers to assist me in cleaning name data fields.
I took me awhile to work out the js expressions such as removing punctuation marks, doing word count of a string etc. Simple ones like converting cases of string
All these were done using javascript transformers. It may be good to consider such string transformers in future release.
And i'm just using all these basic transformers to assist me in cleaning name data fields.
Log in by clicking the login link at the top of the screen
Go back to forum.


