How to copy a range of data to another google sheet using google apps script?

weizer

Source: enter image description here

Destination: enter image description here

Hi everyone,

I have 2 google sheets, Source & Destination. I want to copy a range of data from Source sheet to Destination sheet by using google apps script. So the data should appear at the last row of the Destination sheet. This is my code:

function copyInfo() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("Source");
  var pasteSheet = ss.getSheetByName("Destination");

  // get source range
  var source = copySheet.getRange(3,1,1,10);
  // get destination range
  var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);

  // copy values to destination range
  source.copyTo(destination);

}

Seems like the script only allow me to do the copying within one google sheet (different tab) instead of 2 different google sheet. May I know how should I modified it so that the script able to find my destination google sheet and paste the data at the last row? Any help will be greatly appreciated!

Edit enter image description here

I tried to use onEdit to triggered the script, however it seems like not working. Below is my script:

function onEdit(e) {

  if (e.range.columnStart == 11 && e.range.rowStart == 3){
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    const id ='1r5Hygl5ysahMi6DQ3duDR5L8c4aGX_0CJba7lXxnejw';
    var copySheet = ss.getSheetByName("Sheet1");
    var pasteSheet = SpreadsheetApp.openById(id).getSheetByName("Sheet1");
    
    if (e.value == 'Submit'){
      var source = copySheet.getRange(3,1,1,10);
      const values = source.getValues();
      var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);
      destination.setValues(values);
    }
  }
}

Source: https://docs.google.com/spreadsheets/d/1jnOvE-Dc7y9o7GsgN9fjOXZWlUCwiJayugX5q4Y3CA0/edit#gid=0

Destination: https://docs.google.com/spreadsheets/d/1r5Hygl5ysahMi6DQ3duDR5L8c4aGX_0CJba7lXxnejw/edit#gid=0

idfurw

Use Range.getValues() and Range.setValues()

const id ='ID of paste Spreadsheet';
var pasteSheet = SpreadsheetApp.openById(id).getSheetByName("Destination");
  var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);
  const values = source.getValues();
  destination.setValues(values);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to Copy a data range from one sheet to another sheet using Google Sheet Script

How to copy a range of selected data to after the lastRow of data in the same Sheet using Google Apps Script?

copy data matches from one sheet to another within a specific row range Google apps script

Copy sheet to another spreadsheet [Google Apps Script]

Google Sheets script to copy a range to another sheet

How to copy multiple range to the exact same location in another sheet using google script?

Google Apps Script to copy range in spreadsheet using sheet reference (not sheet name)

Split data in google sheet to another sheet using google app script

How to copy a specific row from one sheet to another (Google Apps Script)

Duplicate some rows in a sheet range using google apps script

How to copy spreadsheet's sheet into another spreadsheet without creating a new sheet copy when using google sheet script?

How to setFormulas() to a range using Google Apps Script?

How to get the specific range of row data from google sheet through apps script

How to print the data of an array into a Google sheet with Google Apps Script

Move filtered array to another sheet using Google Apps Script

How can I fix the range of a Google Sheet MATCH function when using a move rows apps script?

Google Sheet Apps Script: How do I edit the Apps Script below to only copy over data from Column A to L and P to S?

Compare data in source sheet with target sheet and copy missing rows to target sheet with Google Apps Script

Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells

How to Copy data from One Google Sheet to another Sheet in the Same file Using Sheet Name as Criteria and find the sheet base on filter criteria

Using Google Apps Script to copy .CSV file data to Google Sheets

How to match one column and replace a different (corresponding) column in another sheet using google apps script?

Pivot or Transpose data in a google sheet using apps script

How to import data from another Google Sheet using Google Script without using Spreadsheet ID

How to copy the data to destination range when there is any update in google sheet?

How to copy and edit text data inside a table cell in Google Documents using Google Apps Script

How to extract the pubhtml ID in google sheet using Apps Script?

How to border data using a Google Apps script?

Google Apps Script Copying certain cells from a sheet into another sheet without repeating data