Skip to content

Instantly share code, notes, and snippets.

@soundTricker
Last active September 26, 2016 15:00
Show Gist options
  • Save soundTricker/5829067 to your computer and use it in GitHub Desktop.
Save soundTricker/5829067 to your computer and use it in GitHub Desktop.
A project of GooGoogle Apps Script import / export by Google Apps Script. If u need more detail, please see below. (sorry Japanese) http://qiita.com/soundTricker@github/items/e75ee1f2a7d89fa60a60
//You can create new Google Apps Script by using Google Drive API files.insert endpoint.
var KEY = 'Your API Key';
function createNewGASProject() {
var oauthConfig = UrlFetchApp.addOAuthService('drive');
/*
Scopes given
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.scripts
*/
var scope = 'https://www.googleapis.com/auth/drive+https://www.googleapis.com/auth/drive.file+https://www.googleapis.com/auth/drive.scripts';
oauthConfig.setConsumerKey('anonymous');
oauthConfig.setConsumerSecret('anonymous');
oauthConfig.setRequestTokenUrl('https://www.google.com/accounts/OAuthGetRequestToken?scope='+scope);
oauthConfig.setAuthorizationUrl('https://accounts.google.com/OAuthAuthorizeToken');
oauthConfig.setAccessTokenUrl('https://www.google.com/accounts/OAuthGetAccessToken');
//This structure is same as downloaded json of google apps script project.
var files = {
"files": [
{
"name":"Code",
"type":"server_js",
"source":"function doGet() {\n return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);\n}\n"
},
{
"name":"index.html",
"type":"html",
"source":"<html>\n<body>\n Hello, world!!\n </body>\n</html>"
}
//If you add html type hash object, maybe this will be created a server_js type file.
//Please see this issue. https://code.google.com/p/google-apps-script-issues/issues/detail?id=2916
]
};
//content-type is so important
var postParams = {
method:'post',
oAuthServiceName: 'drive',
oAuthUseToken: 'always',
payload: JSON.stringify(files),
contentType: 'application/vnd.google-apps.script+json'
};
//This code create a new Google Apps Script Project.
//But that filename is 'Untitled'.
//You can do creating and renaming a GAS Project in one request, if you do something.
//But that is bit difficult.
var json = UrlFetchApp.fetch("https://www.googleapis.com/upload/drive/v2/files?convert=true&key=" + KEY,postParams).getContentText();
//Rename GAS Project.
DriveApp.getFileById(JSON.parse(json).id).setName("新しく作った奴");
Logger.log(json);
}
//This is a google apps script project json.
//The Import/Export API, that is on the Google Drive API, use this.
//If u have existing project, u can do update, delete, add and rename project's code and file by changing this json structure.
{
//files property is Google Apps Script Project Files.
//It has some hash objects.
"files": [
//Hash object is Google Apps Script File.
//It contain id, name, type, source.
//You can update a code, if you change only a source property.
{
"id":"9basdfbd-749a-4as9b-b9d1-d64basdf803",
"name":"Code",
"type":"server_js",
"source":"function doGet() {\n return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);//updated\n}\n" //add '//updated' line
},
/* You can delete a file, if you do not contain an existing file hash.
{
"id":"3asf7c0d-1afb-4a9-8431-5asdfc79e7ae",
"name":"index",
"type":"html",
"source":"\u003chtml\u003e\n \u003cbody\u003e\n New message!!\n \u003c/body\u003e\n\u003c/html\u003e"
}*/,
// You can change filename of a file, if you change name property.
{
"id":"9basdfbd-749a-4as9b-b9d1-d64basdf804",
"name":"NewName",
"type":"server_js",
"source":"function doGet() {\n return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);//updated\n}\n"
},
//You can create new file, if you add a new file hash.
{
"id":"9basdfbd-749a-4as9b-b9d1-d64basdf804",
"name":"NewFile",
"type":"server_js",
"source":"function doGet() {\n return HtmlService.createHtmlOutputFromFile(\u0027index\u0027);//updated\n}\n"
}
]
}
//You can update existing GAS Project by using Google Drive API files.update endpoint.
var KEY = 'Your API Key';
function updateExistingGASProject() {
//Donwload GAS Project JSON
//file id. it's in script editor url.
var fileId = "drive file id";
var oauthConfig = UrlFetchApp.addOAuthService('drive2');
/*
Scopes given
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.scripts
*/
var scope = 'https://www.googleapis.com/auth/drive+https://www.googleapis.com/auth/drive.file+https://www.googleapis.com/auth/drive.scripts';
oauthConfig.setConsumerKey('anonymous');
oauthConfig.setConsumerSecret('anonymous');
oauthConfig.setRequestTokenUrl('https://www.google.com/accounts/OAuthGetRequestToken?scope='+scope);
oauthConfig.setAuthorizationUrl('https://accounts.google.com/OAuthAuthorizeToken');
oauthConfig.setAccessTokenUrl('https://www.google.com/accounts/OAuthGetAccessToken');
//UrlFetch Option for downloading JSON of GAS Project.
var downloadOptions = {
method:'get',
oAuthServiceName: 'drive2',
oAuthUseToken: 'always'
};
//Get a GAS Project download url.
var url = DriveApp.getFileById(fileId).getDownloadUrl();
//Downloading...
var json = UrlFetchApp.fetch(url,downloadOptions).getContentText();
//parse as json
var scriptObject = JSON.parse(json);
//changing json property for updating GAS Project.
//Update code of a file.
scriptObject.files[0].source += "\n//updated\nfunction c() {Logger.log('hoge')}";
//Delete file.
scriptObject.files[1] = null;
//Squeeze
scriptObject.files = scriptObject.files.filter(function(file) {return file});
//Rename file.(it is not abode file)
scriptObject.files[1].name += "updated";
//Add a new file.
scriptObject.files.push({
"name":"index",
"type":"html",
"source":"\u003chtml\u003e\n \u003cbody\u003e\n Hello, world!!\n \u003c/body\u003e\n\u003c/html\u003e"
});
//UrlFetch Option for updating.
var updateOptions = {
method:'put',
oAuthServiceName: 'drive2',
oAuthUseToken: 'always',
contentType: 'application/vnd.google-apps.script+json',
payload: JSON.stringify(scriptObject)
};
//Updating...
var uploadResponse = UrlFetchApp.fetch('https://www.googleapis.com/upload/drive/v2/files/' +fileId + "?key=" + KEY, updateOptions);
}
@oshliaer
Copy link

@keremtiryaki , I can't find how to update existing GAS Project by using your app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment