简体   繁体   English

将粘贴复制到另一张纸并找到没有空行的最后一行

[英]Copy paste to another sheet and find last row without empty rows

I want to copy just the values of a specific row to a second sheet to get a summary/archive.我只想将特定行的值复制到第二张表以获取摘要/存档。

For this reason I need find the last empty row.出于这个原因,我需要找到最后一个空行。

I am currently using the following:我目前正在使用以下内容:

function CopyPaste() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("Data");
  var pasteSheet = ss.getSheetByName("Summary");

  // get source range
  var source = copySheet.getRange(20, 1, 20, 5);
  // get destination range
  var destination = pasteSheet.getRange(pasteSheet.getLastRow() +1, 1, 1, 5);

  // copy values to destination range
  source.copyTo(destination, {contentsOnly: true});

My problem is that everytime when I run the code, it will insert 7 "empty" rows below the copied row.我的问题是每次运行代码时,它都会在复制的行下方插入 7 个“空”行。

I know that they are not really empty and found serveral solutions (below) to solve this issue, but unfortunately i dont get it combined and running.我知道它们并不是真的空,并找到了几个解决方案(如下)来解决这个问题,但不幸的是我没有把它组合起来运行。

function getLastDataRow(sheet,col) {
// col in letter
  var lastRow = sheet.getLastRow();
  var range = sheet.getRange(col + lastRow);
  if (range.getValue() !== "") {
    return lastRow;
  } else {
    return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
  }              
}
function onOpen(){
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheetname = ss.getSheets()[0].getName();
  // Logger.log("DEBUG: sheetname = "+sheetname)
  const sheet = ss.getSheetByName(sheetname);
  const values = sheet.getRange('A7:A').getValues();
  // const lastRow = values.length - (values.reverse().findIndex(row => !!row[0]));
    const firstEmptyRow = values.findIndex(row => !row[0]) + 1;
  // const range = sheet.getRange(lastRow,1);
    const range = sheet.getRange(firstEmptyRow,1);
  sheet.setActiveRange(range);
}
function getLastRow_(sheet, columnNumber) {
  // version 1.5, written by --Hyde, 4 April 2021
  const values = (
    columnNumber
      ? sheet.getRange(1, columnNumber, sheet.getLastRow() || 1, 1)
      : sheet.getDataRange()
  ).getDisplayValues();
  let row = values.length - 1;
  while (row && !values[row].join('')) row--;
  return row + 1;
}

Try something like this:尝试这样的事情:

function CopyPaste() {
  var ss = SpreadsheetApp.getActive();
  var sh1 = ss.getSheetByName("Sheet0");
  var sh2 = ss.getSheetByName("Sheet1");
  var vs1 = sh1.getRange(20, 1, 20, 5).getValues().filter(r => r.some(c => c != ''));
  sh2.getRange(sh2.getLastRow() +1, 1, vs1.length, 5).setValues(vs1);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从最后一行复制特定的列数据并粘贴到另一个工作表 - Copy specific Columns data from Last Row and paste to another sheet 将多行复制并粘贴到另一张纸上的下一个空行中 - Copying and Paste multiple rows into next empty Row on another sheet 从母版工作表的最后一行复制数据,并将其粘贴到Google工作表中另一工作表的特定单元格 - Copy data from last row of master sheet and paste it in specific cells in another sheet in google sheets Select 单元格(Ctrl+A)然后复制范围并粘贴到另一个工作表的第一个空行 - Select cells(Ctrl+A) then Copy range and paste to another sheet's first empty row 复制一个范围并仅将具有值和图像的行粘贴到另一个工作表中下一个空白行的谷歌工作表中? - Copy a range and paste only rows with values & images into another sheet at the next blank row in the google sheets? 复制一个范围并仅将带有值的行粘贴到下一个空白行的另一个工作表中? - Copy a range and paste only rows with values into another sheet at the next blank row? 复制粘贴到另一张纸 - Copy Paste to another sheet 用于在最后一条记录后复制并粘贴到另一个工作表的 Google Sheets 脚本 - Google Sheets script to copy and paste in another sheet after the last record 复制包含特定值的行并将其粘贴到另一个工作表 - copy rows that contains certain value and paste it to another sheet 如果单元格值 = x,则有效地将行复制并粘贴到另一个工作表 - Efficiently Copy and Paste Row to Another Sheet if Cell Value = x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM