I have a Spreadsheet with about 3000 rows which are grouped by order number. I'm trying to build an macro to:
remove all groups
multisort all rows
recreate groups
collapse all groups marked as finished orders (optional - have no idea how to achieve this )
SHEET_NAME = "PLAN"; SORT_DATA_RANGE = "A2:CJ"; GROUP_DATA_RANGE = "BQ2:BQ"; SORT_ORDER = [ {column: 40, ascending: false}, {column: 2, ascending: true}, {column: 4, ascending: true} ]; function Sortowanie() { var ss = SpreadsheetApp.getActiveSpreadsheet(); removeAllGroups(); multiSortColumns(); groupRows(); ss.toast('Zakończono.'); } function multiSortColumns(){ var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName(SHEET_NAME); var range = sheet.getRange(SORT_DATA_RANGE + sheet.getLastRow()); range.sort(SORT_ORDER); ss.toast('Sortowanie zakończone.'); } function removeAllGroups() { const ss = SpreadsheetApp.getActive(); const ssId = ss.getId(); const sheet = ss.getSheetByName(SHEET_NAME); const sheetId = sheet.getSheetId(); sheet.expandAllRowGroups(); const n = Sheets.Spreadsheets.get(ssId, { ranges: [SHEET_NAME] }).sheets[0].rowGroups.reduce((n, { depth }) => n < depth ? depth : n, 0); const requests = Array(n).fill("").map(_ => ({ deleteDimensionGroup: { range: { sheetId, dimension: "ROWS" } } })); Sheets.Spreadsheets.batchUpdate({ requests }, ssId); ss.toast('Usuwanie grup zakończone.'); } function groupRows() { const ss = SpreadsheetApp.getActive(); const sheet = ss.getSheetByName(SHEET_NAME); const levels = sheet.getRange(GROUP_DATA_RANGE + getLastRowSpecial()).getValues(); const sheetId = sheet.getSheetId(); const requests = levels.flatMap(([a], i) => Array(a).fill("").map(_ => ({ addDimensionGroup: { range: { sheetId, startIndex: i + 1, endIndex: i + 2, dimension: "ROWS" } } }))); Sheets.Spreadsheets.batchUpdate({ requests }, ss.getId()); ss.toast('Ponowne grupowanie zakończone.'); } function getLastRowSpecial() { const ss = SpreadsheetApp.getActiveSpreadsheet() const sheet = ss.getSheetByName(SHEET_NAME); const lastRow = sheet.getRange(GROUP_DATA_RANGE).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow(); return lastRow };
Everything works perfectly when I limit range to 1000 rows. When I try to run this for a whole range or more than 1000 and debuging, I'm getting this:
HttpResponseException: Response Code: 413. Message: response too large.
Without debugger after a while I'm getting this:
The JavaScript runtime has unexpectedly terminated.
Most bizarre thing is that script is creating these groups for a whole range and then error comes out after a while.
I think that 3K rows is not a big range for that, maybe someone has an idea what is wrong?
Here it is sample data sheet: https://docs.google.com/spreadsheets/d/1DLXxZVyrhDxrBe1AX3iy54nQTFVJkoIpeos7M9mEaIo/edit?usp=sharing