简体   繁体   中英

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.

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)

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM