2016-04-01 9 views
0

Bir gıda kaydedici hazırladım. Ve [1] Sayfasında bir satır ekleyebilmem ve yeni bir yiyecek kaynağı eklemem için bana zaten izin verilmemesini sağlayan bir komut dosyası oluşturmaya çalışıyorum. Bu kod burada çalışır, ancak yürütmek için çok yavaştır. Her soru arasında yaklaşık bir saniye var. İdamları daha hızlı hale getirmek için herhangi bir olasılık var mı, yoksa tek bir Sorgudaki 4 değeri isteyin mi?Çok yavaş UserInput Google Scripts

// Display a dialog box with a title, message, input field, and "Yes" and "No" buttons. The 
// user can also close the dialog by clicking the close button in its title bar. 
function foodSource(){ 
var ui = SpreadsheetApp.getUi(); 
    var response = ui.prompt('Tilføjelse af madvare', 'Hvilken madvare er det?', ui.ButtonSet.OK); 
    return response.getResponseText(); 
} 
function protein(){ 
var ui = SpreadsheetApp.getUi(); 
    var response = ui.prompt('Mængde pr 100 gram', 'Protein', ui.ButtonSet.OK); 
    return response.getResponseText(); 
} 
function carbonhydrates(){ 
var ui = SpreadsheetApp.getUi(); 
    var response = ui.prompt('Mængde pr 100 gram', 'Kulhydrater', ui.ButtonSet.OK); 
    return response.getResponseText(); 
} 
function fat(){ 
var ui = SpreadsheetApp.getUi(); 
    var response = ui.prompt('Mængde pr 100 gram', 'Fedt', ui.ButtonSet.OK); 
    return response.getResponseText(); 
} 



function addData(){ 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheets()[1]; 

sheet.appendRow([madvare(),100, protein(), carbonhydrates(), fat()]); 
} 

cevap

0

Her bir API çağrısı, bir komut dosyasını hızlandırmak için API çağrılarını azaltmamız gereken bir "çok zaman" alır.

Maruz kalan kodda, her birinin SpreadsheetApp.getUI işlevine sahip olduğu üç işlev vardır. Yürütme süresi, değiştirilmediği için yalnızca bir tane kullanılarak azaltılabilir. yukarıda yapmaya

bir yolu (bu test edilmemiştir) dördüncü birinde bu üç fonksiyonu entegre etmektir:

 
// Display a dialog box with a title, message, input field, and "Yes" and "No" buttons. The 
// user can also close the dialog by clicking the close button in its title bar. 

function addData(){ 

    /* 
    * Get ui only once 
    */ 
    var ui = SpreadsheetApp.getUi(); 

    //Get foodsource 
    var response = ui.prompt('Tilføjelse af madvare', 'Hvilken madvare er det?', ui.ButtonSet.OK); 
    var foodsource = response.getResponseText(); 

    //Get protein 
    var response = ui.prompt('Mængde pr 100 gram', 'Protein', ui.ButtonSet.OK); 
    var protein = response.getResponseText(); 

    // Get carbohydrates 
    var response = ui.prompt('Mængde pr 100 gram', 'Kulhydrater', ui.ButtonSet.OK); 
    var carbonhydrates = response.getResponseText(); 

    //Get fat 
    var response = ui.prompt('Mængde pr 100 gram', 'Fedt', ui.ButtonSet.OK); 
    var fat = response.getResponseText(); 

    //Append a row with the collected data 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheets()[1]; 
    sheet.appendRow([foodsource,100, protein, carbonhydrates, fat]); 
}