如何使用应用程序脚本将 Firestore 中的数据写入 Google Sheet?

How can I write the data in Firestore to Google Sheet with app script?

我的应用程序脚本有问题。我尝试将我的数据添加到 Google Sheet 但我做不到。首先,我从 Firestore 获取数据。它 returns Document 或 Document[] 变量。那么我如何访问这个变量的内部并写入我的 Google Sheet?我没有找到任何相关信息。 这是我的功能:

function myFunction(){

  var firestore = FirestoreApp.getFirestore (email, key, projectId);
  
  const allDocuments = firestore.getDocuments("TEST");
  console.log(allDocuments.length);
}

你能帮帮我吗?我如何读取该数据并写入我的 Google Sheet?

page provides a link to the wiki,其中包含更多信息。

getDocuments() 方法returns 集合中文档的数组。你只需要遍历这个数组并对这个数组中的文档做任何你想做的事情,例如更新 Sheet 个单元格。

例如,下面显示了一种获取每个文档的 field1 字段值的可能方法:

const allDocuments = firestore.getDocuments("TEST");
for (var i = 0; i < allDocuments.length; i++) {    
    var field1Value = allDocuments[i].fields["field1"];       
    // ...
}

根据您的更新,我猜您想要构建一个数组并将其传递给 appendRow()。

以下应该可以解决问题:

  const allDocuments = firestore.getDocuments("TEST");
  var myArray = [];
  for(var i = 0; I < allDocuments.length; i++){
    var chills = allDocuments[i].fields["chills"];
    myArray.push(chills.boolean);
  }
  sheet.appendRow(myArray);