Custom function substring Apps Script - javascript

I'm trying to integrate a substring method in a custom function, but I'm getting a error. My function is something simple like :
Var num = "1234567"
Var num2 = Num.slice(0,2)
I can't make it run inside a custom function that I want to use as a formula.

Google Apps Script /JavasScript are case sensitive.
Use var instead of Var
Your code declares a variable as num no Num.

Related

How to use wp_localize_script() variable in js as a variable? (not as string)

I've added a field to our wordpress backend where I can write text into it.
(for example "colorVariation + btnVariation) -> That should define a specifc order for the js variables later
I'm able to receive this text in my js file with the wp_localize_script function.
wp_localize_script('wc-product-js', 'script_vars', array(
'order' => get_field("wc_variation_order"),
)
);
It seems to be, that this variable get's converted into a string when I try to use the variable in my js file. like that:
var colorVariation = '_red';
var btnVariation = '_male';
var order = script_vars.order;
var varId = '.variation' + order;
My expected output would be ".variation_red_male" but the output is ".variationcolorVariation + btnVariation)
Is there any way to convert this string?
Alright. Finally I've found the function by myself. It's eval().
The eval() function is able to evaluate or executes an argument that
is passed inside it. If the argument is an expression, this function
will evaluate the expression and the argument is one or more
JavaScript statements, eval() executes the statements.
Quote from https://www.codespeedy.com/convert-string-into-variable-name-in-javascript/
var colorVariation = '_red';
var btnVariation = '_male';
var order = script_vars.order;
var varId = '.variation' + eval(order);

How do I create a custom javascript variable that selects part of an already existing javascript variable?

I am trying to create a custom javascript variable in GTM that returns part of a javascript variable that already exists.
Variable that already exists: window.ShopifyAnalytics.meta.product.variants.0.name
returns this: "Bamboo Basic String - Schwarz - S"
However I want to code a custom javascript variable to just return the Schwarz part, is this possible? If so what is the code that I would need?
Please can someone let me know what code to put into GTM to create this variable?
TIA
If all names are pretty much the same you could use split to get that part of string and then remove whitespaces. It would look like this:
window.ShopifyAnalytics.meta.product.variants.0.name.split('-')[1].replace(/
/g,'');
If the already existing variable is always structured the same way you could do something like this:
let variable = window.ShopifyAnalytics.meta.product.variants.0.name.split('-')
Then by calling varaible[1] you get the 'Schwartz' part of the variable.
If you want a return value you can use a function like the following and call it wherever you want.
Simply make sure to pass the correct argument content
// Declaring a function getColor that returns the second element in the list,
// trimmed (without spaces before and after)
const getColor = (content) => {
return content.split('-')[1].trim();
}
const test = "Bamboo Basic String - Schwarz - S";
console.log(getColor(test));
//console.log(getColor(window.ShopifyAnalytics.meta.product.variants.0.name));
You could split the string on the hypens (-) like this:
const productName = window.ShopifyAnalytics.meta.product.variants.0.name;
const part = productName.split(' - ')[1];
Assuming you have a consistent format, and you always want the second part after that hyphen.
split will separate parts of a string into an array where it finds a match for the argument. The first index [0] will be the product name, the second [1] will be the part you're looking for.
This could cause issues if you have a product name with a - in it too though so use with care!
If it needs to be an anonymous function for GTM, you could try the following (though I'm not a GTM expert):
function () {
const productName = window.ShopifyAnalytics.meta.product.variants.0.name;
return productName.split(' - ')[1] || 'Unknown';
}

Apache JMeter -How do I get variable value in __javaScript function

How do I get variable value in __javaScript function ?
My code:
String[] zoollevelParams = Parameters.split(",");
Random random = new Random();
int zoomValue= Integer.parseInt(zoollevelParams[random.nextInt(10)]);
double lat = 50.669;
double lng = 5.499;
int numTiles = ${__javaScript(Math.pow(2\, "${zoomValue}"))};
This code is unbale to get value of zoomValue in __javaScript function call, how do I get value in this function?
You can't combine Java and Javascript code and don't need to.
Just keep using Java and take parameter from JMeter variables object vars:
int numTiles = Math.pow(2, vars.get( "zoomValue"));
Note: If you save zoomValue as Double or not a regular String use vars.getObject
Don't inline JMeter Variables or Functions into scripts, particular in your case __javaScript function is being executed before zoomValue is getting initialized.
Make sure you use the relevant JSR223 Test Element
Make sure you select groovy from the "Language" dropdown
Make sure you have Cache compiled script if available box ticked
Amend the last line of your code to look like:
int numTiles = Math.pow(2, zoomValue)
Demo:
Check out Apache Groovy - Why and How You Should Use It article for more details on using Groovy for scripting in JMeter tests.

When try to pass date the slashes occur division

I have a variable var that contains a date "29/5/2017" in my page. Now i am trying to pass this variable to a function that is stored in a separate js file. My issue is when i debug the js i see a number 0,0028... The js believes that this is a number a makes a division. how can i prevent this and pass just the date?
the var in my aspx file is :
var mydates = '29/5/2017';
the function call in my aspx file
calldate(mydates);
the function in the js file
function calldate(mydates) {
alert(mydates);
}
you need to use double quotations to single quotations mark during init variable
var mydates = "29/5/2017".toSting();
and user .toString() function for canvart to string.
Thanks
Instead of passing date as string try to pass variable as Date datatype.
there might be chances that the variable getting changed before calling the function.
<pre>
var mydates = Date("29/5/2017");
</pre>

Javascript passing elementid in variable

I need to pass a variable to a javascript function which will then perform calculations and return the answer to another edit box on a form. I need to pass as I have 10 lines of edit boxes and dont want to have 10 seperate javascript functions.
function calc_totalcost(line)
{
$line_qty=line+"_qty";
$line_totcost=line+"_totcost";
$line_unitcost=line+"_unitcost";
$totcost=$line_qty.value*$line_unitcost.value;
document.getElementById('$line_totcost').value = $totcost;
}
on the html:
onchange="calc_totalcost('L1')"
So, on editbox 1 for L1_edit1 I need to send L1 to the function, which will then convert to 'L1_qty' which is an editbox (input) name where it will perform calculations using its contents. Hope that makes sense?
Thanks
You have a few issues, including the last line in the function which does not need the document.getElementById whereas all the others do need it.
function calc_totalcost(line) {
var $line_qty = document.getElementById(line+"_qty");
var $line_totcost = document.getElementById(line+"_totcost");
var $line_unitcost= document.getElementById(line+"_unitcost");
var $totcost=$line_qty.value*$line_unitcost.value;
$line_totcost.value = $totcost;
}

Categories

Resources