$NaN value in pavlovia even if PsychoPy references source Excel correctly

Description of the problem:


I’m having an issue briefly described as:

  • in PsychoPy I’m referencing an Excel file (for a loop) with number values as shown in the yellow column (yellow only exemplified here):

  • these appear correctly according to the below line of code when run locally on PsychoPy. Account_value is determined before in the code and it’s correct. The problem is with reading the strings from Excel.

 msg = "You managed to win $" + String(win) + "\nTotal amount of money: $" + String(account_value);
  • in Pavlovia, I receive an error:
    “You managed to win $NaN. Total amount of money: $NaN”

I tried to format numbers, delete the online experiment and re-run it.
One thing that I found weird is that sometimes the values are returned correctly, but I’m not sure I’m changing anything to make it so.

Is this a known bug?
I did find some threads on this, but on blank spaces or other, not this particular example where the values are actually written in Excel.

Any ideas?

I think it should be:

 msg = "You managed to win $ " + win + "\nTotal amount of money: $ " + account_value;

I would avoid calling a variable win, since that is used for the main PsychoPy window.

string() has a lower case s, which is the cause of your error.

I first tried:

  1. As tandy suggested: no String() or string() before the variables - the experiment no longer loads in pavlovia, it gets stuck. Looking into the developer tools, I observed that I received an error for one of the variables (none of the rest):
    “Unexpected identifier at line 2077” → another “nono” variable is at that line.

  2. After I tried to write string() instead of String(), the same error appears.

Regardless on what I do now (I deleted and re-created the project 3-4 times). I still receive that error.
The variable was never named “win”, it’s actually named otherwise.
I did not think the name would have an influence, the “win” name was provided as an example.

Please could you show a screenshot of your Auto translate code component?

Did you ensure you were running the latest version of the code.

Did you really doctor your Excel spreadsheet image in order to disguise the variable you are using for win? That seems like a lot of effort.

So the problem was that I had forgotten a “+” sign into the sentence → after I put it, I got rid of the “Unexpected identifier at line 2077” error

Yes, I copied the Excel into another to make it a dummy - I will want to run this and I don’t want anyone to figure out anything to tamper with the results of the experiment.

I now left everything without String() - it did not recognize string() with a small capital letter.
However, I intermittently receive either $NaN or the numbers - much more often the $NaN.

I also checked several times the numbers in Excel, they’re formatted as numbers, no decimal places, copied and pasted them as values there to make sure that they are numbers.

Sorry – I focussed on String being wrong because of the capital. The function is actually str()


I tried using str() and it returned the error:
" * ReferenceError: str is not defined"

I thought that str(0 was for Python code - locally everything works fine.

On Pavlovia and in javascript, I can’t get it to work.
The first iteration almost always works, then as the code advances (loops) through the Excel rows that it’s referenced, I still receive the $NaN / NaN message.

I tried using String() and .toString() and also left it with no string - I cannot get rid of the NaN message.

Is this an issue with the PSychoPy code version?
I am using 2021.2.3 - how do I change it fi this is the error?

Or is it related to the Excel file (I cannot see how exactly…but?)

Thanks in advance, I’m quite stressed to get this to work.

str is Python and should translate to the correct JS in an Auto code component.

It autotranslates to variable.toString() → but this doesn’t work either.

Here, variable has different names, depending on the branch of the if-then structure that I have in place.

if (((alegere === “left”) && (keyDecisionResponse.keys==“left”))) {
if ((prob1 > prob2)) {
a = 1;
account_value = (account_value - loss);
loss = Math.round(loss, 0);
msg = "You lost " + String(loss) + "$ \nTotal amount of money: $ " + String(account_value);
} else {
if ((prob3 > prob4)) {
a = 2;
account_value = Math.round((account_value - halfloss), 0);
loss = Math.round(loss, 0);
account_value = (account_value - halfloss);
msg = "You lost " + String(halfloss) + "$ \nTotal amount of money: $ " + String(account_value);
} else {
a = 3;
account_value = (account_value + nono);
loss = Math.round(nono, 0);
msg = "You win " + String(nono) + "$ \nTotal amount of money: $ " + String(account_value);

All of the bolded values from above are variables referenced in the Excel file referenced in the loop.

Even if I change for example String(loss) or loss.toString() or just loss, I still get NaN on the online experiment.

print(‘loss’,loss) so you can have a look in the console to check the value.

I don’t know where exactly to put print(‘loss’,loss). I placed it right before the if structure and the browser stopped as if wanting to print on a printer.

Regardless, I think I found the source of the problem, but I still don’t know how to fix it.
I believe the problem comes from the fact that the online JS/Pavlovia code cannot read the variables from the Excel file - not all of them at least. The first iteration is always read. But after the first one, I always receive NaN.

For example, when I comment the line loss=Math.round(loss,0), I receive “undefined” for loss.
But that’s weird because loss is defined in the JS code using “var loss” and in the Excel file.

Looking into other questions on the forum, I saw that this may stem from the trialList component of the js code:

Here is the piece of my code:

How do I check that the Excel file is correctly loaded on the online platform?

This is a problem. If you have a variable defined in a spreadsheet, don’t also define it in a code component.

If you are coding in JSPsych from scratch then I’m afriad I have no idea what might or might not work. All of my advice is based on using Builder to create the JS code, which is why I give Python code (like print) not JS code (like console.log) in my posts.

I wrote the code in python.
I did not define the variable using the var declaration (e.g var loss).
They are defined automatically from Python to JS using PsychoPy (Auto-JS).

I had to modify one thing only in JS and that’s related to randomly choosing a variable.
Instead of importing * as random in JS (which resulted in an error for unexpected token “*”), I had to use:

var index = Math.floor(Math.random() * alegeri.length);

I’ve since been trying to not get NaN.

If I remove the “round(variable,0)” code lines from Python, do the Auto-JS code translation and introduce the randomization as above, the variables are still not read correctly in JS.

The problem is that Pavlovia is not reading my Excel.
Any idea why?


The excel is in a loop that should be read 10 times, each row of that Excel being like an iteration:


Where should I look to make sure that all variables defined in the Excel are correctly read in Pavlovia?

What variable does your error message refer to?

What Python code (auto translated) refers to that variable?

What does the loop look like that points to that spreadsheet?

Try print(‘nono’,nono) in Begin Routine in the first routine inside the loop.

To answer the questions:
What variable does your error message refer to?

To any of: loss, noyes, yesno, yesyes, halfloss - they are defined in the Excel file (screenshot above)

What Python code (auto translated) refers to that variable?

if((alegere==‘left’) and (‘left’ in keyDecisionResponse.keys)):
if (prob1 > prob2):
account_value = (account_value - loss)
msg = ‘You lost $’ + (str(loss)) + ‘\nTotal amount of money: $’ + (str(account_value))
elif (prob3 > prob4):
account_value = (account_value - halfloss)
msg = ‘You lost $’ + (str(halfloss)) + ‘\nTotal amount of money: $’ + (str(account_value))
account_value = (account_value + nono)
msg = ‘You win $’ + (str(nono)) + ‘\nTotal amount of money: $’ + (str(account_value))
#account_value += account_value

What does the loop look like that points to that spreadsheet?


Here, nReps=1 because I only want the loop to go once through the Excel (equivalent to 10 times as the number of rows from the Excel).

Try print(‘nono’,nono) in Begin Routine in the first routine inside the loop.

Nothing happens.

The variables (from question 1) are not declared by me in JS, they are declared automatically.

  • If I delete them, I receive the undefined error
  • If I specify Math.round(variable,0) - for example Math.round(loss,0), I get NaN.

It all boils down to how pavlovia does (not) recognize the variables from Excel online, unfortunately.
Still cannot find a solution.

This is your issue. In Pavlovia you can’t edit spreadsheet variables. Try

loss2 = round(loss,2)

and then use loss2 in your msg.

Wakecarter, thanks for your patience on this.

I defined all variables as you mentioned i.e. loss2=round(loss,0), nono2=round(nono,0) etc.
However, the problem is still there.

I still receive the NaN message → because the initial values (i.e. loss, nono etc.) are not correctly (or at all?) read from the Excel file.

This is the code section where the Excel file is read. Is it wrong? Am I missing something? All cells are defined as numbers in Excel (tried defining them as General or Accounting , but still nothing).

Should trialList: “excel1.xlsx” be written otherwise?

I saw in a post from you that you defined it differently:

And your code was defined as:

var loading_loop;
var currentLoop;
function loading_loopLoopBegin(loading_loopLoopScheduler) {
// set up handler to look after randomisation of conditions etc
loading_loop = new TrialHandler({
psychoJS: psychoJS,
nReps: 3, method: TrialHandler.Method.RANDOM,
extraInfo: expInfo, originPath: undefined,
trialList: TrialHandler.importConditions(psychoJS.serverManager, ‘stimuli.xlsx’, ‘0:10’),
seed: undefined, name: ‘loading_loop’
psychoJS.experiment.addLoop(loading_loop); // add the loop to the experiment
currentLoop = loading_loop; // we’re now the current loop

I tried doing that, but I received errors compiling.

Are you trying to define a loop in code or using a loop Builder component?

The problem was indeed defining the same variable, I had to delete it from the JS everywhere where I had it declared as var loss, var nono etc.

There were some that had escaped me and they produced errors.

Thanks for the help