我有以下代碼,它從Firebase數據庫中提取數據並填充Google表單文檔中的數據。 該過程每行需要大約1秒,並且有1000行。Google Apps腳本運行速度很慢
我的代碼效率不是很高,還是有更好的方法在谷歌表格上逐行填寫信息?
function getAllData() {
var firebaseUrl = "url-here";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
var data = base.getData();
var iterator = 0
var obje = []
//Logger.log(data)
for(var i in data) {
iterator++;
if (iterator > 5) {
break
}
if (data[i] !== null) {
Logger.log(data[i]);
obje.push(data[i])
}
}
var range = SpreadsheetApp.getActiveSheet().getRange("A5:K5");
range.setBackground("red");
//Set Column Names
SpreadsheetApp.getActiveSheet().getRange('A5').setValue('Key');
SpreadsheetApp.getActiveSheet().getRange('B5').setValue('Time');
SpreadsheetApp.getActiveSheet().getRange('C5').setValue('Location');
SpreadsheetApp.getActiveSheet().getRange('D5').setValue('Participant_1');
SpreadsheetApp.getActiveSheet().getRange('E5').setValue('Participant_2');
SpreadsheetApp.getActiveSheet().getRange('F5').setValue('Category');
SpreadsheetApp.getActiveSheet().getRange('G5').setValue('team1Score');
SpreadsheetApp.getActiveSheet().getRange('H5').setValue('team2Score');
SpreadsheetApp.getActiveSheet().getRange('I5').setValue('gameDetails');
var number = 5
Logger.log(obje.length);
for (var i in obje) {
Logger.log('Hello' + i)
number++
if (i % 2 === 0) {
var range = SpreadsheetApp.getActiveSheet().getRange("A" + number + ":K" + number);
range.setBackground("lightblue");
}
else {
var range = SpreadsheetApp.getActiveSheet().getRange("A" + number + ":K" + number);
range.setBackground("lightgreen");
}
//Populate Row Data
SpreadsheetApp.getActiveSheet().getRange('A' + number).setValue(obje[i].Key);
SpreadsheetApp.getActiveSheet().getRange('B' + number).setValue(obje[i].Time);
SpreadsheetApp.getActiveSheet().getRange('C' + number).setValue(obje[i].Location);
SpreadsheetApp.getActiveSheet().getRange('D'+ number).setValue(obje[i].Participant_1);
SpreadsheetApp.getActiveSheet().getRange('E'+ number).setValue(obje[i].Participant_2);
SpreadsheetApp.getActiveSheet().getRange('F'+ number).setValue(obje[i].Category);
SpreadsheetApp.getActiveSheet().getRange('G'+ number).setValue(obje[i].team1Score);
SpreadsheetApp.getActiveSheet().getRange('H'+ number).setValue(obje[i].team2Score);
SpreadsheetApp.getActiveSheet().getRange('I'+ number).setValue(obje[i].gameDetails);
}
}
getRange的每個調用都會讀取並在您的情況下寫回文件。你需要儘量減少這些電話。請參閱[優化電子表格操作](http://googleappsscript.blogspot.com/2010/06/optimizing-spreadsheet-operations.html)上的Googls Apps Script博客文章 –