2017-08-14 485 views
0

我有以下代碼,它從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); 
    } 
} 
+2

getRange的每個調用都會讀取並在您的情況下寫回文件。你需要儘量減少這些電話。請參閱[優化電子表格操作](http://googleappsscript.blogspot.com/2010/06/optimizing-spreadsheet-operations.html)上的Googls Apps Script博客文章 –

回答

0

如果您的數據是在2d數組中,那麼這應該工作。 不,這不會工作。我去了解了Firebase Today的一些情況,現在我看到它是一個對象數組,所以我會弄清楚如何完成工作並回來修正它。我猜我必須在時間和地點做一條線。

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教程數據的作品。

+0

謝謝!我想出了另一種方式! –