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