How to Save a JSON file in Google Apps Script?

岁酱吖の 提交于 2020-02-24 12:56:31

问题


I have a neural network script that requires training data from a JSON file, is it possible to save a json file on google app scripts?


回答1:


You can use the Advanced Drive Service, which must be enabled.
From the Apps Script code editor:

  • "Resources" menu
  • "Advanced Google Services"
  • Find "Drive API" in the list
  • Click the button to turn it ON

In the past, you needed to also enable the Drive API in the Google Cloud Platform. But now you have two choices.

  • Use the "default" Google Cloud project - You don't need to explicitly enable the Drive API in the Google Cloud Platform.
  • Create a new "standard" Google Cloud project and associate it with your Apps Script project. You will need to enable the Drive API in the Google Cloud Platform.

If the Apps Script project is going to be something used by the general public, then you should create a "standard" Google Cloud project, and enable the Google Drive API in the Google Cloud Platform.

Code:

function saveAsJSON() {
  var blob,file,fileSets,obj;

  obj = {//Object literal for testing purposes
    key:"value"
  }

/**
 * Creates a file in the users Google Drive
 */

  fileSets = {
    title: 'AAA_Test.json',
    mimeType: 'application/json'
  };

  blob = Utilities.newBlob(JSON.stringify(obj), "application/vnd.google-apps.script+json");
  file = Drive.Files.insert(fileSets, blob);
  Logger.log('ID: %s, File size (bytes): %s, type: %s', file.id, file.fileSize, file.mimeType);

}


来源:https://stackoverflow.com/questions/52777524/how-to-save-a-json-file-in-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!