首页 > 解决方案 > Firestore 导入 Google 表格数组

问题描述

如何将数组数据格式化为谷歌表格,以便将其导入 Firestore?见下图文档e。我有一个脚本可以将 Google 工作表中的数据直接导入我的 Firestore,但我不知道如何使用如下图所示的字符串项格式化数组。以 Google 表格/数据格式执行此操作的最佳方法是什么?

在此处输入图像描述

// Create a menu item for 'Export to Firestore' 
function onOpen() {
  SpreadsheetApp.getUi().createMenu(' Firebase').addItem('Export to Firestore', 'main').addToUi();
}

function main() {
  // Get the active spreadsheet and its name for the collection name
  var sheet = SpreadsheetApp.getActiveSheet();
  var sheetName = sheet.getName();

 // Get the first row as object properties
 // [ 'comment', 'companyId', 'date', 'email', 'score', 'userId']
 var properties = getProperties(sheet);
  
 // Get the next 100 rows as records
 // [ ['test comment', 'ec24fmJeLA93l5jWPAN0', '1602547200.00', 'bdavis@wftst.com', '3', 'a8waViyRAJWGr2h6Gc3u'] ] 
var records = getRecords(sheet);
  
 // Export to Firestore private key removed for security reasons
  var firestore = FirestoreApp.getFirestore('XXXXXX', 'XXXXXXX', 'XXXXXX'); 

  exportToFirestore(firestore, sheetName, properties, records);
  
  
}

function exportToFirestore(firestore, collectionName, properties, records) {
  records.map(function(record) {
    // record: ['test comment', 'ec24fmJeLA93l5jWPAN0', '1602547200.00', 'bdavis@wftst.com', '3', 'a8waViyRAJWGr2h6Gc3u']
    // props : [ 'comment', 'companyId', 'date', 'email', 'score', 'userId']
    var data = {};
    properties.forEach(function(prop,i) { data[prop] = record[i]; });
    return data; 
  }).forEach(function(data) {
    firestore.createDocument(collectionName, data);
  });
}

function getProperties(sheet) {
  return sheet.getRange(1, 1, 1, 6).getValues()[0]; // [ [ 'comment', 'companyId', 'date', 'email', 'score', 'userId'] ]
}

function getRecords(sheet) {
 return sheet.getRange(2, 1, 1, 6).getValues(); 
}

标签: firebasegoogle-apps-scriptgoogle-sheetsgoogle-cloud-firestoregoogle-sheets-formula

解决方案


我相信你的目标如下。

  • 您有一个如下所示的示例电子表格。

    在此处输入图像描述

  • 您想要检索这些值并将它们放入 Firestore,如下所示。

    在此处输入图像描述

  • 您想使用 Google Apps 脚本来实现这一点。

修改点:

  • 在这种情况下,我想提出以下流程。
    1. 从电子表格中检索值。
    2. 创建一个对象以发送到 Firestore。
    3. 将对象发送到 Firestore。

当上述流程反映到示例脚本时,它变成如下。

修改后的脚本:

在使用此脚本之前,请先设置 的spreadsheetIdsheetName和、 以及email, key, projectId的变量。FirestoreApp.getFirestore(email, key, projectId)collectionName

const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const sheetName = "Sheet1"; // Please set the sheet name.

// 1. Retrieve values from Spreadsheet.
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
const [header, ...data] = sheet.getDataRange().getValues();

// 2. Create an object for sending to Firestore.
const obj = data.reduce((ar, r) => {
  const temp = {};
  r.forEach((c, j) => {
    let key = header[j];
    if (key.includes("Tags")) {
      key = "tags";
      temp[key] = temp[key] ? temp[key].concat(c) : [c];
    } else {
      temp[key] = c;
    }
  });
  ar.push(temp);
  return ar;
}, []);

// 3. Send the object to Firestore.
const firestore = FirestoreApp.getFirestore(email, key, projectId);
firestore.createDocument(collectionName, obj);

结果:

运行此脚本时,可以得到以下情况。

从:

在此处输入图像描述

在此处输入图像描述

笔记:

  • 在此示例脚本中,使用了我在上面从您更新的问题中向您展示的示例电子表格。因此,当您的实际情况与其不同时,脚本可能无法使用。请注意这一点。
  • 在您的示例电子表格中,我了解到的标题tagsTags 0,Tags 1Tags 2. 此脚本用于标题。请注意这一点。如果您的实际情况不正确,请修改脚本。
  • 在此示例脚本中,它假设您已经能够获取值并将值放入 Firestore。请注意这一点。
  • 请使用此脚本启用 V8 运行时。

参考:

添加:

关于您在回复中提出的以下要求,

  1. 我希望电子表格中的每一行都是集合中自己的唯一文档。2)标签是文档中唯一需要在数组中的数据集。其余的只需要是字符串

如何修改上面的脚本如下?

从:

// 3. Send the object to Firestore.
const firestore = FirestoreApp.getFirestore(email, key, projectId);
firestore.createDocument(collectionName, obj);

至:

// 3. Send the object to Firestore.
const firestore = FirestoreApp.getFirestore(email, key, projectId);
obj.forEach(r => firestore.createDocument(collectionName, r));
  • 在你当前的脚本中,collectionName使用的是什么?还是scores被使用?不幸的是,从您的回复中,我无法理解这一点。所以我只是修改了我提议的脚本。所以请根据您的实际情况修改变量名。

推荐阅读