[英]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 this case, how about the following modification?在这种情况下,如何进行以下修改?
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.