Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-macros

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.

enter image description here

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.

EDIT: Working code is updated below.

  function shiftrowgroupdepth() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getActiveSheet();


  // start from row 6 and column 2
  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++;

    Logger.log(spreadsheet.getCurrentCell().getRow())
   sheet.getRange(row, 1, 70, sheet.getMaxColumns()).activate()
  .shiftRowGroupDepth(1);

   // Add sub-topics (1.1, 1.2 ....)

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

     sheet.getRange(row,col).setValue(i+"."+j);
     sheet.getRange(row+1, 1, 6, sheet.getMaxColumns()).activate()
    .shiftRowGroupDepth(1);

     row=row+7;

     }    


  }
};

Solution

  • The OP code was very close to the mark. The main changes in this answer are:

    • When using a 'dot' separator for the topic codes, Google sheets treats the resulting value as a number; this creates problems displaying '1.10'. I changed the separator to a 'dash'. No doubt there is another potential approach using toString - but this was quick and easy.

    • The Lesson grouping is straightforward; 10 topics, 7 rows per topic = 70 rows.

    • Topic grouping had been complicated by referring to the location of the "current cell" - which could be anywhere on the sheet. I simplified this by using the row variable, which the OP had already (correctly) incremented.


    function so5774532602() {
    
      var ss = SpreadsheetApp.getActive();
      var sheetname = "OPSheet";
      var sheet = ss.getSheetByName(sheetname);
      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);
    
        // add grouping
        // Logger.log("DEBUG: i = "+i+", lesson range = "+sheet.getRange(+(row + 1), 2, 70, 1).getA1Notation());
        sheet.getRange(+(row + 1), 2, 70, 1).activate()
          .shiftRowGroupDepth(1);
    
        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++) {
          // Logger.log("DEBUG: i = "+i+", j = "+j+", row = "+row+", col = "+col); // new
          sheet.getRange(row, col).setValue(i + "-" + j);
    
          // add grouping
          // Logger.log("DEBUG: range details: row = "+(row + 1) +",column = 1"+"number of rows = "+6+", number of columns = 1");
          // Logger.log("DEBUG: topic range = "+sheet.getRange(+(row + 1), 2, 6, 1).getA1Notation());
          sheet.getRange(+(row + 1), 2, 6, 1).activate()
            .shiftRowGroupDepth(1);
    
          row = row + 7;
    
        }
      }
    }
    

    Edit Two minor changes for formatting

    1. sheet.getRange(row,col).setValue("Lesson " + i).setHorizontalAlignment("center");
      Centres the Lesson Number in the column.

    2. sheet.getRange(row,col).setNumberFormat("@").setValue(i+"."+j).setHorizontalAlignment("center");
      A return to a 'dot' separator but enables the tenth topic to display as 1.10, etc (credit @Tanaike). Will also center the text in the column.