Javascript in Pentaho doesn't perform row by row operation? - javascript

I have the following data:
I use this javascript:
var primary_phone ;
if (inter1.length == 10 && inter2.length == 10 && inter3.length == 0) {
primary_phone = inter1;
}
else if (inter1.length == 10 && inter2.length == 10 && inter3.length == 10) {
primary_phone = inter1;
}
else if (inter1.length != 10 && inter2.length == 10 && inter3.length == 0) {
primary_phone = inter2;
}
else if (inter1.length != 10 && inter2.length != 10 && inter3.length != 10) {
primary_phone = "+000000000000";
}
else if (inter1.length == 10 && inter2.length == 0 && inter3.length == 0) {
primary_phone = inter1;
}
And what I get is:
Instead of:
Do you have an idea?

Simplified:
var primary_phone;
if ( inter1.length == 10 && (inter3.length == 0 || inter3.length == 10) {
primary_phone = inter1;
} else if ( inter2.length == 10 && inter3.length == 0 ) {
primary_phone = inter2;
} else if ( inter3.length != 10 ) {
primary_phone = "+000000000000";
}
if ( primary_phone != undefined ) {
//primary_phone was assigned a value
}

The Javascript is not initialized per row, it (and the variables you assign) persists for the runtime of the transformation. In fact, you can have separate tabs for the start, per-row and end scripts.
To make it work, you need to set reset primary_phone either at the start of the script or in the else clause:
else {
primary_phone = null;
}

Related

What is the best way to deal with multiple else if statements to increase speed in Google Apps Script?

I have a function that is supposed to unhide certain columns, but only if other filters that relate to the columns are not in use. Because there are 4 other filters that it needs to check to see if they are in use (either 'true' or 'false'), there are 16 possibilities that the function needs to run through.
I've used else if statements to accomplish this and it does work, but it is incredibly slow. I was wondering if there is a more appropriate way to deal with all the possible options that is faster.
This is the code I currently have (sorry if I've shown too much, not sure how much I need to include):
function Unfilter(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var numCols = sheet.getRange(1,3).getValue(); //gets the number of columns to loop through
var xRow = sheet.getRange(1,5).getValue() + 16; //gets the target row to run the blank check on
// check filter statuses
var nameShow = sheet.getRange(1,1).getValue();
var statusShow = sheet.getRange(2,1).getValue();
var dateShow = sheet.getRange(3,1).getValue();
var evidenceShow = sheet.getRange(4,1).getValue();
//loop through all target columns and unhide all columns that are not filtered
for (var i=10; i<=numCols; i++) {
if (sheet.getRange(xRow,i).getValue() == "") {
var catType = sheet.getRange(16,i).getValue();
if (nameShow == true && statusShow == true && dateShow == true && evidenceShow == true) {
sheet.showColumns(i)
} else if (nameShow == false && statusShow == true && dateShow == true && evidenceShow == true) {
if(catType !== "Name") {
sheet.showColumns(i);
}
} else if (nameShow == false && statusShow == false && dateShow == true && evidenceShow == true){
if (catType == "Date" || catType == "Evidence") {
sheet.showColumns(i);
}
} else if (nameShow == false && statusShow == true && dateShow == false && evidenceShow == true) {
if (catType == "Status" || catType == "Evidence") {
sheet.showColumns(i);
}
} else if (nameShow == false && statusShow == true && dateShow == true & evidenceShow == false){
if (catType == "Status"|| catType == "Date") {
sheet.showColumns(i);
}
} else if (nameShow == false && statusShow == false && dateShow == false && evidenceShow == true){
if (catType == "Evidence") {
sheet.showColumns(i);
}
} else if (nameShow == false && statusShow == false && dateShow == true && evidenceShow == false){
if (catType == "Date") {
sheet.showColumns(i);
}
}
//...etc for all 9 remaining possibilities
}
}
}
Even if you can't speed up showColumns() function you can significantly accelerate your script if you will use getValue() or getValues() as few as it possible. For example here you invoke these functions more than 7 times on the same sheet with static data. It's far from best practices:
You could use getValues() just once to get all data from the sheet and analyze the array instead. It would be much faster. getValue() / getValues() are quite slow and intentionally limited functions.
For example:
var data = sheet.getRange(1,1,10,10).getValues(); // get the array
var numCols = data[0][2];
var xRow = data[0][4] + 16;
var nameShow = data[0][0];
var statusShow = data[1][0];
var dateShow = data[2][0];
var evidenceShow = data[3][0];
// etc
I think it will be about five seconds faster already.
And it will even more faster if you change getRange(xRow,i).getValue() to data[xRow-1][i-1] in the loop.

How to make function simple

Maybe delete some logical Operators, make a "Check function "?
Or connect some logical in one piece?
// Function
function getTicketPrice(childNumber,adultNumber){
if (childNumber > 2 && adultNumber > 2) {
return "-";
}
if (childNumber == 2 && adultNumber == 3) {
return "-";
}
if (childNumber == 3 && adultNumber == 2) {
return "-";
}
var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Ticket");
var row = getTicketPriceChild(childNumber, sheet);
var col = getTicketPriceAdult(adultNumber, sheet);
if (row >-1 && col === undefined) {
return "-";
}
if (row === undefined && col >-1) {
return "-";
}
var value = sheet.getRange(row, col).getValue();
if(value > 1){
return value;
} else {
return '-';
}
}
Something like this:
function getTicketPrice(childNumber, adultNumber) {
if ((childNumber > 2 && adultNumber > 2) || (childNumber == 2 && adultNumber == 3) || (childNumber == 3 && adultNumber == 2))
return "-";
const sheet = SpreadsheetApp.openByUrl(url).getSheetByName('Ticket');
const row = getTicketPriceChild(childNumber, sheet);
const col = getTicketPriceAdult(adultNumber, sheet);
if ((row > -1 && col === undefined) || (row === undefined && col > -1))
return "-";
const value = sheet.getRange(row, col).getValue();
return value > 1 ? value : "-"
}

Salesforce button uses same Docusign template regardless of related Opportunity

when I use the button, the same Docusign template is used regardless of the associated Opportunity. If I move the (OPPNAME = 'WorldQuant LLC%') line to the end, a different template is used, but again its the same regardless of Opportunity Name. I am new to Javascript so feel free to dumb down any answers, thanks.
var OPPNAME;
var PICKDSTEMPLATE;
OPPNAME == '{!JSENCODE(Opportunity.Name)}';
PICKDSTEMPLATE = '{!Opportunity.Type_of_Opportunity__c}';
if (OPPNAME = 'WorldQuant LLC%')
{
DST = '75E95019-6513-4EE9-8f6B-C2FD9A99B9C2';
}
else if (OPPNAME != 'WorldQuant LLC%' && PICKDSTEMPLATE == 'Corp/Ind')
{
DST='38FE916A-0F28-46FD-BBAB-28CA54621B7E';
}
else if (OPPNAME != 'WorldQuant LLC%' && PICKDSTEMPLATE == 'Corporate from Lead')
{
DST = 'ACB0C25C-38FE-4802-9BEA-8CA5D8AA77A9';
}
else if (OPPNAME != 'WorldQuant LLC%' && PICKDSTEMPLATE == 'Corporate')
{
DST = 'ACB0C25C-38FE-4802-9BEA-8CA5D8AA77A9';
}
Got this to work with some help:
var ACCNAME;
var PICKDSTEMPLATE;
ACCNAME = '{!Opportunity.Account}';
PICKDSTEMPLATE = '{!Opportunity.Type_of_Opportunity__c}';
if ((ACCNAME == 'WorldQuant LLC') && (PICKDSTEMPLATE == 'Corp/Ind' || PICKDSTEMPLATE == 'Corporate'))
{
DST = '75e95019-6513-4ee9-8f6b-c2fd9a99b9c2';
}
else if ((ACCNAME != 'WorldQuant LLC') && (PICKDSTEMPLATE == 'Corp/Ind'))
{
DST='38FE916A-0F28-46FD-BBAB-28CA54621B7E';
}
else if ((ACCNAME != 'WorldQuant LLC') && (PICKDSTEMPLATE == 'Corporate from Lead'))
{
DST = 'ACB0C25C-38FE-4802-9BEA-8CA5D8AA77A9';
}
else if ((ACCNAME != 'WorldQuant LLC') && (PICKDSTEMPLATE == 'Corporate'))
{
DST = 'ACB0C25C-38FE-4802-9BEA-8CA5D8AA77A9';
}

Validate Percentage Field

Hi I use the below code to validate a Percentage Field.
The field can accept 2 digits before decimal and 1 digit after decimal.
Values Valid are:
89, 99.9, 1.9,100.
My code is allowing till 99.9 and not 100. How to change the code to let the user enter 100 also?
function run(el, evt, DivID) {
var charCode = (evt.which) ? evt.which : event.keyCode;
var number = el.value.split('.');
var val = el.value;
var caratPos = getItemSelStart (el);
var dotPos = el.value.indexOf(".");
if(number.length>1 && charCode == 46){
return false;
}
// Added these checks to input 100 also.
**if ( val == 10 && (val.length > 1 && val.length < 3) && charCode == 48 ) {
return true;
}
if (dotPos == -1 && (val.length > 2) && String.fromCharCode(charCode) == '.') {
return false;
}**
if(dotPos == -1 && val.length > 1 && String.fromCharCode(charCode)!='.' ) {
return false;
}
if(caratPos > dotPos && (dotPos > -1 ) && number[1].length > 0 ) {
return false;
}
if(caratPos == dotPos && dotPos > 1 && number[1].length > 0) {
return false;
}
return true;
}
function getItemSelStart(o) {
if (o.createTextRange) {
var r = document.selection.createRange().duplicate();
r.moveEnd('character', o.value.length);
if (r.text == '') return o.value.length;
return o.value.lastIndexOf(r.text);
} else return o.selectionStart;
}

How to fix and work with tree toc?(old syntax)

so i bought a site witch is connected to a gestional programm in office..
the site is a little old, it's fully compatible with all the versions of internet explorer but not with other browsers(opera,safari,chrome,firefox...)
the only piece not working is a menu not opening correctly, when you click on it it doesn't happen anything or in chrome it changes the image but it doesn't upload the links. it seems that it doens't upload things in the hiddenframe...
in chrome it gives error in loadchildren function.
in firefox it gives error in function Toc_click ()
TypeError: window.event is undefined
[Break On This Error]
var eSrc = window.event.srcElement;
we searched for the problem and we think that the problem should be in this file
the problem should be a syntax problem, it's compatible in old browsers but not on the new ones. i cannot change completely the code as it is connected with other files(a lot) and sql server...
we already tried to change
document.frames["hiddenframe"].location.replace(strLoc);
by
document.getElementsByName("hiddenframe").location.replace(strLoc);
it doens't work and it doesn't work on IE either...
/* TOC.JS */
var framesTop = parent.parent;
//var L_LoadingMsg_HTMLText = "Loading, click to cancel...";
var LoadDiv = '<DIV ONCLICK="loadFrame(true);" CLASS="clsLoadMsg">';
L_LoadingMsg_HTMLText = LoadDiv + L_LoadingMsg_HTMLText + "</LI>";
function caps(){
var UA = navigator.userAgent;
if(UA.indexOf("MSIE") != -1)
{
this.ie = true;
this.v = UA.charAt(UA.indexOf("MSIE") + 5);
if( this.v == 2 ) this.ie2 = true;
else if( this.v == 3 ) this.ie3 = true;
else if( this.v == 4 ) this.ie4 = true;
else if( this.v == 5 ) this.ie5 = true;
else if( this.v == 6 ) this.ie6 = true;
else this.ie6 = true;
}
else if(UA.indexOf("Mozilla") != -1 && UA.indexOf("compatible") == -1)
{
this.nav = true;
var v = UA.charAt(UA.indexOf("Mozilla") + 8);
if(v == 2 ) this.nav2 = true;
else if(v == 3 ) this.nav3 = true;
else if(v == 4 ) this.nav4 = true;
}
if(UA.indexOf("Windows 95") != -1 || UA.indexOf("Win95") != -1 || UA.indexOf("Win98") != -1 || UA.indexOf("Windows 98") != -1 || UA.indexOf("Windows NT") != -1 || UA.indexOf("Windows XP") != -1) this.win32 = true;
else if(UA.indexOf("Windows 3.1") != -1 || UA.indexOf("Win16") != -1) this.win16 = true;
else if(UA.indexOf("Mac") != -1) this.anymac = true;
else if(UA.indexOf("SunOS") != -1 || UA.indexOf("HP-UX") != -1 || UA.indexOf("X11") != -1) this.unix = true;
else if(UA.indexOf("Windows CE") != -1) this.wince = true;
}
var bc = new caps();
////////////////////////////////////////////
// Not sure why this is here, it puts a scrollbar up when none is needed
// if("object" == typeof(parent.document.all.fraPaneToc)) parent.document.all.fraPaneToc.scrolling = "yes";
////////////////////////////////////////////
var eSynchedNode = null;
var eCurrentUL = null;
var eCurrentLI = null;
var bLoading = false;
function loadFrame( bStopLoad )
{
if( "object" == typeof( eCurrentUL ) && eCurrentUL && !bStopLoad )
{
eCurrentUL.innerHTML = hiddenframe.chunk.innerHTML;
eCurrentUL = null;
bLoading = false;
}
else if( "object" == typeof( eCurrentUL ) && eCurrentUL )
{
eCurrentUL.parentElement.children[1].className = "";
eCurrentUL.parentElement.children[0].src = "bs.gif";
eCurrentUL.parentElement.className = "kid";
eCurrentUL.className = "clsHidden";
eCurrentUL.innerHTML="";
eCurrentUL = null;
bLoading = false;
}
else
{
bLoading = false;
}
return;
}
function GetNextUL(eSrc)
{
var eRef = eSrc;
for(var i = eRef.sourceIndex + 1; i < document.all.length; i++)
{
if( "UL" == document.all[ i ].tagName )
{
return document.all[ i ];
}
else if( "LI" == document.all[ i ].tagName )
{
break;
}
}
return false;
}
function MarkSync(eSrc)
{
if("object" == typeof(aNodeTree)) aNodeTree = null;
if("LI" == eSrc.tagName.toUpperCase() && eSrc.children[1] && eSynchedNode != eSrc )
{
UnmarkSync();
eSrc.children[1].style.fontWeight = "bold";
eSynchedNode = eSrc;
}
}
function UnmarkSync()
{
if("object" == typeof(eSynchedNode) && eSynchedNode )
{
eSynchedNode.children[1].style.fontWeight = "normal";
eSynchedNode = null;
}
}
function MarkActive(eLI)
{
if( "object" == typeof( eLI ) && eLI && "LI" == eLI.tagName.toUpperCase() && eLI.children[1] && eLI != eCurrentLI )
{
MarkInActive();
window.eCurrentLI = eLI;
window.eCurrentLI.children[1].className = "clsCurrentLI";
}
}
function MarkInActive()
{
if( "object" == typeof( eCurrentLI ) && eCurrentLI )
{
window.eCurrentLI.children[1].className = "";
window.eCurrentLI = null;
}
}
function LoadChildren( eLink )
{
var strLoc = "loadtree.asp" + eLink.href.substring( eLink.href.indexOf( "?" ) );
document.frames["hiddenframe"].location.replace(strLoc);
}
function Navigate_URL( eSrc )
{
var eLink = eSrc.parentElement.children[1];
urlIdx = eLink.href.indexOf( "URL=" );
if("object" == typeof(framesTop.fraTopic) && eLink && "A" == eLink.tagName && urlIdx != -1 )
{
if(eLink.target=="fraTopic"||eLink.target=="_top"){
framesTop.fraTopic.location.href = eSrc.parentElement.children[1].href.substring( urlIdx + 4 );
}else{
window.open(eSrc.parentElement.children[1].href,eLink.target);
}
MarkSync(eSrc.parentElement);
}
else if("object" == typeof(framesTop.fraTopic) && eLink && "A" == eLink.tagName && eLink.href.indexOf( "tocPath=" ) == -1 && eLink.href.indexOf( "javascript:" ) == -1 )
{
if(eLink.target=="fraTopic")
{
framesTop.fraTopic.location.href = eSrc.parentElement.children[1].href;
}
else if( eLink.target=="_top" )
{
top.location = eLink.href;
return;
}
else
{
window.open(eSrc.parentElement.children[1].href,eLink.target);
}
MarkSync(eSrc.parentElement);
}
else if( eSynchedNode != eSrc.parentElement && ( urlIdx != -1 || ( eLink.href.indexOf( "javascript:" ) == -1 && eLink.href.indexOf( "tocPath=" ) == -1 ) ) )
{
// START D.S.
if(eLink.target=="fraTopic")
{
if (navigator.userAgent.indexOf("Windows") == -1) {
var MyHref = eSrc.parentElement.children[1].href;
do
{
if (MyHref.indexOf("%2E") != -1) MyHref = MyHref.replace("%2E", ".");
else if (MyHref.indexOf("%2F") != -1) MyHref = MyHref.replace("%2F", "/");
else if (MyHref.indexOf("%3F") != -1) MyHref = MyHref.replace("%3F", "?");
else if (MyHref.indexOf("%3D") != -1) MyHref = MyHref.replace("%3D", "=");
else if (MyHref.indexOf("%26") != -1) MyHref = MyHref.replace("%26", "&");
else break;
}
while (true);
parent.fraTopic.location.href = MyHref;
} else {
parent.fraTopic.location.href = eSrc.parentElement.children[1].href;
}
}
// END D.S.
MarkSync( eSrc.parentElement );
}
}
function Image_Click( eSrc , bLeaveOpen )
{
var eLink = eSrc.parentElement.children[1];
if("noHand" != eSrc.className)
{
eLI = eSrc.parentElement;
MarkActive(eLI);
var eUL = GetNextUL(eLI);
if(eUL && "kidShown" == eLI.className)
{
// hide on-page kids
if( !bLeaveOpen )
{
eLI.className = "kid";
eUL.className = "clsHidden";
eSrc.src = "bs.gif";
}
}
else if(eUL && eUL.all.length)
{
// show on-page kids
eLI.className = "kidShown";
eUL.className = "clsShown";
eSrc.src = "bo.gif";
}
else if("kid" == eLI.className)
{
// load off-page kids
if( !bLoading )
{
bLoading = true;
eLI.className = "kidShown";
eUL.className = "clsShown";
window.eCurrentUL = eUL;
eSrc.src = "bo.gif";
eUL.innerHTML = L_LoadingMsg_HTMLText;
LoadChildren( eLink );
}
}
}
}
function Toc_click ()
{
var eSrc = window.event.srcElement;
event.returnValue = false;
if("A" == eSrc.tagName.toUpperCase() && "LI" == eSrc.parentElement.tagName)
{
var eImg = eSrc.parentElement.children[0];
if(eImg) eImg_click(eImg);
}
else if("SPAN" == eSrc.tagName && "LI" == eSrc.parentElement.tagName)
{
var eImg = eSrc.parentElement.children[0];
if(eImg) eImg_click(eImg);
}
else if("IMG" == eSrc.tagName)
{
}
return event.returnValue;
}
function eImg_click(eImg)
{
if("IMG" == eImg.tagName)
{
Image_Click( eImg , false );
Navigate_URL( eImg );
}
}
function Toc_dblclick()
{
return;
}
function window_load()
{
if( self == top ) location.replace( "default.asp" );
var objStyle = null;
if( bc.win32 && ( bc.ie4 || bc.ie5 || bc.ie6 ) && "object" == typeof ( ulRoot ) && "object" == typeof( objStyle = document.styleSheets[0] ) && "object" == typeof( objStyle.addRule ) )
{
window.eSynchedNode = document.all["eSynchedNode"];
objStyle.addRule( "UL.clsHidden" , "display:none" , 0 );
objStyle.addRule( "UL.hdn" , "display:none" , 0 );
//--ulRoot.onclick=Toc_click;
ulRoot.ondblclick=Toc_dblclick;
if( window.eSynchedNode )
{
MarkActive(window.eSynchedNode);
window.eSynchedNode.all.tags( "B" )[0].outerHTML = eSynchedNode.all.tags("B")[0].innerHTML;
window.scrollTo(0,window.eSynchedNode.offsetTop-(document.body.clientHeight/2));
}
else
{
MarkActive(document.all.tags( "LI" )[0]);
}
}
}
window.onload = window_load;
suggestions?

Categories

Resources