google-apps-scriptgoogle-sheetsgoogle-apps-for-education

2D array reading issue?


I want to create a sort of "stack" and every time I delete an item, the sheet removes the blank cells. I can't use a filter function for this, obviously.

I am having trouble reading the array that is created for this purpose.

My very pseudo-code : I create an empty array, get all the values (including the empty ones), populate my array with all the values except the empty ones, and finally clear the stack and set the values with my array.

Here is my code :

function updateStack() {
 
 var ss = SpreadsheetApp.getActive();
 var sheet = ss.getSheetByName("main");
  
 var zone = sheet.getRange(1, 1, 1, 10);
  
  //seems that .getValues() returns a 2d array

 var values = zone.getValues();
 var j = 0;
  
 var data = new Array();
  
  for (var i = 0 ; i < 10 ; i++) {

    //the problem seems to be here : I can't access the 2d array. After reading the debugging console about 1000 thousand times
    // I discovered the 2 pairs of []  
    
    //I've found multiple ways to detect empty cells. Not sure if this is the right one. I've tried the .length = 0 trick, but something
    // was wrong, maybe because of the "2dimensionality"

    
    if (values[i] != "") {
      
      data[j] = values[i];
      j = j++;
      
    } else {
      
      // do nothing if the cell contains nothing
     
    }
   
  //not sure if I have to use return ! Don't know where to put it exactly too...
  return data; 
  zone.clear();
    //length of range must be the same as the array's length
    
  zone = sheet.getRange(1, 1, 1, data.length);
  zone.setValues(data);
  }
}

There are many comments in my code, I hope you will understand. A link to my test sheet : http://bit.ly/1JiWutn

Thanks for any help !


Solution

  • Currently, you have a section of code like this:

    if (values[i] != "") {
    
      data[j] = values[i];
      j = j++;
    
    } else {
    

    You are testing for an empty string:

    values[i] != ""
    

    But values[i] is an inner array. Your code is getting only one row, and 10 columns.

    var zone = sheet.getRange(1, 1, 1, 10);
    

    So, the array looks like this:

    [ [cell one,cell two,cell three,etc,cell ten ] ]
    

    values[i] returns an inner array, not a value.

    To get the cell value use:

    if (values[0][i] != "") {
    

    You need two indexes, the first index will always be zero. There is only one inner array with all the cell values in it.

    Next, use push to add a value to the data array:

    data.push(values[0][i]);
    

    Another issue is where you have the return statement. A return statement kills the current function. Anything after the return statement inside of that function will not run. So, you can't have a return statement where you have it, and get the code to write values to the spreadsheet. You can do both. You can both write values to the sheet, and return something, but put the return at the end. The return, returns something to whatever function called this function.

    To set values, the values MUST be in a two dimensional array. Your data array is not a 2D array. You must add the data array to yet another array.

    var my2Darray = [];
    my2Darray.push(data);
    
    zone = sheet.getRange(1, 1, 1, data.length);
    zone.setValues(my2Darray);