简体   繁体   中英

Copy range of cells to another sheet

I have two tables on two sheets, I need to copy three chosen cells from first table and paste it in the table on the second sheet.

I recorded some actions and edited something there. So it copies range B9:D9 and pastes it into next empty row(var vv) on second sheet. The best way I see it works is I choose one cell (for example B10) and it takes range B10:D10 and paste it in another sheet, I just can't find the info how to make this kind of copypaste.

function copypaste() {
  var vv = SpreadsheetApp.getActive().getSheetByName("Sheet2").getRange('M1').getValue();
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getCurrentCell().activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'), true);
  spreadsheet.getRange(vv).activate();
  spreadsheet.getRange('\'Sheet1\'$B9:D9').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
  spreadsheet.getActiveRangeList().setBackground(null);
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet1'), true);
};

Try this:

function copypaste() {
  const ss = SpreadsheetApp.getActive();
  const sh2 = ss.getSheetByName("Sheet2");
  const vs = sh2.getRange('B9:D9').getValues();
  const sh1 = ss.getSheetByName("Sheet1");
  sh1.getRange(sh1.getLastRow() + 1 , 2, vs.length,vs[0].length).setValues(vs);
}

Takes the values from Sheet2:B9:D9 and pastes them into Sheet1 next empty row column B to D

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