首页 > 解决方案 > 只有 1 行时,Google 表格组脚本错误

问题描述

我有一个脚本,可以将数据从一张表复制到另一张表中,作为存档。我有它,所以当它将数据粘贴到存档表中时,它将对数据进行分组和折叠。但是,当它只有 1 行时,我不希望它对数据进行分组。这导致错误:

“异常。无效的参数”

这是参考这一行:

ts.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();

我认为一个解决方案可能是合并一个 if 数组,如果复制的数据范围有 1 行什么都不做,2 行将偏移 1 行然后分组然后折叠,3 或更多行将运行相同的代码我有以下。

这是选择数据、组然后折叠它的代码:

 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();

完整脚本:

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() 
 }

标签: google-apps-scriptgoogle-sheets

解决方案


解释:

我了解您录​​制了一个宏来生成此代码。但是,如果您对官方文档进行一些研究,您将能够编写自己的代码并以更少的行数实现相同的结果,但也可以对其进行概括,使其适用于多个项目。

但是,您的问题的直接解决方案可能如下:

您可以获取的值selection并检查此数组的长度是否大于1或换句话说,所选数据包含多于1行:

var nrows = ts.getSelection().getActiveRange().getValues().length;

然后使用if条件检查数据是否包含超过1row: if(nrows>1)

解决方案:

尝试这个:

 ts.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
 ts.getCurrentCell().offset(1, 0).activate(); //offset by 1 row for the header
 var currentCell = ts.getCurrentCell();
 var nrows = ts.getSelection().getActiveRange().getValues().length;
 if (nrows>1){
 ts.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
 currentCell.activateAsCurrentCell();
 ts.getActiveRange().shiftRowGroupDepth(1).collapseGroups();
 }

推荐阅读