简体   繁体   English

复制一个范围并仅将具有值和图像的行粘贴到另一个工作表中下一个空白行的谷歌工作表中?

[英]Copy a range and paste only rows with values & images into another sheet at the next blank row in the google sheets?

I need your help to copy and paste script above.我需要你的帮助来复制和粘贴上面的脚本。 There are variable rows of data in the range of B12:W44. B12:W44范围内有可变行数据。 There might be 2 or 20 rows of data.可能有 2 或 20 行数据。 If there are 2 rows of data, there are 2 rows with values and 31 blank rows in the target range after I run the code.如果有2行数据,我运行代码后目标范围内有2行有值和31行空行。 How to copy and paste only cells with data?如何只复制和粘贴有数据的单元格? So much blank rows in the target range:((目标范围内有这么多空行:((

function Movedata3() {
  var ss = SpreadsheetApp.getActive();
  var srcSheet = ss.getSheetByName("INVOICE");
  var dstSheet = ss.getSheetByName("HISTORICAL SALES");
  var srcRange = srcSheet.getRange("B12:W44");
  var dstRange = dstSheet.getRange(dstSheet.getLastRow() + 1, 1);
  srcRange.copyTo(dstRange, { contentsOnly: true });
}

I believe your current situation and your goal are as follows.我相信你目前的情况和你的目标如下。

  • In your situation, the range of "B12:W44" of the source sheet has multiple empty rows.在您的情况下,源工作表的“B12:W44”范围有多个空行。 About the empty row, in this case, all columns from "B" to "W" are empty cells.关于空行,在这种情况下,从“B”到“W”的所有列都是空单元格。 And, the row includes the values and the images.并且,该行包括值和图像。
  • By removing the empty rows, you want to copy only the values and the images to the last row of the destination sheet.通过删除空行,您只想将值和图像复制到目标工作表的最后一行。

In this case, how about the following modification?在这种情况下,如何进行以下修改?

Modified script:修改脚本:

In this modification, Sheets API is used.在这个修改中,使用了Sheets API。 So, before you use this script, please enable Sheets API at Advanced Google services .因此,在您使用此脚本之前, 请在 Advanced Google services 中启用 Sheets API

function Movedata3() {
  var ss = SpreadsheetApp.getActive();
  var srcSheet = ss.getSheetByName("INVOICE");
  var dstSheet = ss.getSheetByName("HISTORICAL SALES");
  var srcRange = srcSheet.getRange("B12:W44");
  var srcSheetId = srcSheet.getSheetId();
  var dstSheetId = dstSheet.getSheetId();
  var srcStartRow = srcRange.getRow();
  var srcStartCol = srcRange.getColumn();
  var srcEndCol = srcStartCol + srcRange.getNumColumns();
  var dstRange = dstSheet.getRange(dstSheet.getLastRow() + 1, 1);
  var dstStartRow = dstRange.getRow();
  var requests = srcRange.getDisplayValues().reduce((ar, r, i) => {
    if (r.join("") != "") {
      ar.push({ copyPaste: { source: { sheetId: srcSheetId, startRowIndex: i + srcStartRow - 1, endRowIndex: i + srcStartRow, startColumnIndex: srcStartCol - 1, endColumnIndex: srcEndCol }, destination: { sheetId: dstSheetId, startRowIndex: dstStartRow + ar.length - 1, endRowIndex: dstStartRow + ar.length }, pasteType: "PASTE_VALUES" } });
    }
    return ar;
  }, []);
  Sheets.Spreadsheets.batchUpdate({ requests }, ss.getId());
}
  • In this modification, the rows except for the empty rows are retrieved from "B12:W44" in the source sheet.在这个修改中,除了空行之外的行是从源工作表中的“B12:W44”中检索的。 And, those rows are copied to the destination sheet.并且,这些行被复制到目标工作表。

  • When Spreadsheet service (SpreadsheetApp) is used, each row is copied using a loop.使用电子表格服务(SpreadsheetApp)时,每一行都使用循环进行复制。 In this case, I thought that the process cost will become high.在这种情况下,我认为过程成本会变高。 So, in this answer, I proposed to use Sheets API.因此,在这个答案中,我建议使用 Sheets API。

References:参考:

暂无
暂无

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

相关问题 复制一个范围并仅将带有值的行粘贴到下一个空白行的另一个工作表中? - Copy a range and paste only rows with values into another sheet at the next blank row? 从范围复制单元格值并将其粘贴到另一个工作表的单行中 - To copy Cell values from a range and paste it in a single row of another sheet 需要将另一张纸中的格式和值都复制到空白的下一行 - Need to copy both the format and values in another sheet to the next row that is blank 从母版工作表的最后一行复制数据,并将其粘贴到Google工作表中另一工作表的特定单元格 - Copy data from last row of master sheet and paste it in specific cells in another sheet in google sheets Google Sheets 将行复制并粘贴到另一个工作表中,然后从第一个工作表中删除内容 - Google Sheets copy and paste row into another sheet and then delete content from the first sheet 用于将范围复制到另一张工作表的 Google 表格脚本 - Google Sheets script to copy a range to another sheet 在值复制/粘贴或插入/编辑谷歌表格中的整行时插入时间戳,仅插入空白单元格 - Insert timestamp when value copy/paste or inserts/edits whole row in google sheets, only insert to blank cell 如何使用 Google Sheets Macros 从一张工作表复制值并将它们粘贴到另一张工作表中? - How to copy values from one sheet and paste them into another using Google Sheets Macros? 用于在最后一条记录后复制并粘贴到另一个工作表的 Google Sheets 脚本 - Google Sheets script to copy and paste in another sheet after the last record 如何复制值并将其粘贴到另一张纸上的下一个可用行中? - How do I copy values and paste them into the next available rows on another sheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM