Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google sheets 添加行并复制行中的公式-超时_Google Sheets - Fatal编程技术网

Google sheets 添加行并复制行中的公式-超时

Google sheets 添加行并复制行中的公式-超时,google-sheets,Google Sheets,我有一个超过4000行和30列的大表。 如果有少于x个“空”行,我将尝试自动向工作表中添加新行。空行仅通过包含公式定义,但数据列A为空 要检查行是否为空,我检查列a,因为您必须在列a中输入数据。添加新行后,脚本应复制/粘贴新行之前的最后一行,以便它们也包含公式 这是脚本(以前的版本): function addRowsItems() { var ss = SpreadsheetApp.getActive(); var sh = ss.getSheetByName('items');

我有一个超过4000行和30列的大表。 如果有少于x个“空”行,我将尝试自动向工作表中添加新行。空行仅通过包含公式定义,但数据列A为空

要检查行是否为空,我检查列a,因为您必须在列a中输入数据。添加新行后,脚本应复制/粘贴新行之前的最后一行,以便它们也包含公式

这是脚本(以前的版本):

  function addRowsItems() {

  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName('items');  

  var freeRows = 300; // Number of empty Rows after last Entry

  var lRow = sh.getLastRow(), lCol = sh.getLastColumn(), range = sh.getRange(lRow,1,1,lCol);

  var startRow = lRow-freeRows;

  if(startRow < 0) {
    startRow = 1; }

  var values = sh.getRange("A" + startRow + ":A").getValues(); 
  var maxIndex = values.reduce(function(maxIndex, row, index) {
    return row[0] === "" ? maxIndex : index;
  }, 0);

  var maxIndex = maxIndex + startRow;

  var space = lRow - maxIndex;
  var addLines = freeRows - space;

  if(space < freeRows) {

     sh.insertRowsAfter(lRow, addLines); 
     range.copyTo(sh.getRange(lRow+1, 1, addLines, lCol), {contentsOnly:false});

    }
  }

尝试使用
COUNTBLANK
函数返回范围内的空单元格计数。不计算包含文本、数字、错误等的单元格。返回空文本的公式将被计数。

谢谢您的提示。它比
maxIndex
部分快。在这种情况下,
maxIndex
部分的优点是,在最后一个包含数据的单元格之后查找第一个空单元格是可以忽略的,因为中间没有那么多空单元格。因此,
COUNTBLANK
完成了这项工作。我还将
getLastRow
更改为
getMaxRows
,因为它看起来也快多了。
function addRowsItems() {

  var ss = SpreadsheetApp.getActive();
  var sh = ss.getSheetByName('items');  

  var freeRows = 307; // Number of empty Rows after last Entry

  var lRow = sh.getMaxRows(), lCol = sh.getMaxColumns(), range = sh.getRange(lRow,1,1,lCol);
  var space = sh.getRange("B1").getValues();
  var addLines = freeRows - space;

  if(space < freeRows) {

     sh.insertRowsAfter(lRow, addLines); 
     range.copyTo(sh.getRange(lRow+1, 1, addLines, lCol), {contentsOnly:false});

    }
  }