我有以下代码从firebase数据库中提取数据并填写google工作表文档中的数据。 该过程每行大约需要1秒,并且有1000行。
我的代码效率不高,或者是否有更好的方法在Google工作表上逐行填写信息?
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);
}
}
答案 0 :(得分:0)
如果您的数据是二维数组,那么这应该有效。 没有这个不行。我去了解了一下今天的Firebase,我现在看到它是一个对象数组,所以我将弄清楚如何完成工作并回来纠正它。我猜我不得不在时间上做到这一点。
function getAllData()
{
var firebaseUrl = "url-here";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
var data = base.getData();//I assume this is correct I have not used firebase
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var headers=['Key','Time','Locaton','Participant_1','Participant_2','Category','teams1Score','team2Score','gameDetails'];
sh.appendRow(headers);
sh.getRange(2,1,data.length,headers.length).setValues(data);
}
以下是example from another contributor的链接,但我无法解决这个问题。所以我在下面修改了一下:
function writeMySheets() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var firebaseUrl = "https://PROJECTID.firebaseio.com/";
var secret = "SECRET";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl,secret);
var data = base.getData();
var keys = Object.keys(data);
var sheetRow = [];
var entryKeys;
var firstTime=true;
for (index in keys) {
sheetRow = [];
entryKeys = Object.keys(data[keys[index]])
for (i in entryKeys) {
sheetRow.push(data[keys[index]][entryKeys[i]]);
}
//Logger.log(sheetRow);
if(firstTime)
{
sheet.appendRow(entryKeys);//Prints column headers
firstTime=false;
}
sheet.appendRow(sheetRow);
}
}
这似乎适用于我认为我永远无法工作的QuickStart tutorial的教程数据。