你好嗎?
一切都運行良好,我可以使用 Google Apps 腳本上的腳本(如下)將我的 Google 表格同步到 Firebase,但 JSON 的格式不是我所期望的。有沒有辦法得到我需要的方式?查看圖片,我想保留與 Google 表格上相同的格式。謝謝!
Goolge 表格 - 錯誤的 JSON - 正確的 JSON
`
var secret = 'XXXXXXXXXXX'
function getFirebaseUrl(jsonPath) {
/*
We then make a URL builder
This takes in a path, and
returns a URL that updates the data in that path
*/
return (
'https://XXXXXXXXXXXXXXXXX.com/'
jsonPath
'.json?auth='
secret
)
}
function syncMasterSheet(excelData) {
/*
We make a PUT (update) request,
and send a JSON payload
More info on the REST API here : https://firebase.google.com/docs/database/rest/start
*/
var options = {
method: 'put',
contentType: 'application/json',
payload: JSON.stringify(excelData)
}
var fireBaseUrl = getFirebaseUrl('moedas')
/*
We use the UrlFetchApp google scripts module
More info on this here : https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
*/
UrlFetchApp.fetch(fireBaseUrl, options)
}
function startSync() {
//Get the currently active sheet
var sheet = SpreadsheetApp.getActiveSheet()
//Get the number of rows and columns which contain some content
var [rows, columns] = [sheet.getLastRow(), sheet.getLastColumn()]
//Get the data contained in those rows and columns as a 2 dimensional array
var data = sheet.getRange(1, 1, rows, columns).getValues();
var dataObject = {};
//Loop through the rows creating a new object for each one
for(var i=1; i < data.length; i ) {
var dataRow = data[i];
var moedaName = dataRow[0];
var idName = dataRow[1];
var currentDate = dataRow[2];
var currentValue = dataRow[3];
var dailyVar = dataRow[4];
var moedaImg = dataRow[5];
// we then create our first property on our data object dataObject.code-name : { }
dataObject[idName] = {
moedaName:moedaName,
idName:idName,
currentDate:currentDate,
currentValue:currentValue,
dailyVar:dailyVar,
moedaImg:moedaImg,
}}
syncMasterSheet(dataObject)
}
`
我已經嘗試了附加的腳本。我期待獲得格式為 Google 表格的 JSON。
uj5u.com熱心網友回復:
當您使用時,range.getValues()您將獲得 Google 存盤的值,例如,您的格式(如小數精度和日期)未應用。還有另一個函式可以獲取顯示的值:range.getDisplayValues(),檔案:https ://developers.google.com/apps-script/reference/spreadsheet/range#getdisplayvalues
將您的代碼更改為:
var data = sheet.getRange(1, 1, rows, columns).getDisplayValues();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/537865.html
