当前位置:首页 > 未分类 > 正文内容

Copy Google Spreadsheet Data to another Sheet with Apps Script

ceacer3周前 (05-02)未分类3317

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);
}

相关文章

The Best Google Add

The availability of third-party add-ons for Google Docs, Sheets and Google Slides have certainly mad...

How to Create Personalized Images in Bulk with Google Sheets

How to Create Personalized Images in Bulk with Google Sheets

Yesterday marked Friendship Day, and to celebrate, I sent a personalized image to each of my friends...

How to Transcribe Audio and Video Attachments in Gmail

How to Transcribe Audio and Video Attachments in Gmail

The Save Gmail to Google Drive add-on lets you automatically download email messages and file attach...

Find Free Udemy Courses with Google Sheets and the Udemy API

Find Free Udemy Courses with Google Sheets and the Udemy API

Whether you are looking to learn a programming language, enhance your Microsoft Excel skills, or acq...

How to Send SMS Messages with Google Sheets and Android Phone

How to Send SMS Messages with Google Sheets and Android Phone

The Mail Merge for Gmail add-on lets you send personalized emails via Gmail but wouldn’t it be nice...

How to Extract URLs from HYPERLINK Function in Google Sheets

The HYPERLINK formula of Google Sheets lets you insert hyperlinks into your spreadsheets. The functi...