1
votes

I want to build a menu function to edit the selected values in a google sheet:

function fDoSomethingWithRange(){
  //var theSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();

  var theSheet = SpreadsheetApp.getActiveSheet();
  var theRange = theSheet.getActiveRange();
  var theValues = theRange.getValues();
  var theArray = new Array(theRange.getHeight());
  //theSpreadsheet.toast(theValues);

  for (var i = 0;i<theRange.getHeight();i++){
    theArray[i]=new Array(theRange.getWidth());
    };

  for (var h=0;h<theRange.getHeight();h++){  
    for (var w=0;w<theRange.getWidth();w++){

      var i = h + (w * theRange.getHeight());
      theArray[h][w] = parseInt(theValues[i]) * 2; // just an example return double values
    };
  };
  theRange.setValues(theArray);
};

I get stuck on returning the array; the second column is empty and I just don't see why. Anybody?

thanks, Arvid

1

1 Answers

4
votes

Ok, found it myself... finally :-)

getValues() gets a two-dimensional array. I was fooled by the toast message which displays just a row of values, suggesting getValues() gets a one-dimensional array.

function fDoSomethingWithRange(){
  //var theSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();

  var theSheet = SpreadsheetApp.getActiveSheet();
  var theRange = theSheet.getActiveRange();
  var theValues = theRange.getValues();
  var theArray = new Array(theRange.getHeight());
  //theSpreadsheet.toast(theValues);

  for (var i = 0;i<theRange.getHeight();i++){
    theArray[i]=new Array(theRange.getWidth());
    };

  for (var h=0;h<theRange.getHeight();h++){  
    for (var w=0;w<theRange.getWidth();w++){

      var i = h + (w * theRange.getHeight());
      theArray[h][w] = parseInt(theValues[h][w]) * 2; // just an example return double values
    };
  };
  theRange.setValues(theArray);
};