title: Very slow scripts (with weird errors) after a recovery of a previous version of a spreadsheet
body: <p><a href="https://i.stack.imgur.com/mg9k0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mg9k0.png" alt="weir erros" /></a></p>
  <p>I was having problems with a spreadsheet, and I decided to go back to a previous version...</p>
  <p>However, I noticed that after this restoration, the spreadsheet SCRIPTS were SUPER SLOW (often shutting down by timeout) and presenting errors that I've never seen! (like the ones I posted above). It's like the script is having a hard time finding the spreadsheet...</p>
  <p>Does anyone know how I can resolve this? I've tried everything!</p>
  <p>Look at this simple script, which starts quickly, but on the first line it takes 2 MINUTES to perform!!! (Full script after screenshot)</p>
  <p><a href="https://i.stack.imgur.com/jDM70.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jDM70.png" alt="simple script taking 2 minutes to &quot;start&quot;" /></a></p>
  <p>Script:</p>
  <pre><code>    function atualizarDisparos() {
  Logger.log(&quot;step1&quot;)
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('DB'); 
  Logger.log(&quot;step2&quot;)
    
   
    var startRow = 27; // Lembrar de tirar o cabeçalho
    var quantidadeDeViveiros = 16;
    var lastRow = startRow + quantidadeDeViveiros; // figure out what the last row is
  
    Logger.log(&quot;step4&quot;)
  
    var startColumn = 2; // Lembrar de tirar as bordas
    var lastColumn = ss.getLastColumn(); // figure out what the last column is
    var numColumns = lastColumn-startColumn+1; // subtrair um pelo outro
  
    var dataRange = ss.getRange(startRow, startColumn, quantidadeDeViveiros, numColumns)
    var data = dataRange.getValues();
  
    var povoamento =&quot;&quot;;
    var bioensaio =&quot;&quot;;
    var despesca =&quot;&quot;;
    var povoadoHoje =&quot;&quot;;
    var atualizar = 0;
  
    var biomassaDespesca = 2000;
    var tamanhoDespesca = 18;
    var tempoBioensaio = 3;
    var diasParaVenda = 15;
  
  Logger.log(&quot;step4&quot;)
  
    for (var linhaAtual = 0; linhaAtual &lt;= quantidadeDeViveiros-1; linhaAtual++) {
      var row = data[linhaAtual];
      Logger.log(&quot;step5&quot;)
      if (row[7] == 0 || row[35] &lt; diasParaVenda) { //==== POVOAMENTO
        var povoamento = povoamento + &quot;V&quot; + row[0] + &quot; (&quot; + row[12] + &quot;kg de &quot; + row[14] + &quot;g) || &quot;
        var atualizar = 1;
      } 
  
      if (row[7] != 0 &amp;&amp; row[9] == &quot;&quot; &amp;&amp; row[11] &lt;= 3) { //=== BIOENSAIO
        var dataPovoamento = new Date(row[5]);
        var dataBioensaio = dataPovoamento.setDate(dataPovoamento.getDate()+tempoBioensaio-1);
        var dataBioensaio = new Date(dataBioensaio);
        var dataHoje = new Date();
        Logger.log(dataBioensaio)
        Logger.log(dataHoje)      
  
        if (dataHoje &gt;= dataBioensaio) {
          var dataBioensaio = Utilities.formatDate(dataBioensaio, Session.getScriptTimeZone(), 'dd/MM/yy');
          var bioensaio = bioensaio + &quot;V&quot; + row[0] + &quot; (Tirar Bioensaio: &quot; + dataBioensaio + &quot;) || &quot;
        }
        var atualizar = 1;
      } 
  
        if (row[7] != 0 &amp;&amp; row[8] == 0) { //=== POVOADO HOJE
        var dataPovoamento = new Date(row[5]);
        var dataPovoamento = new Date(dataPovoamento);
        var dataPovoamento = Utilities.formatDate(dataPovoamento, Session.getScriptTimeZone(), 'dd/MM/yy');
        var povoadoHoje = povoadoHoje + &quot;V&quot; + row[0] + &quot; (Povoado dia: &quot; + dataPovoamento + &quot;) || &quot;
  
        var atualizar = 1;
      } 
  
      if (row[14] &gt;= 8 &amp;&amp; row[35] &lt; diasParaVenda) { //=== DESPESCA
        var despesca = despesca + &quot;V&quot; + row[0] + &quot; (&quot; + row[12] + &quot;kg de &quot; + row[14] + &quot;g) || &quot;
        var atualizar = 1
      }
    }
  
    if(atualizar == 1) {
      var ssPC = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PC'); 
      ssPC.getRange('k107').setValue(povoamento);
      ssPC.getRange('k108').setValue(bioensaio);
      ssPC.getRange('k109').setValue(despesca);
      ssPC.getRange('k123').setValue(povoadoHoje);
    }
  }
  </code></pre>