Javascript 如何使用Google Apps脚本对10个单元格进行分组

Javascript 如何使用Google Apps脚本对10个单元格进行分组,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我在我的项目中使用谷歌应用程序脚本,有3行,学生,老师和额外帮助,现在对于表格中的所有元素,我想将10名学生,1名老师和1名额外帮助分组,我正在考虑为此使用颜色,例如每10名学生和老师以及额外帮助,我可以将它们的背景颜色更改为蓝色,然后在接下来的十个颜色中更改为红色,然后在接下来的十个颜色中更改为黄色,此外,如果最终用户数量不足,在满足要求之前不要做任何事,请帮助。注意: 请注意,尽可能始终共享样本表或屏幕截图,因为这将有助于我们更好地理解/可视化您的问题,并使我们能够为您提供更好/准确的建议

我在我的项目中使用谷歌应用程序脚本,有3行,学生,老师和额外帮助,现在对于表格中的所有元素,我想将10名学生,1名老师和1名额外帮助分组,我正在考虑为此使用颜色,例如每10名学生和老师以及额外帮助,我可以将它们的背景颜色更改为蓝色,然后在接下来的十个颜色中更改为红色,然后在接下来的十个颜色中更改为黄色,此外,如果最终用户数量不足,在满足要求之前不要做任何事,请帮助。

注意:

请注意,尽可能始终共享样本表或屏幕截图,因为这将有助于我们更好地理解/可视化您的问题,并使我们能够为您提供更好/准确的建议

推荐:

根据我对您文章的理解,您可以参考我在下面创建的示例表和代码。以下是具有此设计结构的样本表:

然后,我创建了一个示例代码,它对每组10名学生进行颜色分块:

function checkStudent() { //This is the main function to be run
  if(numberOfStudents()>10){ //If statement to check if there are more than 10 students
    Logger.log("There are "+numberOfStudents()+" stundent(s) in Column A"); //If ever you want to check the execution log
    colorStudentGroup(); //This will run the function to color every group of 10 in different colors
  }else{
    Logger.log("No student group of 10 yet detected"); //Log if ever you want to check the execution log
  }
}

function numberOfStudents(){ //This function counts total number of students on Column A ('A:A')
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var values = ss.getRange('A2:A').getValues(); //This code gets all student in column A from row 2 and below
  var students = [].concat.apply([], values).filter(String); //This gets only rows that has student names and not the empty ones.
  return students.length; //Returns the total number of students
}

function colorStudentGroup(){ //This function colors every group of 10 to blue,red, yellow and so on and so forth
  var numOfTimesToRun = 1; //Indicates how many times the loop will run based on the number of groups of 10
  var numbOfRows = 2; //Contains the number of rows per group that will be colored. It starts on Row 2 on Column A on the Sheet
  var numofGroups = roundStudentGroups(); //This indicates how may groups of 10 there are on column A
  Logger.log("There are "+numofGroups + " groups of 10"); //If ever you want to check the execution log
  var color = ["blue", "red", "GoldenRod"]; //Declares the colors to be used. Used GoldenRod shade of yellow as text in white font color are visible on this one)
  var colorCount = 0; //This will tell what color to be used for the next groups of 10. It starts with 0 (blue)
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  do {
    if(colorCount == 3){ //checks if all 3 colors are already used and will reset to blue again for the next group of 10 and then so on and so forth
      colorCount = 0; //reset color count (0) to start again to blue
    }
    Logger.log("Student Group "+ numOfTimesToRun); //If ever you want to check the execution log
    for(var x = 1; x<=10; x++){
      Logger.log("Row "+numbOfRows+ " in color "+ color[colorCount]); //If ever you want to check the execution log
      ss.getRange('A'+numbOfRows+':C'+numbOfRows).setBackground(color[colorCount]).setFontColor('white'); //Sets column A:C on the sheet with different color per group of 10.
      numbOfRows++;
    }
    numOfTimesToRun++;
    colorCount++;
  }
  while (numOfTimesToRun <= numofGroups); //This tells the loop how many times it will run based on the number of groups of 10
}

function roundStudentGroups(){ //This function calculates how many groups of 10 there are on Column A (students)
  var group = Math.round(numberOfStudents()/10); //Total number of groups of 10 is being calculated by total student numbers divided by 10
  if(numberOfStudents()%10 >= 5){ //This if statement will identify that any remainder that's more than 5, the total number of groups of 10 will not be rounded off. This is needed so that other students that are not in group of 10 will not be colored (e.g if there are 15 total students, there will only be 1 group of 10 so the 5 remaining will be disregarded).
    return group-1;
  }else{
    return group;
  }
}
函数checkStudent(){//这是要运行的主要函数
if(numberOfStudents()>10){//if语句检查是否有超过10名学生
Logger.log(“A列中有“+numberOfStudents()+”stundent”);//如果要检查执行日志
colorStudentGroup();//这将运行该函数,以将每组10的颜色设置为不同的颜色
}否则{
Logger.log(“尚未检测到10个学生组”);//如果要检查执行日志,请记录日志
}
}
函数numberOfStudents(){//此函数计算A列('A:A')上的学生总数
var ss=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var values=ss.getRange('A2:A').getValues();//此代码从第2行及以下行获取列A中的所有学生
var students=[].concat.apply([],values.filter(String);//这只获取具有学生名称的行,而不获取空的行。
return students.length;//返回学生总数
}
函数colorStudentGroup(){//此函数将每组10的颜色设置为蓝色、红色、黄色等等
var numotimestorun=1;//表示循环将根据10个组的数量运行多少次
var numbOfRows=2;//包含每组将着色的行数。它从工作表A列的第2行开始
var numofGroups=roundStudentGroups();//这表示A列上10个组的数量
Logger.log(“有”+numofGroups+“10个组”);//如果您想查看执行日志
var color=[“蓝色”、“红色”、“黄花”];//声明要使用的颜色。使用的黄花阴影作为白色字体颜色的文本在此字体上可见)
var colorCount=0;//这将告诉下一组10使用什么颜色。它以0(蓝色)开始
var ss=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
做{
if(colorCount==3){//检查是否所有3种颜色都已使用,并将在下一组10中再次重置为蓝色,依此类推
colorCount=0;//重置颜色计数(0)以重新开始为蓝色
}
Logger.log(“学生组”+numOfTimesToRun);//如果您想要检查执行日志

对于(var x=1;xCan),您可以提供一个指向示例的图像或链接,以显示数据的布局,以及您希望结果是什么样的?还可以添加您尝试使用任何日志的代码。您的目标到底是什么?您指的是什么组?