Returning a range with multiple rows with joined columns in Google Appscript

I am creating a custom function for Google Sheets using Google AppScript. I have a dataset that my user function summarizes and displays a summary on another sheet.

I can create my own algorithm for the summary and return this range:

Current

enter image description here

Now I understand that the Appcript function returns a two-dimensional array, but my problem is how to merge some cells using a Scriptcript application? Whenever there is a month? Like this:

Target

enter image description here

In the above example, the range that I will return from my user function will be

[
    ['January', ''],
    ['Project 1', '10 Hours'],
    ['Project 2', '20 Hours'],
    ['Project 3', '30 Hours'],
    ['Project 4', '40 Hours'],
    ['Project 5', '50 Hours'],
    ['February', ''],
    ['Project 1', '10 Hours'],
    ['Project 2', '20 Hours'],
    ['Project 3', '30 Hours'],
    ['Project 4', '40 Hours'],
    ['Project 5', '50 Hours']
]

So, how do I tell AppScript to tell GooglSheets that ['January', ''],both ['February', ''],are cells that need to be combined?

Excel , , .:)

+4
2

Sheet1 Sheet2 3 4 .

function myFunction() {  
  var ss = SpreadsheetApp.getActiveSpreadsheet();  
  var sheet1 = ss.getSheets()[0];
  var data = sheet1.getDataRange().getValues();   
  var mergeRows = [];  
  for(var i = 0; i < data.length; i++) {    
    if (data[i][1] == "") {
      mergeRows.push(i);      
    }
  }  
  var startRow = 3;  
  var startCol = 4;  
  var sheet2 = ss.getSheets()[1];  
  sheet2.getRange(startRow, startCol, data.length, data[0].length).setValues(data);  
  for(var i = 0; i < mergeRows.length; i++) {    
    sheet2.getRange(startRow + mergeRows[i], startCol, 1, 2).merge().setHorizontalAlignment('center');
  }  
}
+2

merge() mergeAcross(). -

sheet.getRange('A1:B1').merge();
sheet.getRange('A7:B7').merge();
+1

Source: https://habr.com/ru/post/1619430/


All Articles