0

我有這段腳本。 它通過標準篩選範圍,然後它複製在特定工作表中的尊重標準的值 然後它將刪除原始工作表中的所有符合標準的行。 因此,如果我的範圍包含1000多行,它對我說錯誤:Google應用程序腳本超時。如何減少此腳本的執行時間(Google應用程序腳本超時)

我把我的代碼放在這裏,你能幫我得到一個關於這個腳本的執行時間更好的性能嗎?

function trasferisciDati() { 
var ui = SpreadsheetApp.getUi(); 
var response = ui.prompt('Inserisci il mese dei dati da esportare', '(Esempio: agosto (tutto minuscolo))', ui.ButtonSet.OK_CANCEL); 
var inizioTRASFERISCIVALORI = Utilities.formatDate(new Date(), "CET", "HH:mm:ss.SSS"); 
if (response.getSelectedButton() == ui.Button.OK) { 
//get filtered range and set values to the new range 
var description = ui.prompt('Inserisci una descrizione per questa esportazione', 'apparirà come tag dell\'esportrazione', ui.ButtonSet.OK_CANCEL); 
var sourceData = SpreadsheetApp.openById("1XkYhjdQfgU7mVCR7E8mfZsf292I-cJ16PnpCimnd1v8").getSheetByName("Prova"); 
var destinationData = SpreadsheetApp.openById("1cdXMqqBwgWK5nCQUtAP_TyIIDOHksS7wWvSG4jRu658").getSheetByName("Prova"); 
var lastRow = sourceData.getLastRow(); 
var data = sourceData.getRange(1, 1, lastRow, 1).getValues(); 
var chiave = response.getResponseText(); 
    for(var i=0;i<data.length;i++) 
{ 
if (data[i][0] == chiave) { 
    var filteredRow = sourceData.getRange(i+1,1,1,5).getValues(); 
destinationData.appendRow(filteredRow[0]); 
} 
} 

//number of records of the filtered range 
var lastRow = destinationData.getLastRow(); 
var data = destinationData.getRange(1, 6, lastRow, 1).getValues(); 
var loop = 0 
    for(var i=0;i<data.length;i++) 
{ 
    if(!data[i][0]) 
    { 
    var loop = loop + 1 
    } 
} 
Logger.log(Utilities.formatString('%1.0f', Math.floor(loop))) 
//appendi timestamp al rigo ed eventuale descrizione aggiuntiva inserita dall'utente 
var lastRow = destinationData.getLastRow(); 
var data = destinationData.getRange(1, 6, lastRow, 1).getValues(); 
var timestamp = Utilities.formatDate(new Date(), "CET", "dd/MM/YYYY HH.mm.ss") 
for(var i=0;i<data.length;i++) 
{ 
    if(!data[i][0]) 
    { 
destinationData.getRange(i+1,6).setValue(timestamp) 
destinationData.getRange(i+1,7).setValue(description.getResponseText()) 
    } 
} 
//cancella l'intervallo originale 
var maxRows = sourceData.getMaxRows(); 
var data = sourceData.getRange(1, 1, maxRows, 1).getValues(); 
    for(var i=data.length; i>=0;i--) 
{ 
if (data[i] == chiave) { 
sourceData.deleteRow(i+1) 
    } 
    } 
    var fineTRASFERISCIVALORI = Utilities.formatDate(new Date(), "CET", "HH:mm:ss.SSS"); 
    var inTime=inizioTRASFERISCIVALORI.split(":"); 
    var outTime= fineTRASFERISCIVALORI.split(":"); 
    var hr = outTime[0] - inTime[0]; 
    var min = ((outTime[1] - inTime[1])+hr*60)%60; 
    var sec = ((outTime[2] - inTime[2])+min*60)%60; 
    var duration = Utilities.formatString('%2.0f', Math.floor(hr)) + 'h ' + Utilities.formatString('%2.0f', Math.floor(min)) + 'm ' + Utilities.formatString('%2.0f', sec) + 's'; 
    ui.alert('Trasferite '+ Utilities.formatString('%1.0f', Math.floor(loop)) +' righe in '+ duration, ui.ButtonSet.OK) 
} else if (response.getSelectedButton() == ui.Button.CANCEL) { 
    SpreadsheetApp.getUi().alert('L\'utente ha annullato l\'operazione'); 
} else { 
    SpreadsheetApp.getUi().alert('L\'utente ha chiuso la finestra di dialogo'); 
    } 
} 
+0

您的代碼不完整。請再次粘貼。你錯過了一些括號。 – Cooper

+0

現在沒關係@Cooper –

+0

@Cooper我在你以往的回答中找到了完美的解決方案https://stackoverflow.com/questions/42632622/use-google-apps-script-to-loop-through-the-whole-列/ 42633934#42633934 < - 這一個,那還有什麼?謝謝! –

回答

0

你可以試試這個:

var data = sourceData.getRange(1, 1, lastRow, 5).getValues(); 
    var chiave = response.getResponseText(); 
    for(var i=0;i<data.length;i++) 
    { 
    if (data[i][0] == chiave) 
    { 
     //var filteredRow = sourceData.getRange(i+1,1,1,5).getValues(); 
     destinationData.appendRow(data[i]); 
    } 
    } 

,你可能會考慮你的目標數據同樣的事情。

+0

如果我把中斷,它將只追加1行,但我的過濾器範圍包含1000多行,我希望我的腳本設置所有這些行的值 –

+0

如果我猜測我會說使用資源密集型方法循環中的getValues()可能會導致您的問題。 – mongoose36