[英]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.我相信你的目标如下。
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?
从这种情况来看,下面的修改呢?
setValues
is used in a loop, the process cost becomes high.setValues
时,处理成本变高。 Ref 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. When these points are reflected in your script, how about the following modification?当这些点反映在你的脚本中时,下面的修改怎么样?
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.