3
votes

I need help when copy sheet to new Spreadsheet. I wanted to paste only values.

I am creating .pdf from a copy of sheet but the CopyTo copy also formula from sheet.

 function topdf() {
  var foldersave = DriveApp.getFolderById('ID');
   var originalSpreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Print_sheet_Damir"); 
  var radni_sheet_Damir = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Radni_sheet_Damir");
  var name = originalSpreadsheet.getRange("C4").getValue();
  var ID = radni_sheet_Damir.getRange("E2").getValue();
 var newSpreadsheet = SpreadsheetApp.create("ID_" + ID + "_" + name);
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var projectname = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Print_sheet_Damir");
  sheet.copyTo((newSpreadsheet), {contentsOnly: true});


 newSpreadsheet.getSheetByName('Sheet1').activate();
  newSpreadsheet.deleteActiveSheet();

pdf = newSpreadsheet.getBlob();
  var file = foldersave.createFile(pdf);

But the following line doesn't work for me:

sheet.copyTo((newSpreadsheet), {contentsOnly: true});
2

2 Answers

2
votes

From this documentation, you can check here on how to properly use the function copyTo(spreadsheet). Here is the sample code.

 var source = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = source.getSheets()[0];

 var destination = SpreadsheetApp.openById('ID_GOES HERE');
 sheet.copyTo(destination);

and the copyTo(destination, options)

 // The code below will copy only the values of the first 5 columns over to the 6th column.
 var sheet = SpreadsheetApp.getActiveSheet();
 sheet.getRange("A:E").copyTo(sheet.getRange("F1"), {contentsOnly:true});
 }

You can also check this tutorial on how to copy Spreadsheet values to a different spreadsheet.

For more information, just check the Google AppsScript documentation for Spreadsheet and this related SO question.

0
votes

You should use getRange() in this line:

sheet.copyTo((newSpreadsheet), {contentsOnly: true});

It should look something like this:

sheet.copyTo(newSpreadsheet.getRange("The range you want to work with"), {contentsOnly: true});