Antor Cha
Antor Cha

Reputation: 83

Problems copying data to another Sheet

I have a sheet with data and I want to copy the values from that sheet to the main sheet.

The data from the Import sheet is obtained by several arrayformulas, so when I copy it to the main sheet, it doesn't copy properly.

How can I copy only the final result of that sheet? (the "visible" part only)

function copyTo() {
  var source = SpreadsheetApp.openById("xxxxxxxxxxxxx"),
      sheet = source.getSheetByName('ImportJSON'),
      destination = SpreadsheetApp.getActiveSpreadsheet(),
      destSheet = destination.getSheetByName('universe'),
      copydSheet = sheet.copyTo(destination);
  copydSheet.getDataRange().copyTo(destSheet.getDataRange(),{contentsOnly:true});
  destination.deleteSheet(copydSheet);
}

Original Sheet

Result after copy

Upvotes: 0

Views: 69

Answers (1)

Daniel H.
Daniel H.

Reputation: 670

Try using the script below:

function CopyDataToNewFile() {
  var sss = SpreadsheetApp.openById('SOURCE'); // sss = source spreadsheet
  var ss = sss.getSheetByName('SOURCE_SHEET'); // ss = source sheet
  //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();

  var tss = SpreadsheetApp.openById('TARGET'); // tss = target spreadsheet
  var ts = tss.getSheetByName('TARGET_SHEET'); // ts = target sheet
  //set the target range to the values of the source data
  ts.getRange(A1Range).setValues(SData);

} 

Just substitute SOURCE, SOURCE_SHEET, TARGET, and TARGET_SHEET to what fits your needs.

Upvotes: 2

Related Questions