简体   繁体   中英

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. There might be 2 or 20 rows of data. 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. 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. About the empty row, in this case, all columns from "B" to "W" are empty cells. 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. So, before you use this script, please enable Sheets API at Advanced Google services .

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. And, those rows are copied to the destination sheet.

  • When Spreadsheet service (SpreadsheetApp) is used, each row is copied using a loop. In this case, I thought that the process cost will become high. So, in this answer, I proposed to use Sheets API.

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM