Are you looking for the solution of this question yet? If you still do, how about this answer? Recently, Google Apps Script API was added. By this, users got to be able to easily manage GAS project files. This sample script uses this API.
In order to use this sample, please do the following installation flow.
Installation :
- Enable Google Apps Script API at API console.
- If you have already opened the script editor, you can access there by this link.
- Retrieve current scopes.
- On script editor, File -> Project properties -> Scopes
- Copy scopes.
- Add a scope of
https://www.googleapis.com/auth/script.projects
and https://www.googleapis.com/auth/script.external_request
to the Manifests file (appsscript.json) of the script editor.
- On script editor, View -> Show manifest file
- Add
"oauthScopes": ["https://www.googleapis.com/auth/script.projects", "https://www.googleapis.com/auth/script.external_request", "### other scopes ###"]
to appsscript.json, and save.
- If your script needs other scopes, please add them here.
- Copy and paste this sample script, and run. And please authorize.
Flow of script
- Retrieve filename of source project.
- Retrieve source project.
- Create new project (bound script) in the Google Docs.
- Update the created new project by retrieved source project.
By these flow, the project (bound script) of source spreadsheet is copied to the destination spreadsheet.
Sample script :
Before launch this sample, please input the project ID of source spreadsheet and the destination spreadsheet ID.
function main() {
var srcProjectId = "### project ID ###"; // Source project ID
var dstGoogleDocsId = "### file ID of Google Docs ###"; // Destination spreadsheet ID
var baseUrl = "https://script.googleapis.com/v1/projects";
var accessToken = ScriptApp.getOAuthToken();
// Retrieve filename of bound-script project.
var srcName = JSON.parse(UrlFetchApp.fetch(baseUrl + "/" + srcProjectId, {
method: "get",
headers: {"Authorization": "Bearer " + accessToken}
}).getContentText()).title;
// Retrieve bound-script project.
var obj = UrlFetchApp.fetch(baseUrl + "/" + srcProjectId + "/content", {
method: "get",
headers: {"Authorization": "Bearer " + accessToken}
}).getContentText();
// Create new bound script and retrieve project ID.
var dstId = JSON.parse(UrlFetchApp.fetch(baseUrl, {
method: "post",
contentType: 'application/json',
headers: {"Authorization": "Bearer " + accessToken},
payload: JSON.stringify({"title": srcName, "parentId": dstGoogleDocsId})
}).getContentText()).scriptId;
// Upload a project to bound-script project.
var res = JSON.parse(UrlFetchApp.fetch(baseUrl + "/" + dstId + "/content", {
method: "put",
contentType: 'application/json',
headers: {"Authorization": "Bearer " + accessToken},
payload: obj
}).getContentText());
}
Note :
- This script copies the source project as a new project. So if there are some bound script projects in the Google Docs, this script does NOT affect the existing script.
- But, if you use this sample sceipt, at first, please test using a new project and new Google Docs. By this, please understand the work of this script.
References :
If I misunderstand your question, I'm sorry.
Updated: July 4th, 2019
At April 8, 2019, the specification of Google Apps Script Project was changed. Please see the detail at Google Cloud Platform Projects. When you use Google Apps Script API with the GAS project created after At April 8, 2019, please create a new Cloud Platform Project and link the GAS project to the created Cloud Platform Project. (At script editor, Resources --> Cloud Platform project) By this, the above flow can be used.