简体   繁体   English

如何从电子表格的Google GAS数组中设置值

[英]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. 我已经使用Google脚本一个星期了,我已经进行了尽可能多的搜索以获得答案。 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). 我写了一个简单的脚本,根据课程号的后三位数字(即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. 我不知道如何写回Google表格。 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. 我现在在7行上运行它,但需要在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[][] . 要记住的重要一点是, Range的尺寸必须等于Array [] []的精确尺寸。 This array must be two-dimensional! 该数组必须是二维的! Otherwise you'll get an error that setValues() method expects an Object[][], not an Array. 否则,您将收到一个错误,提示setValues()方法需要Object [] [],而不是Array。

You're trying to set a simple array. 您正在尝试设置一个简单的数组。 Also, the method you'll use is setValues(), not setValue(). 另外,您将使用的方法是setValues(),而不是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 只需将for(var k = 0; k<students.length; k++)内部for循环移到主要for循环的外部并按照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 这是我的电子表格https://docs.google.com/spreadsheets/d/1iA9v_3rPH9JAhAmt2EJTdbqTQkFEl7yewawPy3w4YIg/edit#gid=0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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