我正在撰寫一個程式,該程式從大型 Google 表格中獲取資料,對其進行處理和操作,并與同一檔案中的其他表格建立連接。為了改善我的運行時間并制作備份資料庫。
我想將資料存盤到JSON我可以在 Google Apps 腳本中輕松使用的某個檔案中(我相信 Google Drive 是最好的免費選項)。我該如何做到這一點?
如果您能顯示將以下檔案保存到 Google Drive 然后將其讀回腳本的確切代碼,我將不勝感激。
let listA = [{id: 34, performance: 200, supervisor: 'A', name: 'Aethelwich'},
{id: 35, performance: 300, supervisor: 'B', name: 'Aethelram'},
{id: 36, performance: 400, supervisor: 'A', name: 'Aethelham'},
{id: 41, performance: 500, supervisor: 'A', name: 'Aethelfred'}];
let jsonObject = JSON.stringify(listA);
// 1. code to save this jsonObject to Google Drive
// 2. code to retrieve the same saved JSON file back from google drive
uj5u.com熱心網友回復:
為了實作您的目標,下面的示例腳本怎么樣?
示例腳本:
let listA = [{ id: 34, performance: 200, supervisor: 'A', name: 'Aethelwich' },
{ id: 35, performance: 300, supervisor: 'B', name: 'Aethelram' },
{ id: 36, performance: 400, supervisor: 'A', name: 'Aethelham' },
{ id: 41, performance: 500, supervisor: 'A', name: 'Aethelfred' }];
let jsonObject = JSON.stringify(listA);
// Save the data as a file.
const file = DriveApp.createFile("sample.txt", jsonObject); // Or, if you want to put the file to the specific folder, please use DriveApp.getFolderById("folderID").createFile("sample.txt", jsonObject)
const fileId = file.getId();
console.log(fileId)
// Retrieve the data from the file.
const text = DriveApp.getFileById(fileId).getBlob().getDataAsString(); // please set the file ID of the saved file.
const array = JSON.parse(text);
console.log(array)
參考:
- 創建檔案(名稱,內容)
- getFileById(id)
- getDataAsString()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/448546.html
上一篇:將字典的鍵名提取到串列中|蟒蛇|
下一篇:復制JSON密鑰
