# How to call variable from Excel file in code component?

**URL:** <https://discourse.psychopy.org/t/how-to-call-variable-from-excel-file-in-code-component/13418>\
**Category:** Online experiments\
**Created:** [May 17, 2020, 12:56am UTC](https://discourse.psychopy.org/t/how-to-call-variable-from-excel-file-in-code-component/13418 "2020-05-17T00:56:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![psych](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@psych](https://discourse.psychopy.org/u/psych)\
**Post date:** [May 17, 2020, 12:56am UTC](https://discourse.psychopy.org/t/how-to-call-variable-from-excel-file-in-code-component/13418/1 "2020-05-17T00:56:43Z")

</div>

I have a variable (“opacity”) in my conditions Excel file I want to call in a JavaScript code component - e.g. if (opacity === 1): do this. However, it seems PsychoPy isn’t able to access this variable in a code component. What can I do to make this work?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [May 17, 2020, 7:00am UTC](https://discourse.psychopy.org/t/how-to-call-variable-from-excel-file-in-code-component/13418/2 "2020-05-17T07:00:52Z")

</div>

Try changing the name of the variable, just in case opacity is already being used by PsychoPy

The other possibility is that it is coming in as a string rather than a digit.

Best wishes,

Wakefield

---

<div class="post-metadata">

**Author:** ![psych](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@psych](https://discourse.psychopy.org/u/psych)\
**Post date:** [May 17, 2020, 5:18pm UTC](https://discourse.psychopy.org/t/how-to-call-variable-from-excel-file-in-code-component/13418/3 "2020-05-17T17:18:15Z")

</div>

I’ve changed the name to a unique variable, and the values under it are just 1s and 0s. What would be the standard way of calling a variable from an Excel file? I tried doing this, but it didn’t work:

```auto
conds = new TrialHandler({
        psychoJS: psychoJS,
        nReps: 1, method: TrialHandler.Method.SEQUENTIAL,
        extraInfo: expInfo, originPath: undefined,
        trialList: 'counterbalancing2.xlsx',
        seed: undefined, name: 'conds'
    });

condList = conds.getTrialList()

```

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [May 17, 2020, 5:21pm UTC](https://discourse.psychopy.org/t/how-to-call-variable-from-excel-file-in-code-component/13418/4 "2020-05-17T17:21:43Z")

</div>

Personally I’ve only ever used PsychoPy Builder loops to access Excel files so I’m afraid I can’t help.

Are you getting an error message (either from Pavlovia or a console) you can give us?

---

<div class="post-metadata">

**Author:** ![psych](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@psych](https://discourse.psychopy.org/u/psych)\
**Post date:** [May 17, 2020, 6:01pm UTC](https://discourse.psychopy.org/t/how-to-call-variable-from-excel-file-in-code-component/13418/5 "2020-05-17T18:01:15Z")

</div>

So basically the code I have is what I posted above, and to call the variable I have this:

```auto
if (condList[0]['mem_opacity']){
  foil = num[4];
}

```

The error I get on Pavlovia when I try to pilot:

 ![Capture](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/2/3/23bba8770810a117bcf8446c2e03452c400e4e79.png)

---

<div class="post-metadata">

**Author:** ![hbng26](https://avatars.discourse-cdn.com/v4/letter/h/4af34b/32.png) [@hbng26](https://discourse.psychopy.org/u/hbng26)\
**Post date:** [May 18, 2020, 6:44am UTC](https://discourse.psychopy.org/t/how-to-call-variable-from-excel-file-in-code-component/13418/6 "2020-05-18T06:44:21Z")

</div>

Hi,  
I use this in the code component to call variable from the Excel file:

```auto
thisTrial['variablename'] == 'condition'

```

In your case, it should be:

```auto
if thisTrial['opacity'] == 1:
    do this

```

If it’s not working, maybe try change 1 to string in the Excel file and try:

```auto
if thisTrial['opacity'] == '1':
    do this

```
