如何将我的所有数据(文件和文件夹)转换为 JSON 文件
How to convert all my data (files and folders) into a JSON file
使用 Apps 脚本我想知道如何转换我的所有数据,即我的所有文件和文件夹,并将其转换为 json 文件。
从你的问题中不清楚你想对文件和文件夹做什么,所以作为初学者,如果你想记录文件和文件夹的名称,请以 JSON 格式列出它们,然后使用以下代码return 云端硬盘中文件和文件夹的名称。
var filesArr = [];
var foldersArr = [];
var filesFoldersObj = {};
function doGet(request){
var myJSON = getFilesFoldersToJSON();
// return JSON text with the appropriate Media Type
return ContentService.createTextOutput(myJSON).setMimeType(ContentService.MimeType.JSON);
}
function getFilesFoldersToJSON() {
// Get the name of every file in Drive.
var files = DriveApp.getFiles();
while (files.hasNext()) {
var file = files.next();
filesArr.push(file.getName());
}
// create json string of file names
var filesJSON = JSON.stringify(filesArr);
// Get the name of every folder in Drive.
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
foldersArr.push(folder.getName());
}
// create json string of folder names
var foldersJSON = JSON.stringify(foldersArr);
filesFoldersObj['files'] = filesArr;
filesFoldersObj['folders'] = foldersArr;
Logger.log(filesFoldersObj);
var myJSON = JSON.stringify(filesFoldersObj);
// log myJSON to check it - remove or comment out after checking...
Logger.log(myJSON);
return myJSON;
}
产生这样的JSON...
{
"files": [
"Files and Folders to JSON",
"Test File 6",
"Test File 5",
"PDF File 6.pdf",
"PDF File 5.pdf",
"PDF File 4.pdf",
"PDF File 3.pdf",
"PDF File 2.pdf",
"PDF File 1.pdf",
"Test File 1",
"Test File 2",
"Test File 3",
"Test File 4",
"Getting started"
],
"folders": [
"Test Folder 6",
"Test Folder 5",
"Test Folder 2",
"Test Folder 1",
"Test Folder 4",
"Test Folder 3"
]
}
使用 Apps 脚本我想知道如何转换我的所有数据,即我的所有文件和文件夹,并将其转换为 json 文件。
从你的问题中不清楚你想对文件和文件夹做什么,所以作为初学者,如果你想记录文件和文件夹的名称,请以 JSON 格式列出它们,然后使用以下代码return 云端硬盘中文件和文件夹的名称。
var filesArr = [];
var foldersArr = [];
var filesFoldersObj = {};
function doGet(request){
var myJSON = getFilesFoldersToJSON();
// return JSON text with the appropriate Media Type
return ContentService.createTextOutput(myJSON).setMimeType(ContentService.MimeType.JSON);
}
function getFilesFoldersToJSON() {
// Get the name of every file in Drive.
var files = DriveApp.getFiles();
while (files.hasNext()) {
var file = files.next();
filesArr.push(file.getName());
}
// create json string of file names
var filesJSON = JSON.stringify(filesArr);
// Get the name of every folder in Drive.
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
foldersArr.push(folder.getName());
}
// create json string of folder names
var foldersJSON = JSON.stringify(foldersArr);
filesFoldersObj['files'] = filesArr;
filesFoldersObj['folders'] = foldersArr;
Logger.log(filesFoldersObj);
var myJSON = JSON.stringify(filesFoldersObj);
// log myJSON to check it - remove or comment out after checking...
Logger.log(myJSON);
return myJSON;
}
产生这样的JSON...
{
"files": [
"Files and Folders to JSON",
"Test File 6",
"Test File 5",
"PDF File 6.pdf",
"PDF File 5.pdf",
"PDF File 4.pdf",
"PDF File 3.pdf",
"PDF File 2.pdf",
"PDF File 1.pdf",
"Test File 1",
"Test File 2",
"Test File 3",
"Test File 4",
"Getting started"
],
"folders": [
"Test Folder 6",
"Test Folder 5",
"Test Folder 2",
"Test Folder 1",
"Test Folder 4",
"Test Folder 3"
]
}