Rommel
Rommel

Reputation: 322

Creating a variable based on a loop.

What I want to do is create a variable in Google apps script based on the i in a for loop. Also how can i code a loop to all the variable back. I want to do what i wrote in the code below, but it does not work.

for (var i = 1; i < 5; i ++){
     var pieChart + i = Charts.newPieChart()
        .setDataViewDefinition(Charts.newDataViewDefinition().setColumns([1,3]))
        .build(); 
  }

I then want to call use it kinda like this

for (var i = 1; i < 5; i ++){
        pieChart + i . do stuff with the it 

        }

Upvotes: 0

Views: 136

Answers (2)

BlackHatSamurai
BlackHatSamurai

Reputation: 23493

You would probably be better off to use an array, which would look something like this:

int[] pieChart;
pieChart = new int[5] 
for (var i = 1; i < 5; i ++){
     pieChart[i]= Charts.newPieChart()
    .setDataViewDefinition(Charts.newDataViewDefinition().setColumns([1,3]))
    .build(); 
 }

Upvotes: 0

Pointy
Pointy

Reputation: 413757

What you want is an array.

var pieChart = [];
for (var i = 0; i < 4; ++i)
  pieChart[i] = whatever;

Arrays in JavaScript start with element zero, not 1. (You're free to ignore element zero if you like, but it makes things awkward because the length of the array is figured as the largest populated index + 1.)

Upvotes: 2

Related Questions