firebase - 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();
}
解决方案
我相信你的目标如下。
修改点:
- 在这种情况下,我想提出以下流程。
- 从电子表格中检索值。
- 创建一个对象以发送到 Firestore。
- 将对象发送到 Firestore。
当上述流程反映到示例脚本时,它变成如下。
修改后的脚本:
在使用此脚本之前,请先设置 的spreadsheetId
、sheetName
和、 以及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);
结果:
运行此脚本时,可以得到以下情况。
从: 至笔记:
- 在此示例脚本中,使用了我在上面从您更新的问题中向您展示的示例电子表格。因此,当您的实际情况与其不同时,脚本可能无法使用。请注意这一点。
- 在您的示例电子表格中,我了解到的标题
tags
是Tags 0
,Tags 1
和Tags 2
. 此脚本用于标题。请注意这一点。如果您的实际情况不正确,请修改脚本。 - 在此示例脚本中,它假设您已经能够获取值并将值放入 Firestore。请注意这一点。
- 请使用此脚本启用 V8 运行时。
参考:
添加:
关于您在回复中提出的以下要求,
- 我希望电子表格中的每一行都是集合中自己的唯一文档。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
被使用?不幸的是,从您的回复中,我无法理解这一点。所以我只是修改了我提议的脚本。所以请根据您的实际情况修改变量名。
推荐阅读
- python - 如何在 python 中使用 selenium 替换 ID?
- java - 处理 Lambda 表达式中的空值
- python - 将三位数和四位数整数转换为日期
- google-ads-api - 如何在 AdWords API 中更改广告系列时更新数组值
- python - 发布帖子请求时无法获得所需的响应
- java - BorderPane 重叠内容
- python - 如何在元素不可点击错误后重试循环并继续脚本?现在脚本刚刚停止
- amazon-web-services - us-gov-west-1 中的 EC2 实例获取“请求中包含的安全令牌无效”
- javascript - 如何在Javascript中的两个间隔之间切换
- c++ - 矢量化图像处理