2

I have a sheet like this:

enter image description here

And I have this function:

    function getRangeAsArrays(sheet) {
  var dataRange = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());
  var data = dataRange.getValues();
  var array = [];  
  for (var r=0; r<sheet.getLastColumn(); r++) {
  for (i in data) {
    var row = data[i];
    array.push(row);
       }
    }
  return array;
}

Which I use to build a listboxthis way:

   var recipientArray = getRangeAsArrays(activeSheet);
  var item3Panel = app.createHorizontalPanel();
  item3Panel.add(app.createLabel("recipient"));
  var listBox = app.createListBox().setName('item3');  
  for(var i = 0; i < (recipientArray.length); i++){
  Logger.log("recipientArray[i][2] = " + recipientArray[i][3]);
  Logger.log(" i = " + i);
      listBox.addItem(recipientArray[i][4]);
    }
  item3Panel.add(listBox);

But when I iterate over the array length (4 rows), I got this (unexpected to me) result and the logs shows i variable goes until 14:

enter image description here

Since recipientArray.length should give me the first dimension of the 2 dimensional array and recipientArray[i].length the second dimension, and since I want the first dimension (number of rows) row to fix that? What is going wrong here?

4
  • not sure I understand exactly what you need... what data should be in each listItem ? Commented Mar 27, 2014 at 6:30
  • Hi, @Sergeinsas, each row is been repeated 3 times in the listbox. I need each row to generate one (and only one) item list for that row. Thanks! Commented Mar 27, 2014 at 14:32
  • @Sergeinsas, look what is on my table: pho.to/551NP =)) I'll start reading it this weekend Commented Mar 27, 2014 at 15:21
  • 1
    :-) excellent!!! Thank you! If ever you find something unclear you know where to find me :-) Commented Mar 27, 2014 at 15:58

1 Answer 1

2

Even if I'm still unsure that I understood what you need (I guess I'm a bit tired or I become stupid... go figure...:), I wonder why you try using separate functions since the value returned by range.getValues() is already an array... A 2D array but still an array.

If you want to create one listBox per row and add the following cells as items then a double loop like this will do the job. (tell me if I'm completely off the subject, thx).

I wrote an example code with the main structure and comments to explain where things go.

function test() {
  var dataRange = SpreadsheetApp.getActiveSheet().getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());
  var data = dataRange.getValues();
  Logger.log(data)
  var array = [];  // this is useless
  for (var r=0; r<data.length; r++) {

    // create listBox widget here
    //var listBox = app.createListBox().setName('listBox'+r);   
    for (i in data[0]) {
      var cell = data[r][i];

      //add items to listBox here
      // listBox.addItem(cell);
      array.push(cell);// this is useless
    }
  }
  Logger.log(array);//useless
  return array;//useless
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, @Sergeinsas!! You are not tired or stupid, I didn't realize that the range already give me a 2 dimensional array! So, I was puting more dimensions on it! Thanks A LOT! I give a look on your book and I like books with a lot of code to read. I've alread rate it on Amazon! Thanks for writing this book. I will fill a lot of gaps in my GAS knowledge!

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.