首页 文章

从JSON文件导入Google App脚本项目

提问于
浏览
2

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

将此类文件导回Google Cloud 端硬盘时,它与Google脚本编辑器应用程序无法正确关联 .

有没有办法正确地做到这一点?

2 回答

  • 0

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

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

    要更新文件,您将"PUT"更新的JSON:https://www.googleapis.com/upload/drive/v2/files/

    Apps脚本文件看起来像

    {
      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},    
          },
          {...}
        ]
    }
    

    添加文件:使用键名称,类型,源将对象添加到files数组

    修改文件:修改文件对象的名称,类型或来源的值,但不要修改id .

    当您将文件返回时,请确保将整个文件数组与您的修改放在一起,而不仅仅是新文件对象 .

    要在GAS中进行修改,看起来像:

    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 .

    精细更多:https://developers.google.com/apps-script/import-export

  • 3

    将mimetype设置为 application/vnd.google-apps.script

相关问题