简体   繁体   English

有没有办法提高与 Firebase 连接的我的谷歌表格应用程序脚本的性能?

[英]Is there anyway to increase performance of my google sheet app script connected with Firebase?

I'm trying to connect my Google Sheet to Firebase-RTDB as i want to transfer my data to proper database (due to gigantic amouth of data) and fetch them through google sheet based on first column value.我正在尝试将我的 Google 表格连接到 Firebase-RTDB,因为我想将我的数据传输到适当的数据库(由于大量数据)并根据第一列值通过 google 表格获取它们。

The code works, but the performance is not very satisfying at all.代码有效,但性能一点也不令人满意。 It takes about 20 seconds, even with the If statement catching whether the cell is empty before doing a loop.即使在执行循环之前使用 If 语句捕获单元格是否为空,也需要大约 20 秒。 If i don't do that it will take a whooping 60 seconds just for 80 rows.如果我不这样做,仅80行就需要60秒。 Which is not good at all.这一点都不好。

function getAllData_ID33() {
  //Read sheet
  var ss = SpreadsheetApp.openById('**MY_GOOGLESHEET_ID**');
  let sheet = ss.getSheetByName('Test')
  let startRow = 2

  //Get Range
  let numRow = sheet.getDataRange().getLastRow();
  let lastCols = sheet.getDataRange().getLastColumn();
  let dataRange = sheet.getRange(startRow, 1, numRow, lastCols);
  let celldata = dataRange.getValues();

  //connect_link
  //ID33_Headoffice
  var ID33_HEADOFFICE_DATABASE = "**MY_FIREBASE_LINK**";


      //if first and fourth column is not empty, pass.
      //Otherwise, start the fetching loop.
  for (j = 0; j < celldata.length-1; j++) {
    //row = row[column]
    row = celldata[j];

    let phyid = ID33_HEADOFFICE_DATABASE + "//" + row[0]
    let base = FirebaseApp.getDatabaseByUrl(phyid);
    let dataSet_33headoffice = [base.getData()]; 
    let id33_rows = [], data_33_headofflice;

    if (row[0] != "") {
      if (row[4] == "") {
        for (i = 0; i < dataSet_33headoffice.length; i++) {
      //need Booking ID, Stockout-ID, Branch ID,  Branch To Name, Comment
          data_33_headofflice = dataSet_33headoffice[i];
            //Logger.log([
            //data_33_headofflice['Booking ID'], 
            //data_33_headofflice['Stock Out (ID)'],
            //data_33_headofflice['Branch (ID)'],
            //data_33_headofflice['Branch To (Name)'],
            //data_33_headofflice['Comment']
            //]);
            
            id33_rows.push([
            data_33_headofflice['Booking ID'], 
            data_33_headofflice['Stock Out (ID)'],
            data_33_headofflice['Branch (ID)'],
            data_33_headofflice['Branch To (Name)'],
            data_33_headofflice['Comment']
            ]);      
          let id33dataRange = sheet.getRange(j + 2,2,id33_rows.length,5);
          id33dataRange.setValues(id33_rows); 
        }
      }
    }     
  }
}

Should be noted that i'm not very familliar with Javascript/Google App Script at all.应该注意的是,我对 Javascript/Google App Script 一点也不熟悉。 I don't even know if Firebase is even fit for a task like this.我什至不知道 Firebase 是否适合这样的任务。 So if you do know what's causing it to be slow and how to improve, please explain.因此,如果您确实知道是什么导致它变慢以及如何改进,请解释一下。 Thanks for all the support!感谢所有的支持!

You need to move these two lines out of the loop:您需要将这两行移出循环:

      let id33dataRange = sheet.getRange(j + 2, 2, id33_rows.length,5);
      id33dataRange.setValues(id33_rows); 

Otherwise, you're updating the spreadsheet after every loop iteration.否则,您将在每次循环迭代后更新电子表格。

I believe your goal is as follows.我相信你的目标如下。

  • You want to reduce the process cost of your script.您想降低脚本的处理成本。

From The code works, but the performance is not very satisfying at all. From The code works, but the performance is not very satisfying at all. , I understood that your script worked. ,我知道您的脚本有效。 From this situation, how about the following modification?从这种情况来看,下面的修改呢?

Modification points:修改点:

  • When setValues is used in a loop, the process cost becomes high.在循环中使用setValues时,处理成本变高。 Ref 参考
  • From your showing script, I guessed that dataSet_33headoffice.length might be always 1. Because when dataSet_33headoffice.length is 2, I thought that the row is deviated.从您的显示脚本中,我猜测dataSet_33headoffice.length可能始终为 1。因为当dataSet_33headoffice.length为 2 时,我认为该行有偏差。 But, in your question, you say The code works .但是,在您的问题中,您说The code works So, I guessed that the value might be 1.所以,我猜这个值可能是1。
  • In order to put the values to the discrete rows by reducing the process cost, I would like to propose using Sheets API.为了通过降低流程成本将值放入离散行,我想建议使用表格 API。 By this, by one API call, the values can be put.这样,通过一次 API 调用,可以输入值。

When these points are reflected in your script, how about the following modification?当这些点反映在你的脚本中时,下面的修改怎么样?

Modified script:修改后的脚本:

Before you use this script, please enable Sheets API at Advanced Google services .在使用此脚本之前, 请在 Google 高级服务中启用表格 API

function getAllData_ID33() {
  var spreadsheetId = "###"; // Please set your Spreadsheet ID.
  var sheetName = 'Test'; // Please set your sheet name.
  var ID33_HEADOFFICE_DATABASE = "**MY_FIREBASE_LINK**"; // Please set this value.

  var ss = SpreadsheetApp.openById(spreadsheetId);
  let sheet = ss.getSheetByName(sheetName)
  let startRow = 2
  let numRow = sheet.getDataRange().getLastRow();
  let lastCols = sheet.getDataRange().getLastColumn();
  let dataRange = sheet.getRange(startRow, 1, numRow, lastCols);
  let celldata = dataRange.getValues();
  var data = [];
  for (j = 0; j < celldata.length - 1; j++) {
    row = celldata[j];
    let phyid = ID33_HEADOFFICE_DATABASE + "//" + row[0]
    let base = FirebaseApp.getDatabaseByUrl(phyid);
    let dataSet_33headoffice = [base.getData()];
    let id33_rows = [], data_33_headofflice;
    if (row[0] != "") {
      if (row[4] == "") {
        for (i = 0; i < dataSet_33headoffice.length; i++) {
          data_33_headofflice = dataSet_33headoffice[i];
          id33_rows.push([
            data_33_headofflice['Booking ID'],
            data_33_headofflice['Stock Out (ID)'],
            data_33_headofflice['Branch (ID)'],
            data_33_headofflice['Branch To (Name)'],
            data_33_headofflice['Comment']
          ]);
        }
        if (id33_rows.length > 0) {
          data.push({ range: `'${sheetName}'!B${j + 2}`, values: id33_rows });
        }
      }
    }
  }
  if (data.length == 0) return;
  Sheets.Spreadsheets.Values.batchUpdate({ data, valueInputOption: "USER_ENTERED" }, spreadsheetId);
}
  • When this script is run, the request body is created in the loop.运行此脚本时,将在循环中创建请求正文。 And, the request body is used outside of the loop using Sheets API.并且,请求正文使用表格 API 在循环之外使用。 By this, the process cost can be reduced.由此,可以降低处理成本。

Note:笔记:

  • This sample script supposes that your script worked fine.此示例脚本假定您的脚本运行良好。 Please be careful about this.请注意这一点。

Reference:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase | 谷歌表 | 应用脚本 | 如何将列中的数据而不是行中的数据从 Firebase 保存到 Google 表格? - Firebase | Google Sheet | App Script | How do I save the data in column instead of row from Firebase to Google Sheet? 不确定 firebase 是否连接到我的应用程序并正常工作 - Not sure if firebase is connected to my app and working properly 火力地堡 | 谷歌表 | 应用脚本 | 每次触发函数时如何将值放在新行上? - Firebase | Google Sheet | App Script | How do I put the value on new row everytime the function is triggered? 检测用户是否连接到 firebase 应用程序 - Detect if user is connected to firebase app 使用 firebase SDK 在我的 React 应用程序上进行 Google 分析 - Google analytics on my React app with firebase SDK 在 Google App Script 上安装 Firebase SDK v9? - Install Firebase SDK v9 on Google App Script? SequelizeConnectionRefusedError - 尝试使用 Firebase/Google Cloud SQL 访问我部署的 web 应用程序 - SequelizeConnectionRefusedError - Trying to access my deployed web app with Firebase/Google Cloud SQL 可以在由 Big Query 提供支持的 Google Connected Sheet 列之间插入手动列 - Possibility of inserting manual columns between a Google Connected Sheet columns Powered by Big Query 我如何在 Excel 工作表中导出我的 Firebase 数据 - How I export my Firebase data in Excel Sheet 在 Firebase 应用程序分发中链接到 Google Play 的问题 - Issue Linking to Google Play in Firebase App Distribution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM