简体   繁体   中英

How to Setvalues off of an array google GAS for spreadsheets

I have been using google scripts for a week, and I have searched as much as I could to get the answer. Can someone please help me? I wrote a simple script to evaluate if a course is online based on the last three digits of a course number(ie PSY-250-400). The script works fine, and I pushed the result into the end of the array. I don't know how to write back to google sheets. Below is what I have. Currently it will set the values based on the first result(online course). So all values are set to online. I am running it on 7 rows right now, but will need to run it on 20,000.

function onlineonly(online){
   var sheet = SpreadsheetApp.getActiveSheet();
   var students = sheet.getRange('A2:D7').getValues();
   var online = ["400","401","403","404","600"];
   var m;
   var section;

   for(var i=0; i<students.length; ++i){

       section = students[i][3].substring(8,13);

       for(var j = 0;j<online.length; j++){

           if(section===online[j]){
               section = m; 
           }
       } 

       if(section === m){

           students[i].push("online");

       } else {

           students[i].push("not online");

       }

       var method = [];

           for(var k = 0; k<students.length; k++){ 

               if(students[i][4]=== "online"){
                   method = "online";
               } else {
                   method = "in person";
               }

           sheet.getRange('c2:c7').setValue(method);

           }
       }
   }

The important thing to remember is that the dimensions of the Range must equal the exact dimensions of the Array[][] . This array must be two-dimensional! Otherwise you'll get an error that setValues() method expects an Object[][], not an Array.

You're trying to set a simple array. Also, the method you'll use is setValues(), not setValue().

Your code is a little hard to understand, so this is an example of the pattern:

function writeOutValues() {

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();

    var range = sheet.getRange("C2:C7");
    var values = range.getValues();

    //remember, a 2d array is a grid of values that corresponds to the grid of the range like so: values[row][column]
    //so in this range, values[0][0] = C2, values[1][0] = C3, etc.

    values[0][0] = "New Value";
    values[1][0] = "Another one";

    //to set value, 
    range.setValues(values);

}

just move inner for loop for(var k = 0; k<students.length; k++) to outside the main for loop and apply the technique as told by zbnrg
here is working code

function onlineonly()
{
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var students = sheet.getRange('A2:C4').getValues();
  var online = ["400","401","403","404","600"];
  var m;
  var section;
  for(var i=0; i<students.length; ++i)
  {
     section = students[i][1].slice(8,11);
     for(var j = 0;j<online.length; j++)
     {
       if(section===online[j])
       {
           section = m; 
       }
     } 

     if(section === m)
     {
         students[i].push("online");
     } 
     else 
     {
        students[i].push("not online");
     }
  }
var range = sheet.getActiveSheet().getRange("C2:C4");
var method = range.getValues();
for(var k = 0; k<students.length; k++)
method[k][0] = students[k][3]==="online"?"online":"in person";   
Logger.log(method[0][0] +" "+method[1][0] +" "+ method[2][0])
range.setValues(method);
}

here is my spreadsheet https://docs.google.com/spreadsheets/d/1iA9v_3rPH9JAhAmt2EJTdbqTQkFEl7yewawPy3w4YIg/edit#gid=0

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