In Geogebra let's define a JavaScript global function from a button (property):
function ggbOnInit() {
}
function test(par) {
return par * math.random();
}
How do I call that function from a Geogebra script attached to that button ("on click" property)?
The following Geogebra script:
Sequence[test(3),k,1,5]
launches an error message " unknown command test", whereas a sequence of five random numbers between 0 and 3 were waited.
In order to call your Global JavaScript function your "On click" script must be of type JavaScript and not GeoGebra Script.
I found this possible solution.
Create a button and in the "On Click" section, with "JavaScript" selected at the bottom, insert the code:
ggbApplet.evalCommand("list={0}");
if (! ggbApplet.exists("par")) {ggbApplet.evalCommand("par=200")};
for(var i =1;i<5;i++)
ggbApplet.evalCommand("SetValue[list," +i+", par* random()]");
The par parameter can be set as a Geogebra variable or inside the code (that last value will be used in case the variable doesn't exist).
Anyway I couldn't find how to use a function in the Global Javascript tab or how to use the Javascript math.random() command.
The problem is that the JavaScript commands available inside Geogebra are probably a limited set of the JavaScript available outside it, but I'm not sure about that.
I'm not sure whether you want to get five random integers or decimal numbers. If you want to get five random integers between 0 and 3 you can easily do it with GeoGebra scripting.
You can first create an empty list writing for example list={} into Input Bar and then fill it with a button or create it dynamically with a button.
Create a button, open Object Properties, go to Scripting tab and inside On Click tab write next line:
list=Sequence[RandomBetween[0,3],k,1,5]
Be sure that you've chosen "GeoGebra Script" in drop-down menu.
IF YOU WANT FIVE RANDOM DECIMAL NUMBERS between 0 and 3 do the same but with little different code line:
list1=Sequence[random()+RandomBetween[0,2],k,1,5]
Hope this helped...
Related
This question already has answers here:
Set anonymous/dynamic functions to Menu
(3 answers)
Increment Cell value by one by clicking
(3 answers)
button click is only working on Windows & not working on Android mobile sheet
(2 answers)
Closed 2 years ago.
I need to do the same operations on different cells and I am trying to write a generic script. I give a trivial example that explains my problem below.
Let's say that I want to click on a button and decrease the value of the cell A1 by 1. That's straightforward. I define the following function
function dec_cell_def(){
var cell = fr = SpreadsheetApp.getActiveSheet().getRange("A1")
cell.setValue(cell.getValue() - 1)
}
Then assign the "script" dec_cell_def to any button and I am done.
Let's say now, that I want to do this for many different cells. I could of course write a function for each one, but this is hardly a solution. So I thought the following would work. I define the function cell_dec that take as an argument the label of a cell and returns a function that decreases the value of the cell:
function dec_cell(label){
() => {
var cell = fr = SpreadsheetApp.getActiveSheet().getRange(label)
cell.setValue(cell.getValue() - 1)
}
}
However when I assign the "script" dec_cell("A1") to a button, I am told that the script does not exist. What is the proper way to do that?
PS. I know that I can get the selected cell and act on it, but I would like to have a fixed target for my function.
Edit: I meant that I want to do this for few cells but not all at once. I would like to create a button for each cell and I would like to avoid copy-pasting the same code. I think that none of the suggested questions answer that.
I am trying to automate a simple action with python and selenium (with safari) :
I have a webpage with a list on top of it going from "A" to "Z", each letter activate a Javascript on the page (at least I guess, i don't know really well Javascript and HTML) when you click on it, making a table below the letter list to reload and output a list of stocks ("A" give you the stocks starting with letter A, "B" all the stocks starting with letter B, etc...)
My goal is to click on each button, list all the stocks of the table, store the list in Python and then go to next letter.
So I created a method that clicks on the letter passed as an argument, but I want python to wait for the table to 'load' before allowing it to go to the next line of my code because for now it is creating problems as the script goes on the next letter before the table is loaded.
The command I use to click on the buttons is :
driver.execute_script("arguments[0].click();",driver.find_element_by_xpath(f"//li[text()='{Stock_Letter.upper()}']"))
where driver is my selenium webdriver, and Stock_Letter is the method's argument containing the element i want to click
To loop all the letters I use :
alphaB = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
for lettre in alphaB:
Session.GoToGamePage(page='buysell', Stock_Letter=lettre.upper())
(Session is my Object, and GoToGamePage is the method I use to click the expected letter, it contains other stuffs game related)
I tried to use the wait.until method of selenium but the problem is that the first element of the table has the same HTML attributes no matter the letter, so unless it exists a selenium expected condition to wait for special text, I don't know how to do it ? Or even an expected condition to wait for the first letter of an attribute to be some value, but I can't find a complete documentation on expected conditions ??
My first guess would be to code the waiting in the execute_script command but I really don't know how to do it ?
If you need any further information on the webpage itself let me know, here is the first table row data :
<td class="allf">EDENRED</td>
The webpage concerned is this one : https://school.abcbourse.com/skemafinance/tab_market.aspx
but you need to create an account (really quick) on https://school.abcbourse.com/skemafinance and use the code 195 (feel free to participate in the trading contest if you want to !)
Might be useful to someone- python selenium
in this example I am using SweetAlert2 (https://sweetalert2.github.io/#usage) to pop up an alert and then use WebDriverWait wait to know when I close the alert and continue on with my code:
result = df.to_html(classes='table table-striped table-hover', header = "true", justify = "center")
result = result.replace('\n','').replace('<thead>', '<thead class="thead-dark">')
driver.execute_script(
'''
Swal.fire({
title: '<strong><u>Item Found</u></strong>',
html:'%s',
showCloseButton: true,
showCancelButton: true,
imageHeight: 1000,
focusConfirm: false,
})
''' % result)
while True:
try:
element_present = EC.presence_of_element_located((By.XPATH,'//div[#class="swal2-popup swal2-modal swal2-show"]'))
WebDriverWait(driver, 1).until(element_present)
except:
break
To answer your first question -- no, execute_script does not do any waiting. It just runs some Javascript on the page, and that's it. You mentioned that the first element in your table has the same HTML attributes as all the others, so you can't just wait on that element. That makes sense.
You could try waiting on the presence of ALL elements located by your selector and see if that helps.
WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.XPATH, "//li[text()='{Stock_Letter.upper()}']")))
You can also use text_to_be_present_in_element to wait on element.Text to equal something, or you can wait on text_to_be_present_in_element_value to wait on an element's value attribute to equal something, depending on what the HTML looks like.
This documentation may help:
https://selenium-python.readthedocs.io/waits.html
I'm have a simple script to turn off the Source.Text when the counter reaches 0
rate = -1;
clockStart = 10;
clockTime = Math.max((clockStart-1*(time-inPoint)),0);
if (clockTime ==0)
{
thisComp.layer("Counter").transform.opacity.setValue(0);
clockTime
}
Counter is the layer whose sourcetext I need to put the Opacity as 0. The script is functioning fine. But in the comp window it displays an error: The project contains an expression error. The line it mentions is pointing to the following code:
thisComp.layer("Counter").transform.opacity.setValue(0);
Whats wrong with this line? It's effective despite the error.
You're trying to use extendscript for an expression. The two are different things. If you want to run this as a script you have to run it through the file>script> menu, not in the expression editor of the property.
The expressions language doesn't have the setValue() function. The expression just has to return a value and that will be the value of the property the expression is applied to. If you want to change the value of another property you have to apply another expression to it. Or you can set the value with a script, which acts just like you set it yourself with the gui.
I am a new student understanding javascript. I am currently having a difficult time understanding a simple concept on storing a variable on my calculator side project. My problem is when a operator (+,-,/,*) is clicked, I want to store the first value as a variable (first user input). Then after the user clicks on the digits again, the display screen will clear and display the second variable (or the second user input). Then if the operator button or equals button is pressed it will calculate the two variables (so var + var2). I have used a global variable for variable1, but I am having trouble assigning the second variable with the user input after clearing the item. I have a feeling there is a simple answer to this question, but I want to know what I am fundamentally doing wrong so I can start reviewing all the topics I need to do cover again. Anyways any help will be great! Thanks
'http://codepen.io/kevk87/pen/EVoEaa`
First thing, you have a syntax error in your statement that is causing jQuery to not select the right element. I have fixed that in my snippet below.
Second, you need to implement the equal operator to put all of this together. Meanwhile, to address your problem and point you in the right direction, You aren't really capturing the second element. Everytime the user clicks on an operator, you are reassigning the value that is on the screen to 'number' variable, then clearing the screen and assigning the value that is on the screen(which is clear because you just cleared it) to 'number2'.
One way to get around this is to check and see if the 'number' variable has a value, if so then you assign the next value to the 'number2' variable. Here's a snippet of code that does this.
$(".operator").on("click",function() {
if (number == null) {
number = $("#screen").html();
}
else {
number2 = $("#screen").html();
$("#screen").html("");
}
});
You forgot the # selector on the line $("screen").html("");, jQuery won't select the correct element. Also you haven't implemented the equal operator yet.
$(".operator").on("click",function() {
number = $("#screen").html();
$("screen").html("");
number2 = $("#screen").html();
});
I am completely confused here. So I am looking for a solution for the following problem:
I want to trigger some function(for now an alert box) using jQuery on an input field. Conditions are:
Input field always maintains the focus.
Input is fed from a USB device, which acts just like a keyboard input. So for 10 characters, there will be 10 keydown and keyup events.
Once input is filled with 10 characters, respective alert box should pop out.
Now the problem I am facing, how do I find out that input fed in is not equal to 10 characters, so throw an error alert box.(lets say just 5 chars came in input, how do I figure out the final count is 5, because there will be 5 keyup events)
You could show a message underneath/beside the input box instead of popping an alert box.
E.g. on every keyup event, check the string length, and if it's not 10, show that message.
If you really, really have to resort to alert box, you could do a timeout check, e.g. only perform the validation after 1000ms of key event inactivity. This could get very annoying on the user though.
You really have two problems here. One is just understanding the jQuery syntax (see the second part to my answer), and the other is - what is the best way to understand WHEN to throw up an error box.
To answer the second question first, my recommendation would be to not use an alert box to warn the user as they tend to be modal and really interrupt the flow of input. Secondly, as you said - how do you know when the person has stopped "typing." Unless you use some sort of timing mechanism (which is more trouble than it's worth), you don't. My suggestion would be to utilize a "div" within your HTML that shows there is an error UNTIL you reach 10 characters. Once that happens, you can hide the div. (And, of course, the div can be styled to look pretty in the meantime.)
So...how to do this...
Let's assuming your input field has an id of "myField." If you are using jQuery (which is in your tags), you would do something like this.
$(function() {
var keypresses = 0;
$('#myField').keyUp(function () {
keypresses++;
if(keypresses == 10) {
$('#error').hide(); // This is your div error with some error text in it.
// Do other stuff.
} else {
// Display an error.
}
});
Alternatively, if you don't want to use the keypresses variable, you can also use..
if($(this).val().length == 10) { }
The real issue is the fact that you are measuring in key press events, because not all key presses (even when the field has focus) will insert a character into field (for example returnesc). Therefore, you will need to measure the string length in order to validate the code before you start executing functions.
In actuality you don't even need jQuery to accomplish what you need, just bind the function call to a key press event, and only execute the function call if yourstring.length = 10
yourInput.onKeyPress(yourString.length = 10 && yourFunction());
Try -
$('#idofinputfield').keyUp(function () {
var length = $('#idofinputfield').val().length;
if(length <= 10){
alert("less than 10");
}else{
alert("greaterthan 10");
}
});