Maddy
Maddy

Reputation: 35

Google Apps Script to make copy of a spreadsheet and then populate the cells of the new spreadsheet

I need to use a spreadsheet as template and then populate the cells in the newly created spreadsheet. Below is my code.

var sourceSheet = 'source spreadsheet ID'
var string = ["string"]
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");\
var folder = DriveApp.getFolderById('folderId');
var getFile = DriveApp.getFileById(sourceSheet)
var newFile = (SpreadsheetApp.openById(sourceSheet).getName() + "_" + formattedDate)
getFile.makeCopy(newFile, folder);
var sheet = 'I need to know how to get 'newFile' getFileById()'
sheet.getRange(1, 1).setValue(string);

I need to know how to get the newly created file ID. Please help.

Upvotes: 1

Views: 2433

Answers (1)

user3717023
user3717023

Reputation:

As sudo bangbang wrote, makeCopy returns a file object. Then you can open that file as a spreadsheet:

var file = getFile.makeCopy(newFile, folder);
var ss = SpreadsheetApp.open(file);
var sheet = ss.getSheetByName('some sheet name'); // or ss.getSheets()[0]; to get the first sheet
sheet.getRange(1, 1).setValue(string);

Upvotes: 1

Related Questions