如何将 FireStore 数据库自动导出到 Google Sheets?

问题描述 投票:0回答:1

我的项目需要将 FireStore 数据库自动导出到 Google Sheets。

有我的Apps脚本功能。

function exportFirestoreToSheets() {
  // Connect to Firestore
  
  const email = "###.gserviceaccount.com";
  const key = "-----BEGIN PRIVATE KEY-----###\n-----END PRIVATE KEY-----\n";
  const projectId = "moked-report";

  const firestore = FirestoreApp.getFirestore(email, key, projectId);
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = ss.getSheetByName("moked reports data"); // Change this to your desired sheet name
  // If the sheet doesn't exist, create it
  if (!sheet) {
    sheet = ss.insertSheet("moked reports data");
  }

  const allDocuments = firestore.getDocuments("reports");
  
  allDocuments.forEach((doc, index) => {
    const data = doc.fields; // Assuming the document structure is represented as fields
    const rowData = Object.values(data);
    // Write data to the next available row in the sheet
    sheet.getRange(index + 1, 1, 1, rowData.length).setValues([rowData]);
  });
}

我收到“执行已完成”,但我没有看到包含数据的 Google 表格文件。 我调试了该函数,该函数从集合中获取数据,但未创建 Google Sheets 文件。

提前致谢。

行数据 所有文档图片

google-sheets google-apps-script google-cloud-firestore
1个回答
0
投票

根据您的以下评论,

allDocuments - Info [ { name: 'projects/moked-reports/databases/(default)/documents/reports/3QMFQlZcpgpSKKL3tExC', fields: { date: [Object], '机器名称': [Object], '停止原因': [对象], '工作人员名称': [对象], 状态: [对象] }, createTime: '2024-04-23T08:13:43.660107Z', updateTime: '2024-04-23T08:13:43.660107Z ', 读取时间: '2024-04-27T13:06:04.371477Z' },

rowData - Info [ { timestampValue: '2024-04-23T08:13:42.382Z' }, { stringValue: 'V414' }, { stringValue: 'חלק מבйקורת' }, { stringValue: 'eyal' }, { stringValue: ‘工作’}]

从这些评论中,我猜测

allDocuments[0].fields
可能如下。

{
  "date": { "timestampValue": "2024-04-23T08:13:42.382Z" },
  "machine name": { "stringValue": "V414" },
  "reason for stop": { "stringValue": "חלק מביקורת" },
  "worker name": { "stringValue": "eyal" },
  "status": { "stringValue": "working" }
}

在您的显示脚本中,

rowData
sheet.getRange(index + 1, 1, 1, rowData.length).setValues([rowData]);
[{"timestampValue":"2024-04-23T08:13:42.382Z"},{"stringValue":"V414"},{"stringValue":"חלק מביקורת"},{"stringValue":"eyal"},{"stringValue":"working"}]
。因此,当运行时,这些值将被放入“模拟报告数据”表的第一行。至少,这些值被放入单元格中。但你说
I get "Execution completed" but I don't see a Google Sheets file with the data.
。在这种情况下,我担心您的实际值可能与您的情况不同。

根据这种情况,通过相信您评论中的

allDocuments
rowData
的值,我提出了一个修改后的脚本。使用您提供的
allDocuments
rowData
值修改后的脚本如下。

修改后的脚本:

function exportFirestoreToSheets() {
  // Connect to Firestore
  
  const email = "###.gserviceaccount.com";
  const key = "-----BEGIN PRIVATE KEY-----###\n-----END PRIVATE KEY-----\n";
  const projectId = "moked-report";

  const firestore = FirestoreApp.getFirestore(email, key, projectId);
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = ss.getSheetByName("moked reports data"); // Change this to your desired sheet name
  // If the sheet doesn't exist, create it
  if (!sheet) {
    sheet = ss.insertSheet("moked reports data");
  }

  const allDocuments = firestore.getDocuments("reports");
  
  // I modified the below script.
  const keys = Object.keys(allDocuments[0].fields);
  const values = allDocuments.map((doc) => keys.map(k => Object.values(doc.fields[k])[0]));
  if (values.length == 0) return;
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
  • 在此修改中,从
    fields
    allDocuments
    创建一个二维数组,并将该数组放入单元格中。

注:

  • 我认为您提供的值是正确的值,因此提出了这个修改后的脚本。如果修改后的脚本不起作用,您可以再次提供
    allDocuments
    的值吗?此时,请在
    console.log(JSON.stringify(allDocuments))
    行后面放置脚本
    const allDocuments = firestore.getDocuments("reports");
    并运行该脚本。并且,请提供 log
    console.log(JSON.stringify(allDocuments))
    的值。这样,我想修改脚本。
© www.soinside.com 2019 - 2024. All rights reserved.