【问题标题】:Google Sheet to Firestore multiple documentsGoogle Sheet 到 Firestore 多个文档
【发布时间】:2019-12-18 15:05:06
【问题描述】:

我正在尝试将数据从 Google 表格写入 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);
}

【问题讨论】:

  • 看起来多次调用firestore.createDocument(...) 应该会创建多个文档。你试过了吗?
  • @FrankvanPuffelen 我没试过,你能指导我或给我一个例子吗?
  • 抱歉,这不是我们为您编写代码的网站。我建议您自己试一试,如果无法正常工作,请回帖。
  • @PacoZevallos 我对 Firebase 一无所知,但由于代码是作为脚本编写的,我希望您能够创建一个循环,其中 1) data 和其他相关信息,例如文档的名称,是根据电子表格中的数据创建的,然后 2) firestore.createDocument(xxx); 创建文档。这个过程有很多先例。起点是您创建(和共享)一个包含您希望使用的数据/测试数据的 Google 电子表格。然后我们可以看一个具体的编码“问题”。
  • @Tedinoz 感谢您的贡献,如果您有任何参考,我将尝试查找有关如何制作循环的信息。

标签: google-apps-script google-sheets google-cloud-firestore


【解决方案1】:

您想知道“如何在一次执行 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 的格式有点不寻常 - 对象数组;我对Create an array of JavaScript objects from an existing array of objectsTurn object into array of objects 中建议的方法进行了修改
  • 我假设(为了方便起见)只有 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 app script - looping through the rows in a spreadsheet

Iterating over an object in a Google Apps script and printing to Google Sheets(特定于 Firebase)

【讨论】:

  • 不要忘记添加库:“libraryId”:“1VUSl4b1r1eoNcRWotZM3e87ygkxvXltOgyDZhixqncz9lQ3MjfT1iKFw”,您缺少一个额外的“}”。它有效。
【解决方案2】:

下面是一个 for in loop 示例,用于在 FireStore 中创建多个文档:

// Note: this Google Apps Script uses an external library as per this page:
// https://github.com/grahamearley/FirestoreGoogleAppsScript

function testMultiWrites() {
  const email = 'sheets@yourprojectid.iam.gserviceaccount.com';
  const key = '-----BEGIN PRIVATE KEY-----\nPRIVATEKEY\n-----END PRIVATE KEY-----\n';
  const projectID = 'yourprojectid';
  var firestore = FirestoreApp.getFirestore(email, key, projectID);
  var collection = "TestMutliWrites";

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

  for (let i in data) {
    try {
      firestore.createDocument(collection + "/", data[i]);
    }
    catch (e) {
      //Console.log("Error: " + e); // << If you print to a browser console
      //Logger.log("Error: " + e); // << If you print to Google Apps Script Logger
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2019-07-20
    • 1970-01-01
    相关资源
    最近更新 更多