Created
January 25, 2024 22:11
-
-
Save mustpax/ee66966f4104473658db166cc5a1d147 to your computer and use it in GitHub Desktop.
Google Apps Script for creating collapsible groups (assumes data is sorted, requires underscore)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function run() { | |
groupByColumn(0); | |
groupByColumn(1); | |
groupByColumn(2); | |
} | |
function selectRows(sheet, startRow, endRow) { | |
let numRows = endRow - startRow; | |
let startRow1Indexed = startRow + 2; | |
return sheet.getRange(startRow1Indexed, 1, numRows, sheet.getDataRange().getNumColumns()); | |
} | |
function groupByColumnB() { | |
return groupByColumn(1); | |
} | |
function groupByColumn(columnIndex) { | |
let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
let range = sheet.getDataRange(); | |
let values = range.getValues().map((row, index) => ({ row, index })) | |
values = values.slice(1); // skip header row | |
let colValues = values.map(({index, row}) => ({index, col: row[columnIndex]})) | |
let groups = _.groupBy(colValues, 'col') | |
for (let [group, rows] of Object.entries(groups)) { | |
console.log({group, rows}) | |
rows = rows.map(row => row.index) | |
if (rows.length > 2) { | |
selectRows(sheet, _.min(rows), _.max(rows)).shiftRowGroupDepth(1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment