从Google Sheets脚本重新排列Firebase实时数据库中显示的值

卡洛斯·克雷格(Carlos Craig)

我想更改我的值在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数据库的屏幕截图: Firebase数据库的屏幕截图

电子表格的屏幕截图: 电子表格的屏幕截图

抢劫犯

数据将以您发送的格式推送到Firebase数据库中-的返回值为Range.getValues()a Object[][](请参见此处)。

解决方法是将数据整理成所需的格式。根据您的描述,我认为数组的顶层是数字(例如,数组索引)是可以的,但是您希望每个项目的内部都被标记为与列相同。

我不会复制所有代码,只复制需要更改的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)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在 Google Cloud 服务器中触发实时数据库字段(不在 Firebase 的功能中)

来自分类Dev

如何将数据从Google Pub / Sub发送到Firebase实时数据库

来自分类Dev

从 Google Cloud Functions 原子更新实时数据库

来自分类Dev

从 Google Firebase 实时数据库读取 Google 地图标记数据时遇到问题

来自分类Dev

如何检查 firebase 实时数据库中的更改值并使它们显示在 html 表中?

来自分类Dev

显示来自 Firebase 实时数据库的数据

来自分类Dev

Android Google Firebase数据库对象值

来自分类Dev

如何使用数据库中的纬度和经度值在Google地图上显示?

来自分类Dev

如何从Firebase实时数据库中获取数据并将其显示在微调器上

来自分类Dev

Firebase实时数据库不会更新值

来自分类Dev

如何从 Firebase 实时数据库正确检索值?

来自分类Dev

如何从Firebase中的实时数据库获取数据

来自分类Dev

从 Firebase 实时数据库中检索子数据

来自分类Dev

Firebase 实时数据库中的数据插入无限

来自分类Dev

Firebase 实时数据库,从键值对中获取数据

来自分类Dev

无法从 Firebase 实时数据库中检索数据

来自分类Dev

Android Firebase - 从 Firebase 实时数据库中检索特定值

来自分类Dev

Firebase:从实时数据库中检索childByAutoID

来自分类Dev

如何从Firebase实时数据库中删除?

来自分类Dev

Web中的Flutter Firebase实时数据库

来自分类Dev

如何更新Firebase实时数据库中的位置?

来自分类Dev

在实时数据库中存储 Firebase 云消息

来自分类Dev

从 Firebase 实时数据库中检索好友

来自分类Dev

显示实时数据库中的值。得到索引错误

来自分类Dev

如何更新Firebase实时数据库中的现有值?

来自分类Dev

从SQLite数据库中读取Google Map Marker的值

来自分类Dev

在Firestore中检索实时数据库值

来自分类Dev

Firebase实时数据库addValueEventListener

来自分类Dev

Firebase实时数据库-规则

Related 相关文章

  1. 1

    如何在 Google Cloud 服务器中触发实时数据库字段(不在 Firebase 的功能中)

  2. 2

    如何将数据从Google Pub / Sub发送到Firebase实时数据库

  3. 3

    从 Google Cloud Functions 原子更新实时数据库

  4. 4

    从 Google Firebase 实时数据库读取 Google 地图标记数据时遇到问题

  5. 5

    如何检查 firebase 实时数据库中的更改值并使它们显示在 html 表中?

  6. 6

    显示来自 Firebase 实时数据库的数据

  7. 7

    Android Google Firebase数据库对象值

  8. 8

    如何使用数据库中的纬度和经度值在Google地图上显示?

  9. 9

    如何从Firebase实时数据库中获取数据并将其显示在微调器上

  10. 10

    Firebase实时数据库不会更新值

  11. 11

    如何从 Firebase 实时数据库正确检索值?

  12. 12

    如何从Firebase中的实时数据库获取数据

  13. 13

    从 Firebase 实时数据库中检索子数据

  14. 14

    Firebase 实时数据库中的数据插入无限

  15. 15

    Firebase 实时数据库,从键值对中获取数据

  16. 16

    无法从 Firebase 实时数据库中检索数据

  17. 17

    Android Firebase - 从 Firebase 实时数据库中检索特定值

  18. 18

    Firebase:从实时数据库中检索childByAutoID

  19. 19

    如何从Firebase实时数据库中删除?

  20. 20

    Web中的Flutter Firebase实时数据库

  21. 21

    如何更新Firebase实时数据库中的位置?

  22. 22

    在实时数据库中存储 Firebase 云消息

  23. 23

    从 Firebase 实时数据库中检索好友

  24. 24

    显示实时数据库中的值。得到索引错误

  25. 25

    如何更新Firebase实时数据库中的现有值?

  26. 26

    从SQLite数据库中读取Google Map Marker的值

  27. 27

    在Firestore中检索实时数据库值

  28. 28

    Firebase实时数据库addValueEventListener

  29. 29

    Firebase实时数据库-规则

热门标签

归档