Reputation: 594
I'm duplicating spreadsheets based on a template file with attached appsscript project. Below you can see the basic code. This works perfectly for the spreadsheets, but the name of the appsscript project remains the same as the template file. Which is a problem, as I can't distinguish them anymore. I will have hundreds of duplicates in the end.
Is there a way to set the appsscript project name on duplication?
Thank you in advance!
function copyTemplatev2(filename, sheetID) {
var ss = SpreadsheetApp.openById(sheetID);
//Make a copy of the template file
var copy = DriveApp.getFileById(sheetID).makeCopy()
var documentId = copy.getId();
// Set permissions
copy.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT)
//Rename the copied file
DriveApp.getFileById(documentId).setName(filename);
}
Upvotes: 2
Views: 370
Reputation: 201643
attached appsscript project
of a template file with attached appsscript project
is the container-bound script of Spreadsheet.In this case, how about this answer?
From above situation, I would like to propose the following flow.
By above flow, the GAS project name of container-bound script in the copied Spreadsheet can be renamed.
When above workaround is reflected to your script, it becomes as follows.
Before you run the script, please enable Drive API v3 at Advanced Google services. And please set the variables of spreadsheetId
, newSpreadsheetName
, GASProjectId
, and newGASProjectName
.
function myFunction() {
var spreadsheetId = "###"; // Please set the source Spreadsheet ID.
var newSpreadsheetName = "newName of copied Spreadsheet"; // Please set the new Spreadsheet name.
var GASProjectId = "###"; // Please set the container-bound script ID of the template Spreadsheet.
var newGASProjectName = "newName of copied container-bound script"; // Please set the new GAS project name.
// Rtetirve the original filename of the filename of the container-bound script.
var originalGASProjectName = DriveApp.getFileById(GASProjectId).getName();
// Rename to new project name.
Drive.Files.update({ name: newGASProjectName }, GASProjectId);
//Make a copy of the Spreadsheet and rename it.
var copy = DriveApp.getFileById(spreadsheetId).makeCopy(newSpreadsheetName);
// Set permissions
// If you want to publicly share the copied Spreadsheet, please use the below line.
copy.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
// Rename to original project name.
Drive.Files.update({ name: originalGASProjectName }, GASProjectId);
}
In the current stage, it seems that the container-bound script can also be renamed with DriveApp. So, the above script can be modified as follows. In this case, Drive API is not required to be enabled.
function myFunction() {
var spreadsheetId = "###"; // Please set the source Spreadsheet ID.
var newSpreadsheetName = "newName of copied Spreadsheet"; // Please set the new Spreadsheet name.
var GASProjectId = "###"; // Please set the container-bound script ID of the template Spreadsheet.
var newGASProjectName = "newName of copied container-bound script"; // Please set the new GAS project name.
// Rtetirve the original filename of the filename of the container-bound script.
var originalGASProjectName = DriveApp.getFileById(GASProjectId).getName();
// Rename to new project name.
var orgGASProject = DriveApp.getFileById(GASProjectId);
orgGASProject.setName(newGASProjectName);
//Make a copy of the Spreadsheet and rename it.
var copy = DriveApp.getFileById(spreadsheetId).makeCopy(newSpreadsheetName);
// Set permissions
// If you want to publicly share the copied Spreadsheet, please use the below line.
copy.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
// Rename to original project name.
orgGASProject.setName(originalGASProjectName);
}
Upvotes: 4