I have a script that copies the data from one sheet into another which acts as an archive. I have it so when it pastes the data into the archive sheet, it will group and collapse the data. However, when it is only 1 row I don't want it to group the data. This is causing an error:
"Exception. Invalid argument"
which is in reference to this line:
ts.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
I think a solution to this might be to incorporate an if array where if the range of data copied has 1 row do nothing, 2 rows will offset by 1 row then group then collapse, and 3 or more rows will run the same code I have below.
Here is the code that selects the data, groups, and then collapse it:
ts.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
ts.getCurrentCell().offset(1, 0).activate(); //offset by 1 row for the header
var currentCell = ts.getCurrentCell();
ts.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
ts.getActiveRange().shiftRowGroupDepth(1).collapseGroups();
Full script:
function CopyRangeSoldTest() {
var sss = SpreadsheetApp.openById('1vT4R-_xPx3Z7bOYWgpOp2JCypL2yODxcJ'); //replace with source ID
var ss = sss.getSheetByName('Sold'); //replace with source Sheet tab name
var range = ss.getDataRange(); //assign the range you want to copy
var data = range.getValues();
var tss = SpreadsheetApp.openById('1srDDle9R81Qlh'); //replace with destination ID
var ts = tss.getSheetByName('Sold History'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+2,1,ss.getLastRow(),5).setValues(data)
ts.getRange(ts.getLastRow(),1,ts.getLastRow(),5).activate();
ts.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
ts.getCurrentCell().offset(1, 0).activate();
var currentCell = ts.getCurrentCell();
ts.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
ts.getActiveRange().shiftRowGroupDepth(1).collapseGroups();//.shiftRowGroupDepth(1).collapseGroups();//you will need to define the size of the copied data see getRange()
var sss = SpreadsheetApp.openById('1vT4R-_xPx3Z7bOYWgpOp2JCypL2yODx'); //replace with source ID
var ss = sss.getSheetByName('Received'); //replace with source Sheet tab name
var range = ss.getDataRange(); //assign the range you want to copy
var data = range.getValues();
var tss = SpreadsheetApp.openById('1srDDle9R81Qlh'); //replace with destination ID
var ts = tss.getSheetByName('Received History'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+2,1,ss.getLastRow(),5).setValues(data)
ts.getRange(ts.getLastRow(),1,ts.getLastRow(),5).activate();
ts.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
ts.getCurrentCell().offset(1, 0).activate();
var currentCell = ts.getCurrentCell();
ts.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
ts.getActiveRange().shiftRowGroupDepth(1).collapseGroups();//.shiftRowGroupDepth(1).collapseGroups();//you will need to define the size of the copied data see getRange()
}