Home > OS >  Copy specific data from spreadsheet to another
Copy specific data from spreadsheet to another

Time:12-27

I Have this code to copy all sheet data from one spreadsheet to another

    function importData() {
  var ss1 = SpreadsheetApp.openById("1_cujCKj2Xd-JT-bCu2-xSv2gij6qpQXzFtNQ6BY").getSheetByName("Sheet1").getDataRange();
  var ss2 = SpreadsheetApp.openById("1N1tMU3ydG1poly4RDekdDGzlmsyRYLPn3LsfWmWg").getSheetByName("Sheet5");
  
  var toRange = ss2.getRange(1, 1, ss1.getNumRows(), ss1.getNumColumns())
  toRange.setValues(ss1.getValues()); 
}

But what i need to copy specific data range instead of copying all the sheet. Can anyone help me so i can copy range like ("A1:E10") from the source spreadsheet to the cell ("B1") in the target sheet

CodePudding user response:

In your situation, how about the following modification?

Modified script:

function importData() {
  var srcRange = "Sheet1!A1:E10"; // Please set the source range as A1Notation.
  var dstRange = "Sheet5!B1"; // Please set the destination range as A1Notation.

  var ss1 = SpreadsheetApp.openById("1_cujCKj2Xd-JT-bCu2-xSv2gij6qpQXzFtNQ6BY").getRange(srcRange);
  var ss2 = SpreadsheetApp.openById("1N1tMU3ydG1poly4RDekdDGzlmsyRYLPn3LsfWmWg").getRange(dstRange);
  var srcValues = ss1.getValues();
  ss2.offset(0, 0, srcValues.length, srcValues[0].length).setValues(srcValues);
}
  • When this script is run, the values are retrieved from "Sheet1!A1:E10", and the retrieved values are put to "Sheet5!B1".
  • Related