【问题标题】:How to get an array of object from firestore in appscript如何从appscript中的firestore获取对象数组
【发布时间】:2020-10-18 18:33:24
【问题描述】:

我在 Firestore 中有一组这种格式的文档。问题数组将包含 10 个问题。

我想获取问题字段的数据:一题一排

我在 appscript 中编写代码来执行此操作

这是我目前的代码(仅适用于一个文档)

function test(){
  const firestore = getFirestore();
  
  var query =  firestore.getDocument("QuestionCollection/test").fields;
  var data =  {};
       
  data.subject = query.subject;
  data.questions= query.questions;
  
  const sheet = SpreadsheetApp.getActiveSheet();
  
  for(i = 0 ; i < 10 (no. of question); i++){
    const row = [data.questions[i].answer, data.questions[i].difficulty];
    sheet.appendRow(row);
  }  
}

错误:

TypeError: Cannot read property 'answer' of undefined

【问题讨论】:

    标签: database firebase google-apps-script google-sheets google-cloud-firestore


    【解决方案1】:

    修改点:

    • 当我在图像中看到您的示例数据时,似乎questions 的数组长度为2。但在for 循环中,循环的结束索引是9。我认为他们会发生这样的错误。
    • 当您要放入“Serial”的值时,需要将要放入的值添加到电子表格中。
    • 在您的脚本中,appendRow 用于循环。在这种情况下,处理成本会变高。

    当以上几点反映到你的脚本中时,它变成如下。

    修改脚本:

    你的 for 循环修改如下。

    从:
    for(i = 0 ; i < 10 (no. of question); i++){
      const row = [data.questions[i].answer, data.questions[i].difficulty];
      sheet.appendRow(row);
    }
    
    到:
    var values = [];
    for (var i = 0; i < data.questions.length; i++) {
      const row = [i + 1, data.questions[i].answer, data.questions[i].difficulty];
      values.push(row);
    }
    sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
    
    • 对于循环的结束索引,使用数组长度。

    参考:

    【讨论】:

    • TypeError: Cannot read property 'length' of undefined (line 66, file "Code") 此错误出现在最后一行,如果我尝试删除最后一行并改用 appendRow,则没有任何反应。该脚本将data.questions.length 视为0。如果我手动给出一个值而不是长度data.questions[i].answer, data.questions[i].difficulty,这些将变为未定义
    • @Nehal 感谢您的回复。根据您的回复,我担心您的 query 可能没有价值。因为如果query 具有您的示例图像中的值,我可以确认该脚本有效。那么我可以问你脚本中query 的值吗?如果可以,请在您的脚本中提供query 的示例值?借此,我想确认一下。
    • Firestore 文档中有值,但 AppScript 中没有发生任何事情
    • 感谢您的回复。不幸的是,我无法理解there is vaues in the Firestore documents but nothing is happening in the AppScript。在您的情况下,您仍然没有从 Cloud Firestore 中检索到值。我的理解正确吗?
    【解决方案2】:

    您不应直接查询.fields 属性(因为您的数据未正确转换)。假设您使用的是 v28+ 的库,您的代码应如下所示:

    function test(){
      const firestore = getFirestore();
      
      const query = firestore.getDocument("QuestionCollection/test").obj;  // Don't use .fields here
      
      const sheet = SpreadsheetApp.getActiveSheet();
      const values = [["Serial", "Answer", "Difficulty"]];  // Init 2D array with Header row
    
      // Loop through each question in the array and extract necessary values to construct Data rows
      for (const question of query.questions){
        values.push([query.questions.indexOf(question) + 1, question.answer, question.difficulty]);
      }
    
      // Replace 1, 1 below with coords of "Serial" header cell
      const range = sheet.getRange(1, 1, values.length, values[0].length);
      range.setValues(values);
      
      // sheet.getRange(subjRow, subjCol).setValue(query.subject);  // Add location for Subject data
    }
    

    我看到您希望“序列”代表“问题编号”,所以我将该列添加到标题行和数据行。

    正如 Tanaike 所提到的,循环写入电子表格会巨大性能下降,因此最好设置一个二维数组值以使用 range.setValues(array2D) 一次性写入。理想情况下,您需要 minimize the calls 到电子表格 API。

    免责声明:我是FirestoreGoogleAppsScript 库的积极贡献者。

    【讨论】:

      猜你喜欢
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 2018-12-15
      • 2021-07-25
      • 1970-01-01
      相关资源
      最近更新 更多