2

I'm trying to loop over multiple arrays using a single "for in" or "for each" loop.

I have three arrays namely, name, id , and available. The size of these arrays are equal.

What I need to do is that I need to iterate through each value of the above arrays and based on the index of the row and column (values of i and j, respectively), copy the value of the element to a cell in spreadsheet.

The below is the code I am using:

for (i = 1; i <= copy_range.getNumRows(); i++) {
  for (j = 1; j <= copy_range.getNumColumns(); j++) {
    if (j == 1) {
      var name_cell = copy_range.getCell(i, j);
      // I want to do this however I'm not able to do this since I already have i and j for
      // row and column iteration and that another nested loop makes things complicated
      name_cell.setValue(name[k]);
    }
    else if (j == 2) {
      var id_cell = copy_range.getCell(i, j);
      Logger.log(id_cell.getA1Notation());
      id_cell.setValue(id[k]); //Same idea as in previous if
    }
    else {
      var availability_cell = copy_range.getCell(i, j);
      Logger.log(availability_cell.getA1Notation());
      availability_cell.setValue(available[k]); //Same as if and else if loops previously.
    }
  }   

The reason I'm not able to use indices is that I already use i and j as iterative variables to refer to row and column, and using another nested loop does not give me the intended output - it leads to unwanted iterations and execution time.

Please let me know if there is any way where I can use a "for in" or a similar loop to iterate over each item of all the three arrays.

11
  • could you share your spreadsheet.I'll make a pleasure to help.? Commented Oct 6, 2018 at 15:37
  • 2
    You don't say what k is or how it is related to i and j. Is k simply the same as i or i plus some number? You should also look at best practices developers.google.com/apps-script/guides/support/best-practices, numerous calls to setValue can degrade performance. You seem to be setValue() for every i and j, why not build a 2D array and then use setValues(array). Commented Oct 6, 2018 at 15:42
  • 1
    Do not use for in for arrays. Do not use for each(var... at all. Commented Oct 6, 2018 at 15:49
  • Also I'd like to point out that cell in cell.setValue() is not defined in your loop. Did you mean name_cell, id_cell and availability_cell? Commented Oct 6, 2018 at 16:25
  • Your code is very convoluted at the moment, only because of numerous calls to .setValue. This will slow your script down quickly, as does .getValue or other methods that must access the spreadsheet. Instead of your solution, the best way to move forward is to create some sort of two dimensional array, or even three dimensional if necessary (I don't understand what you're trying to do completely). Since we need to alter a large portion of your code you should create a copy of your spreadsheet, remove any sensitive information, and share an editable copy here that we can look at! Commented Oct 6, 2018 at 18:21

1 Answer 1

3

To me it seems like you have three "column" arrays of N entries and want to write them to a range that is N rows and 3 columns (named copy_range). The best way to do this is to join these 3 arrays into the requisite 2D array that can be written directly in a single call:

const output = name.map(function (nameVal, row) {
  return [nameVal, id[row], availability[row]];
});
copy_range.setValues(output);

The above uses the Array#map method to iterate the name array, and assigns the index associated with each element nameVal to be row. The corresponding element in the id and availability arrays is then accessed using row.

Sign up to request clarification or add additional context in comments.

1 Comment

I thin you got the answer spot on, sorry for not being more descriptive on original question. Basically I have three 1D arrays namely name, id, available that have N elements each. Hence I declared a range N*3 to accommodate the 2D array that contains all the values of the above 3 1D arrays. Once I did I used the setValues to do this in a single stroke. The way I did it was to use two for loops, one for row and the other for the column , I will explore your answer since I'm not yet aware of map. Thanks for your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.