I have a neural network script that requires training data from a JSON file, is it possible to save a JSON file to Google Drive using Google Apps Script?
You can use the Advanced Drive Service. There are 3 options for creating files in Google Drive.
- DriveApp - Built-in Apps Script service
- Advanced Drive Service - Specific to Apps Script - Must be added.
- Drive API - Must use the REST API option
Currently Apps Script has both the legacy and new code editor. Enabling the Advanced Drive Service is different in both editors.
Legacy Editor:
- "Resources" menu
- "Advanced Google Services"
- Find "Drive API" in the list
- Click the button to turn it ON
New Editor:
- Click "Services" in the sidebar on the left
- Find "Drive API" in the list, click it
- Click the "Add" button
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);
}
Drive.Files.insert(filesets, blob);
as you have written and, say, DriveApp.getRootFolder().createFile('AAA_Test.json', JSON.stringify(obj), MimeType.PLAIN_TEXT);
? Is there is a difference if/when one attempts to read the contents of the file? In my tests (and at first glance), the files appear identical in form and function, with one exception: when clicking on the file in GDrive, the "Details" pane will show "Type: Unknown File" for the former and "Type: Text" for the latter. –
Internist filesets
mimetype should be "text/plain"
or MimeType.PLAIN_TEXT
For example: fileSets = {title: "AAA_Test.txt", mimeType: "text/plain"};
–
Anticlerical https://www.googleapis.com/auth/drive.file
–
Anticlerical Expanding @Brandon's comment about DriveApp
to a separate answer:
Simplest solution is:
const obj = { key: "value" };
DriveApp.createFile("Test.json", JSON.stringify(obj));
Can write to an existing subfolder using:
const folder = DriveApp.getFoldersByName("Your_Dir").next();
folder.createFile("Test.json", JSON.stringify(obj));
Or get the exact folder with DriveApp.getFolderById(the_share_id)
.
Setting the mime type with .createFile("Test.json", data, MimeType.PLAIN_TEXT)
makes Drive show the file as "Type=Text" instead of "Unknown File"
.createFile()
returns a File
object with has methods like .getId()
.
© 2022 - 2024 — McMap. All rights reserved.
application/vnd.google-apps.script+json
?MimeType
has no such definition: developers.google.com/apps-script/reference/base/mime-type – Churchwoman