Reputation: 31
I have a model of spreadsheet, and I want to automatically duplicate the sheet in this model to a new spreadsheet (not in the same spreadsheet). This new spreadsheet must be automatically opened and saved in my Google Drive. How can I do this?
I started with this code but it doesn't work
function CreateReports()
{
var ssNew = SpreadsheetApp.create(date);
var url = ssNew.getUrl()
var ss = SpreadsheetApp.openByUrl(url);
Logger.log(ssNew.getUrl());
}
Upvotes: 1
Views: 2189
Reputation: 45720
If you want to duplicate the entire spreadsheet, see the example in the documentation for Spreadsheet.copy()
.
This script will:
function copyModel() {
// Get today's date as a string
var date = (new Date()).toDateString();
// Get our model sheet
var source = SpreadsheetApp.getActiveSpreadsheet();
var model = source.getSheetByName("model");
// Create a new spreadsheet on Drive, named by date
var destination = SpreadsheetApp.create(date);
// Copy and rename the model sheet into new spreadsheet
var newSheet = model.copyTo(destination).setName(date);
return newSheet;
}
Upvotes: 2
Reputation: 5645
If the script is running as the end user (which it does automatically from the Script Editor), the any create or copy of a spreadsheet ends up in the Drive without any additional steps.
Its not possible to automatically open a Spreadsheet or any window for that matter. The user will have to click on the URL after the create/copy is made.
So in order to the spreadsheet UI, you have to show the URL from getUrl
in a popup or email it to the user and they can click in.
Upvotes: 2