簡體   English   中英

如何將數據從 Google 工作表發送到 Firestore Cloud?

[英]How to send data from Google sheets to Firestore Cloud?

我一直在嘗試將數據從 google 電子表格發送到我的 Firestore 數據庫(使用這個),但結果很奇怪。 看看下面:

我的腳本 - 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 數據庫時,它會創建一個“地圖”。

怎么了?

謝謝

問題可能與此有關(它創建了一個數組):

 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]
   });

我有同樣的問題,只需像這樣修改你的最后一行......

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM