首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Firestore导入谷歌工作表阵列

Firestore导入谷歌工作表阵列
EN

Stack Overflow用户
提问于 2021-01-10 03:51:55
回答 1查看 532关注 0票数 0

如何将数组数据格式化为google,以便将其导入到消防局?见图

E下文。我有一个脚本可以直接将Google中的数据导入到Firestore中,但我不知道如何用下面的图像这样的字符串项来格式化数组。在Google /数据格式中,最好的方法是什么?

代码语言:javascript
运行
复制
// 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(); 
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-14 06:15:48

我相信你的目标如下。

  • 您有如下所示的示例电子表格。
  • 您希望检索这些值,并按如下方式将它们放到Firestore中。
  • 您希望使用Google脚本来实现这一点。

修改要点:

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

当上面的流反映到示例脚本中时,如下所示。

修改脚本:

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

代码语言:javascript
运行
复制
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 0Tags 1Tags 2。此脚本用于标头。请小心这个。如果您的实际情况不正确,请修改脚本。
  • 在这个示例脚本中,它假设您已经能够获得并将值放入Firestore。请小心这个。
  • 请在启用V8运行时时使用此脚本。

参考文献:

添加:

关于你在回复中的要求,

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

按照以下方式修改上面的脚本如何?

发自:

代码语言:javascript
运行
复制
// 3. Send the object to Firestore.
const firestore = FirestoreApp.getFirestore(email, key, projectId);
firestore.createDocument(collectionName, obj);

至:

代码语言:javascript
运行
复制
// 3. Send the object to Firestore.
const firestore = FirestoreApp.getFirestore(email, key, projectId);
obj.forEach(r => firestore.createDocument(collectionName, r));
  • 在您的当前脚本中,使用了collectionName?还是使用scores?不幸的是,从你的回复中,我无法理解这件事。所以我修改了我的剧本。因此,请根据实际情况修改变量名。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65649973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档