user14389287
user14389287

Reputation: 39

How can I group 10 cells using Google Apps Script

I am using google apps script for my project and have 3 rows, Students, Teacher, and Extra-help, now for all element in the sheet, i want to group 10 students, 1 teacher and 1 Extra-help, I was thinking about using colors for this purpose, for example for every tenth student and teacher and Extra-help, I can change their background color to blue, then red for the next ten, then, yellow for the next ten, also if in the end the number of users is not sufficient, don't do anything until the requirement is met, please help.

Upvotes: 0

Views: 102

Answers (1)

SputnikDrunk2
SputnikDrunk2

Reputation: 4058

NOTE:

Please be reminded to always share a sample sheet or screenshot whenever possible, as this will help us better undertand/visualize your issue and for us to be able to provide a better/exact recommendation for you.

Recommendation:

You can refer to this sample sheet and code that I've created below, based on my understanding of your post. Here is a sample sheet with this design structure:

enter image description here

Then, I've created this sample code where it color blocks every group of 10 students:

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;
  }
}

After running the code, this will be the result on the sample sheet file:

enter image description here

On the Apps Scrip editor's execution logs, here's the result:

enter image description here enter image description here

Upvotes: 1

Related Questions