Import Google app script project from JSON file

In Google Drive, it's possible to download an app script project as a .json file.

When such file is imported back to a Google Drive it's not properly associated with Google Script editor app.

Is there any way to do it properly?

Answer1:

Importing and exporting of Apps Script files requires the use of the import/export API.

To modify an existing script you will need to have a Oauth2 token with the scope of: https://www.googleapis.com/auth/drive.scripts

For updating a file you will "PUT" the updated JSON to: https://www.googleapis.com/upload/drive/v2/files/{FileId}

The Apps Script file looks like

{ files: [ { name:{fileName}, type:{server_js or html }, source:{source code for this file}, id:{Autogenerated. Omit this key for a new file, or leave value unmodified for an updated file}, }, {...} ] }

To add a file: Add an object to the files array with the keys name, type, source

To modify a file: Modify the values of name, type, or source of the file object but do not modify the id.

When you PUT the file back make sure you put the entire files array with your modifications, not just the new file object.

To make the modification in GAS itself would look like:

var scriptFiles = JSON.parse(downloadedJSONFile); scriptFiles.files.push({"name":fileName,"type":fileType,"source":source}); var url = https://www.e-learn.cn/content/wangluowenzhang/"https://www.googleapis.com/upload/drive/v2/files/"+scriptId; var parameters = { method : 'PUT', headers : {'Authorization': 'Bearer '+ tokenWithProperScope, payload : JSON.stringify(scriptFiles), contentType:'application/vnd.google-apps.script+json', muteHttpExceptions:true}; var response = UrlFetchApp.fetch(url,parameters);

You will get a response code of 200 for a successful change. The response text will include the entire new JSON files with the assigned id to the file you added.

Fine more at: https://developers.google.com/apps-script/import-export

Answer2:

Set the mimetype as application/vnd.google-apps.script

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Import Google app script project from JSON file