【问题标题】:How to send data from Google sheets to Firestore Cloud?如何将数据从 Google 表格发送到 Firestore Cloud?
【发布时间】:2019-12-31 13:42:04
【问题描述】:

我一直在尝试将数据从 google 电子表格发送到我的 Firestore 数据库(使用此 library),但结果很奇怪。往下看:

我的脚本 - Google Apps 脚本

function firestore() {

   // Firestore setup
   const email = "xxxxxxxxxx";
   const key = "-----BEGIN PRIVATE KEY-----XXXXXXXXXXX-----END PRIVATE KEY-----\n";
   const projectId = "xxxxxx";
   var firestore = FirestoreApp.getFirestore (email, key, projectId);

   // get document data from ther spreadsheet
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheetname = "Orçamento";
   var sheet = ss.getSheetByName(sheetname); 
   // get the last row and column in order to define range
   var sheetLR = sheet.getLastRow(); // get the last row
   var sheetLC = sheet.getLastColumn(); // get the last column

   var dataSR = 2; // the first row of data
   // define the data range
   var sourceRange = sheet.getRange(2,1,sheetLR-dataSR+1,sheetLC);

   // get the data
   var sourceData = sourceRange.getValues();
   // get the number of length of the object in order to establish a loop value
   var sourceLen = sourceData.length;


   // Loop through the rows
   for (var i=0;i<sourceLen;i++){

     var data = [];

     data.push({
      "id": sourceData[i][0],
      "data": sourceData[i][1],
      "acao": sourceData[i][2],
      "categoria": sourceData[i][3],
      "movimentos": sourceData[i][4],
      "descricao": sourceData[i][5]
   });

   firestore.createDocument("orcamento",data);

  }

}

这可行,但 Firestore 中的数据有问题。 往下看:

我的 Firestore

当数据发送到 Firestore 数据库时,它会创建一个“地图”。

怎么了?

谢谢

【问题讨论】:

    标签: javascript firebase google-sheets google-cloud-firestore


    【解决方案1】:

    问题可能在于这个(它创建了一个数组):

     var data = [];
    

    你不妨试试:

      var data = {};
      data.id = sourceData[i][0];
      data.data = sourceData[i][1];
      data.acao = sourceData[i][2];
      data.categoria = sourceData[i][3];
      data.movimentos = sourceData[i][4];
      data.descricao = sourceData[i][5];
    

    ...而不是

         var data = [];
    
         data.push({
          "id": sourceData[i][0],
          "data": sourceData[i][1],
          "acao": sourceData[i][2],
          "categoria": sourceData[i][3],
          "movimentos": sourceData[i][4],
          "descricao": sourceData[i][5]
       });
    

    【讨论】:

      【解决方案2】:

      要解决问题,只需像这样修改最后一行:

      firestore.createDocument("orcamento/" + i ,data);
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 2019-06-25
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      相关资源
      最近更新 更多