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.
Related
I have a file called 'test.abcde.houses.csv' and I want to extract the substring 'abcde' which I will use in my next processor group to query the database.
Currently, I am using the updateAttribute Processor group to try to extract the substring.
This is the code I am using in the value section.
var userPattern = java.util.regex.Pattern.compile('(.+?)\.[0-9]{8}-[0-9]{7,9}\..+');
var userMatcher = userPattern.matcher(fileName);
var matchExists = userMatcher.matches();
var user;
var userRemove;
if (matchExists) {
user = userMatcher.group(1);
userRemove = user + ".";
}
else {
throw 'Unable to parse username from file metadata.';
}
Question:
Is this the right way to extract a substring from a flow file name in NIFI?
Am I using the right processor group?
Does this code work with Nifi?
You need to use NiFi Expression Language, a kind of NiFi's own scripting feature which provides the ability to reference attributes, compare them to other values, and manipulate their values. Please refer to this official documentation.
UpdateAttribute processor is used to update/derive new/delete attributes. So you need to use the Expression Language inside UpdateAttribute to manipulate attributes.
Example:
test.abcde.houses.csv - this is your filename and if you want to extract abcde string from filename then you can use getDelimitedField function (Expression Language string function) like below. If the expression did not evaluate, then user attribute will be having empty/null value.
Property: user (if already present then update/assign value, otherwise create new attribute)
Value: ${filename:getDelimitedField(2, '.')} (abcde is at second index/position in filename attribute value)
Expression Language has Boolean, Conditional, String Manipulation, etc. functions, so you can easily replicate your JS logic into UpdateAttribute to derive desired attribute value.
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.
I've ran into a problem - I need to add a custom stamp (type of annotation) to a number of .pdf files. I can do it through Actions for Acrobat X Pro, but my clients do not have that license and they still need to do it. The list of files is stored in Excel spreadsheet, so ideally I am looking for a VBA solution. I have came up with the following code :
Option Explicit
Sub code1()
Dim app As Acrobat.AcroApp
Dim pdDoc As Acrobat.CAcroPDDoc
Dim page As Acrobat.CAcroPDPage
Dim recter(3) As Integer 'Array defining the rectangle of the stamp - in real code wil be calculated, simplified for ease of reading
Dim jso As Object
Dim annot As Object
Dim props As Object
Set pdDoc = Nothing
Set app = CreateObject("AcroExch.App")
Set pdDoc = CreateObject("AcroExch.PDDoc")
recter(0) = 100
recter(1) = 100
recter(2) = 350
recter(3) = 350
pdDoc.Open ("C:\Users\maxim_s\Desktop\Code_1\test1.pdf")
Set jso = pdDoc.GetJSObject
If Not jso Is Nothing Then
Set page = pdDoc.AcquirePage(0)
Set annot = jso.AddAnnot
Set props = annot.getprops
props.page = 0
props.Type = "Stamp"
props.AP = "#eIXuM60ZXCv0sI-vxFqvlD" 'this line throws an error. The string is correct name of the stamp I want to add
props.rect = recter
annot.setProps props
If pdDoc.Save(PDSaveFull, "C:\Users\maxim_s\Desktop\Code_1\test123.pdf") = False Then
MsgBox "fail"
pdDoc.Close
Else
MsgBox "success"
pdDoc.Close
End If
End If
End Sub
The problem is with the setprops and getprops procedures - it seems that at the moment when annotation is created (jso.AddAnnot) it does not posses the AP property, which is the name of the stamp I want to add. If I set the property Type= "Stamp" first and then try to specify the AP, the result is that one of the default stamps is added and it's AP is renamed to my custom stamps' AP. Also note, that if I launch acrobat and use the code below, the proper stamp is added:
this.addAnnot({page:0,type:"Stamp",rect:[100,100,350,350],AP:"#eIXuM60ZXCv0sI-vxFqvlD"})
If there is a way to execute this Javascript from VBA inside of the PDDoc object, that will solve the problem, but so far I have failed.
You can use "ExecuteThisJavaScript" from the AForm Api. Short example:
Set AForm = CreateObject("AFormAut.App")
AForm.Fields.ExecuteThisJavaScript "var x = this.numPages; app.alert(x);"
It has the advantage that you don't need to translate the js examples into jso code. If you search for ExecuteThisJavaScript you will get some more and longer examples.
Good luck, reinhard
In...
props.Type = "Stamp"
The type should be lower case. But if the pure JavaScript is working from the console, you might consider just executing the string using the jso.
I have a DIV that is fed by a server side script that I don't have access too, and it outputs the value in £'s.
HTML Example:
<div id="totalExpenditure">£1,125</div>
I then want to have a jQuery script take that figure and workout the difference between a set value of £2,000 and result it to another DIV.
Which says: <div id="totalSurplus">You have £725 remaining.</div>
I've searched Google for mathmatic jQuery but the results look far too complex. What I'm not sure is even possible is to convert the output of the ID totalExpenditure into the DOM to be manipulated.
1) get the string: var myVal = $('#totalExpenditure').text()
2) Get rid of the non-numeric pound sign: myVal = myVal.replace('£','') and the comma myVal = myVal.replace(',','')
3) turn it into an number: myVal = parseFloat(myVal)
4) Perform any math you want with myVal.
You can do this all in one step, but this gives you an idea of how the language works.
You've got two issues here.
First you need to parse a string and convert it to a number.
Then you need to perform the calculation.
Neither of these are really jquery specific. JQuery can help with getting the string, and writing the output, but the rest is just pure javascript.
var num = parseFloat($("#totalExpenditure").text().replace("£", ""));
var remain = 2000 - num;
var outputStr = "You have £" + remain.toFixed(2) + " remaining";
$("#totalSurplus").text(outputStr);
For more control over the output of the currency perhaps check out this post: How can I format numbers as money in JavaScript?
You are able to feed the value (£1,125) from the server to the client's JavaScript engine the same way you're feeding HTML to the client.
It is really not recommended to read a DOM element for a text node and interpret said node as a value for mathematical operations. You should have a JavaScript variable aside to calculate this for you.
obtain the totalExpenditure div content and set totalExpenditure var value (using a regex):
var content = $('#totalExpenditure').text();
var pattern = /[0-9\.,]/g;
var totalExpenditure = pattern.exec(content);
subtract
var totalImport = 2000;
var result = totalImport - totalExpenditure;
I have returned object from signature device and when i make quick watch on it, it told me that its an array of long and when i pass it to web method in my code behind (vb.net) it gives me nothing.
i need some help..
note: i'm using an activeX to capture the signature from the device.
this is javascript code :
function OnSave() {
var sign = document.FORM1.SigPlus1.SignatureString;
PageMethods.Save(sign);
}
this is my webmethod:
<WebMethod()> _
Public Shared Function Save(ByVal obj As Object) As String
Dim obj1 As New PFSIGNATURELib.SigniShellSignature
obj1.SignatureMime = obj
obj1.SaveBitmapToFile(System.AppDomain.CurrentDomain.BaseDirectory() & "\sign1.bmp", 200, 200)
Return "\sign1.bmp"
End Function
I don't know much about ASP.Net, but it seems like the PageMethods.Save function can't handle an array of long. Another possibility is that the sign variable is null in the javascript code.
Try adding
alert(sign);
in the middle your Javascript function, or better yet, install firebug and do
console.log(sign);
instead. This way you'll make sure the sign var actually contains what you think it does.
If it indeed contains an array of numbers (javascript doesn't have a long type), maybe you need to convert it to something else before calling the PageMethods.Save function.
For example, this javascript snippet will convert sign into a space-separated string of numbers:
s = ""
for (i in sign) {
s += sign[i] + " ";
}
sign = s
If you manage to pass this string to your webmethod, you can use some string parsing to get back the original array.