从JSON文件导入Google应用脚本项目 [英] Import Google app script project from JSON file

查看:71
本文介绍了从JSON文件导入Google应用脚本项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google云端硬盘中,可以将应用程序脚本项目下载为.json文件.

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

将此类文件导入回Google云端硬盘后,它与Google脚本编辑器应用未正确关联.

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?

推荐答案

导入和导出Apps脚本文件需要使用导入/导出API.

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

要修改现有脚本,您将需要具有Oauth2令牌,其范围为: https: //www.googleapis.com/auth/drive.scripts

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

要更新文件,您将放置"更新后的JSON到: https://www.googleapis.com/upload/drive/v2/files/ {FileId}

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

Apps脚本文件看起来像

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

要修改文件,请执行以下操作: 修改文件对象的名称,类型或源的值,但不要修改id.

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.

要在GAS本身中进行修改,就像这样:

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.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);

您将获得200的成功更改响应码.响应文本将包括整个新的JSON文件,并为您添加的文件分配ID.

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.

更多信息,请访问: https://developers.google.com/apps-script/import-export

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

这篇关于从JSON文件导入Google应用脚本项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆