OrangeHat
OrangeHat

Reputation: 7

Setting Values in Google Apps Script

I'm trying to set values using the following logic:

function compare(){
   var values = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();

   for(n=0;n<values.length;++n){

     var cell = values[n][0] ; 
     values[n][0].setValue("myValue");
   }
}

But I can't set the value this way. I'm trying to use the cell[n][x] notation because its easier for what I have to do later on.

Thanks in advance for your help.

Upvotes: 0

Views: 512

Answers (1)

Eduardo Conte
Eduardo Conte

Reputation: 1203

You can't use .setValues() because values[n][0] is not an object, it is just the value of a 2D-array. Just use values[n][0] = "my Value". At the end you will have editted your array, then you'll need to use range.setValues() to set the new values to your range.

Example:

function compare(){
  var myRange = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange();
  var values = myRange.getValues();

  for(var n=0;n<values.length;n++){
    values[n][0]="myValue "+n;
    values[n][1]="myValue 2 "+n;
  }
  myRange.setValues(values)

}

Upvotes: 2

Related Questions