首页 > 解决方案 > Google Sheet 到 Firestore 多个文档

问题描述

我正在尝试从 Google Sheet 将数据写入 Firestore。目前我正在测试这段代码,它工作正常,但它只为我创建了一个文档。

我的问题是:如何在sendFirestore()函数的一次执行中创建多个文档?

function sendFirestore() {
  const email = "...";
  const key = "...";
  const projectId = "...";

  var firestore = FirestoreApp.getFirestore (email, key, projectId); 

  const data = [
    {
      "id": "proveedor1",
      "name": "direccion1"
    },
    {
      "id": "proveedor2",
      "name": "direccion2"
    },
    {
      "id": "proveedor3",
      "name": "direccion3"
    },

  ];

  firestore.createDocument("proveedores", data);
}

标签: google-apps-scriptgoogle-sheetsgoogle-cloud-firestore

解决方案


您想知道“如何在一次 sendFirestore() 函数的执行中创建多个文档?”

有几种方法可以做到这一点。
例如,您可能会创建几个唯一的实例,并在每个实例之后data执行。firestore.createDocument();这不是特别有效或方便,但它是可能的。

var data01 = ...;
firestore.createDocument("document1",data01);
var data02 = ...;
firestore.createDocument("document2",data02);
var data03 = ...;
firestore.createDocument("document3",data03);

与其编辑脚本,不如将数据输入 Google 电子表格然后检索它会更高效、更方便。这种方法的关键是创建一个循环,在该循环中 i) 检索数据并 ii)createdocument执行。这将使您能够创建任意数量的文档。

在以下示例中,有几点需要注意:

  • 有一个for语句启用循环遍历行
  • 的格式data有点不寻常 - 对象数组;我对从现有对象数组创建 JavaScript 对象数组将对象​​转换为对象数组中建议的方法应用了变体
  • 我假设(为了方便起见)只有 3 个字段,并且这些字段是长期添加的;当然,这在现实生活中是最不可能的。更优雅的解决方案是创建一个嵌套循环,该循环将遍历每一行的列,以便构建data. 这种方法还可以满足每行(即每个文档)上不同数量的数据元素。
  • 就firebase而言,该代码未经测试。

电子表格可能如下所示:

示例电子表格
电子表格屏幕截图


代码输出三个data值,如下所示:
- [{name=abc1_1, id=xyz1_1}, {name=abc1_2, id=xyz1_2}, {name=abc1_3, id=xyz1_3}]
- [{name=abc2_1, id=xyz2_1}, {name=abc2_2, id=xyz2_2}, {name=abc2_3, id=xyz2_3}]
-[{name=abc3_1, id=xyz3_1}, {name=abc3_2, id=xyz3_2}, {name=abc3_3, id=xyz3_3}]


function so5745433303() {

  // Firestore setup
  const email = "...";
  const key = "...";
  const projectId = "...";
  var firestore = FirestoreApp.getFirestore (email, key, projectId); 

  // get document data from ther spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = "firebase_data3";
  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
  // Logger.log("DEBUG: Last row = "+sheetLR+", and Last Column = "+sheetLC);  
  var dataSR = 3; // the first row of data
  // define the data range
  var sourceRange = sheet.getRange(3,1,sheetLR-dataSR+1,sheetLC);
  // Logger.log("DEBUG: the source range is "+sourceRange.getA1Notation());
  // 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;
  // Logger.log("DEBUG: the source data length = "+sourceLen);

  // Loop through the rows
  for (var i=0;i<sourceLen;i++){
    var data = [];
    var title = sourceData[i][0];
    // Logger.log("DEBUG: the document title is "+title);

    // #1 ID and Name
    data.push({
      "id": sourceData[i][1],
      "name": sourceData[i][2]
    });
    // Logger.log("DEBUG: id#1 is "+sourceData[i][1]+", and name#1 = "+sourceData[i][2]); 

    // #2 ID and Name
    data.push({
      "id": sourceData[i][3],
      "name": sourceData[i][4]
    });  
    // Logger.log("DEBUG: id#2 is "+sourceData[i][3]+", and name#2 = "+sourceData[i][4]); 

    // #3 ID and Name
    data.push({
      "id": sourceData[i][5],
      "name": sourceData[i][6]
    });
    // Logger.log("DEBUG: id#3 is "+sourceData[i][5]+", and name#3 = "+sourceData[i][6]); 

    // Logger.log(data);

    firestore.createDocument(title, data);

  }

}

供参考

谷歌应用脚​​本 - 遍历电子表格中的行

迭代 Google Apps 脚本中的对象并打印到 Google 表格(特定于 Firebase)


推荐阅读