Google script copy data from one spreadsheet to another

If you keep data in separate Google Sheets, copy a range of data from one spreadsheet to another with the IMPORTRANGE function.

For example, you may track quarterly sales data for a product in a different spreadsheet for each region. To combine all that quarterly sales data, copy the data from each region's spreadsheet into a single spreadsheet using IMPORTRANGE.

Use the IMPORTRANGE function

  1. In Sheets, open a spreadsheet.
  2. In an empty cell, enter =IMPORTRANGE.
  3. In parenthesis, add the following specifications in quotation marks and separated by a comma*:
    • The URL of the spreadsheet in Sheets.
    • The sheet name (optional) and the range of cells to import.

    *Note: If you are in a Spanish-speaking country, use the semicolon as a separator instead of the comma.

  4. Press Enter.
  5. Click Allow access to connect the 2 spreadsheets.

For example:

To import cells A1 through C10 from sheet 1 of the abcd123abcd123 spreadsheet, you enter: =IMPORTRANGE("//docs.google.com/spreadsheets/d/abcd123abcd123", "sheet1!A1:C10")

Was this helpful?

How can we improve it?

The cloneGoogleSheet() function will copy data (all rows and columns, but no formatting styles) from one Google Spreadsheet to any other Google Spreadsheet under the same Google Drive.

You need specify the file IDs of the source and destination Google Spreadsheets as arguments in the formula and also change the source and target sheet names inside the method body.

This function can be invoked via a time-based trigger or run it manually from the Apps Script editor. However, if you would like to keep the two spreadsheet in sync with each other always, you an consider using the IMPORTRANGE() Google formula that automatically imports a range of cells from a specified spreadsheet into the currently selected cell / range /sheet.

// copy data from Google Sheet A to Google Sheet B // Credit: @chrislkeller function cloneGoogleSheet(ssA, ssB) { // source doc var sss = SpreadsheetApp.openById(ssA); // source sheet var ss = sss.getSheetByName('Source spreadsheet'); // Get full range of data var SRange = ss.getDataRange(); // get A1 notation identifying the range var A1Range = SRange.getA1Notation(); // get the data values in range var SData = SRange.getValues(); // target spreadsheet var tss = SpreadsheetApp.openById(ssB); // target sheet var ts = tss.getSheetByName('Target Spreadsheet'); // Clear the Google Sheet before copy ts.clear({ contentsOnly: true }); // set the target range to the values of the source data ts.getRange(A1Range).setValues(SData); }

  • HTTP request
  • Path parameters
  • Request body
    • JSON representation
  • Response body
  • Authorization Scopes
  • Examples
  • Try it!

Copies a single sheet from a spreadsheet to another spreadsheet. Returns the properties of the newly created sheet.

HTTP request

POST //sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/sheets/{sheetId}:copyTo

The URL uses gRPC Transcoding syntax.

Path parameters

Parameters
spreadsheetId

string

The ID of the spreadsheet containing the sheet to copy.

sheetId

integer

The ID of the sheet to copy.

Request body

The request body contains data with the following structure:

JSON representation
{ "destinationSpreadsheetId": string }
Fields
destinationSpreadsheetId

string

The ID of the spreadsheet to copy the sheet to.

Response body

If successful, the response body contains an instance of SheetProperties.

Requires one of the following OAuth scopes:

  • //www.googleapis.com/auth/drive
  • //www.googleapis.com/auth/drive.file
  • //www.googleapis.com/auth/spreadsheets

For more information, see the OAuth 2.0 Overview.

How do I automatically copy data from one Google Sheet to another?

Import data from another spreadsheet.
In Sheets, open a spreadsheet..
In an empty cell, enter =IMPORTRANGE..
In parenthesis, add the following specifications in quotation marks and separated by a comma*: The URL of the spreadsheet in Sheets. ... .
Press Enter..
Click Allow access to connect the 2 spreadsheets..

How do I automatically copy rows from one sheet to another in Google Sheets?

Sync data from one spreadsheet to another.
To start, open up the spreadsheet or tab you want to copy to the new Sheet > copy the sheet's URL..
Make a note of the cells you want to import..
Open the new sheet where you want the data to appear..
In the cell begin to type > =IMPORTRANGE (you'll see the code as you begin to type).

How do I copy a sheet in Google App Script?

Open a Sheets spreadsheet that has a macro and that you have permission to edit. To use a sample spreadsheet, make a copy of the Sample macro spreadsheet. Click Extensions > Apps Script. Under the script ID, click Copy.

Toplist

Latest post

TAGs