gpt4 book ai didi

firebase - 从谷歌表脚本重新排列 firebase 实时数据库中显示的值

转载 作者:行者123 更新时间:2023-12-04 10:59:55 25 4
gpt4 key购买 nike

我想更改我的值在 firebase 实时数据库中的显示方式。它们显示为数字,我希望显示列的标题。

脚本文本:

var secret = 'xxxx'


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://no-excusas.firebaseio.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('Users')


/*
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()


//Use the syncMasterSheet function defined before to push this data to the "masterSheet" key in the firebase database

syncMasterSheet(data)

}

firebase 数据库截图:
screenshot of firebase database

电子表格截图:
screenshot of spreadsheet

最佳答案

数据以您发送的格式推送到 Firebase 数据库——返回值 Range.getValues()Object[][] (见 here)。

解决方法是将数据转换为您想要的格式。根据您的描述,我认为您对数组的顶层是数字(例如数组索引)没问题,但您希望每个项目的内部标记为与列相同。

我不会复制您的所有代码,只会复制需要更改的 2 个功能。一、startSync应该分别抓取标题行和数据行(假设您不想要数据库中标题行本身的项目):

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.
// Get the headers in a separate array.
var headers = sheet.getRange(1, 1, 1, columns).getValues()[0]; // [0] to unwrap the outer array
var data = sheet.getRange(2, 1, rows - 1, columns).getValues(); // skipping the header row means we need to reduce rows by 1.

//Use the syncMasterSheet function defined before to push this data to the "masterSheet" key in the firebase database

syncMasterSheet(headers, data)
}

二、 syncMasterSheet()应该在执行 PUT 之前构建您想要 PUT 的对象:

function syncMasterSheet(sheetHeaders, sheetData) {
/*
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
*/
const outputData = [];
for(i = 0; i < sheetData.length; i++) {
var row = sheetData[i];
var newRow = {};
for(j = 0; j < row.length; j++) {
newRow[sheetHeaders[j]] = row[j];
}
outputData.push(newRow);
}

var options = {
method: 'put',
contentType: 'application/json',
payload: JSON.stringify(outputData)
}

var fireBaseUrl = getFirebaseUrl('SpreadsheetTest')

/*
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)
}

关于firebase - 从谷歌表脚本重新排列 firebase 实时数据库中显示的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58880947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com