r/GoogleAppsScript • • Sep 01 '19

How can i group rows using google apps scripts ?

I am writing code in which a user can automatically generate a template of lesson and sub-topics. Each lesson will have 10 sub-topics.

I also need to group the rows lesson-wise and topic-wise. But, I am unable to group the rows lesson-wise and topic-wise. Tried using the macro-recorder, but the code does not work while generating multiple lessons.

Screenshot of the template Here is my code:

function Lessongeneration() {
      var spreadsheet = SpreadsheetApp.getActive();
      var sheet = spreadsheet.getActiveSheet();
      var row = 6;
      var col = 2;
    
      //Ask user for the no. of lessons
      var shlen = Browser.inputBox("Enter no of lessons", Browser.Buttons.OK_CANCEL);

  for (var i = 1; i <= shlen; i++) {

   sheet.getRange(row,col).setValue("Lesson " + i);
   row++; 

   // Add sub-topics (1.1, 1.2 ....)   
      leave 6 blank rows below each sub-topic. Then, group those blank rows

     for (var j=1;j<=10;j++){

       sheet.getRange(row,col).setValue(i+"."+j);

       **tried this, not working**
       sheet.getRange(spreadsheet.getCurrentCell().getRow() + 1, 1, 6, 
       sheet.getMaxColumns()).activate()
      .shiftRowGroupDepth(1);

       row=row+7;

     }
   }
};
6 Upvotes

1 comment sorted by

1

u/AmnesiaInnocent Sep 01 '19

I would suggest that you first try grouping a single set of rows that already exist.

Then work your way up to creating/grouping rows dynamically...