Reputation: 1585
I am trying to run a google apps script that copies the current script to another folder but for some reason getId()
after the copy function duplicateactivesheet
is not allowing me to access the new sheets id.
TypeError: newSheet.getId is not a function copySheetToFolder @ Code.gs:29
I get a Type error after running:
function copySheetToFolder(folderId) {
// Get the sheet to be copied
//var sheet = SpreadsheetApp.getActiveSheet();
sheet = SpreadsheetApp.getActiveSpreadsheet();
// Make a copy of the sheet
var newSheet = sheet.duplicateActiveSheet();
// Get the ID of the new sheet
var newSheetId = newSheet.getId();
// Get the folder where the sheet will be copied
var folder = DriveApp.getFolderById(folderId);
// Add the sheet to the folder
folder.addFile(DriveApp.getFileById(newSheetId));
}
Upvotes: 2
Views: 111
Reputation: 201513
From your provided official document, duplicateActiveSheet()
is the method of Class Spreadsheet. But, in your script, sheet
of var sheet = SpreadsheetApp.getActiveSheet()
is Class Sheet. I think that this is the reason for your current issue of TypeError: SpreadsheetApp.getActiveSheet(...).duplicateActiveSheet is not a function
.
But, from a google apps script that copies the current script to another folder
, I thought that you might have wanted to copy the active spreadsheet to the specific folder. If my understanding is correct, how about the following modification?
function copySheetToFolder(folderId) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
DriveApp.getFileById(ss.getId()).makeCopy(DriveApp.getFolderById(folderId));
}
By this modification, the active Spreadsheet is copied to the specific folder of folderId
.
If you want to give a new title of Spreadsheet, please use DriveApp.getFileById(ss.getId()).makeCopy("new name", DriveApp.getFolderById(folderId))
.
If you want to use duplicateActiveSheet()
, please use the following modification.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var newSheet = ss.duplicateActiveSheet();
Now, I noticed that you updated your question after I posted an answer. About your new issue of TypeError: newSheet.getId is not a function
, I think that the reason of your new issue is due to that you are using the sheet ID as the spreadsheet ID. If you want to move the Spreadsheet to the specific folder after you copied the active sheet, please modify as follows.
function copySheetToFolder(folderId) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.duplicateActiveSheet();
var ssId = sheet.getId();
var folder = DriveApp.getFolderById(folderId);
DriveApp.getFileById(ssId).moveTo(folder);
}
From your showing script, in this modification, the active sheet is copied and the active Spreasheet is moved to the specific folder.
In the current stage, addFile
of Class Folder has been deprecated. Please be careful about this. Ref In this modification, moveTo
is used. Ref
Upvotes: 2