0
votes

I've this code in a google spreadsheet which works well when I run it with an interactive execution:

var originalSpreadsheet = SpreadsheetApp.getActive();
var original = originalSpreadsheet.getSheetByName("Sheet1");
var newSpreadsheet = SpreadsheetApp.create("New");
original.copyTo(newSpreadsheet);

But if I run it with a time trigger it creates correctly the "New" spreadsheet, but the copyTo command doesn't copy anything of the original spreadsheet in it.

Have I made some mistake?

I use Mac OS X Lion 10.7.5 and Chrome.

1
This does work for me. You should be looking at 'Copy of Sheet1' in the 'New' sheet. - Chris

1 Answers

0
votes

As mentioned in the comment above the sheet copy has a new name because Sheet1 exists by default when you create the spreadsheet. You can avoid this with a small change in your code. See code below

function test(){
  var originalSpreadsheet = SpreadsheetApp.getActive();
  var original = originalSpreadsheet.getSheetByName("Sheet1");
  var newSpreadsheet = SpreadsheetApp.create("New");
  var defaultSheet = newSpreadsheet.getSheetByName('Sheet1').setName('xxx');
  original.copyTo(newSpreadsheet).setName('Sheet1');
  newSpreadsheet.deleteSheet(defaultSheet);
}

or, more simply, you can also copy the spreadsheet itself directly (if you want to copy the whole content of the original spreadsheet)

function test(){
  var originalSpreadsheet = SpreadsheetApp.getActive();
  var newSpreadsheet = originalSpreadsheet.copy('new')      
}

This option will copy the script as well, the copy is a "clone" of the original.