title: How to reduce time of script execution with FormApp...getResponses()?
body: <p>The Email collection function built into the form is in the Text format. We have a lot of employees filling out this form, so it would be more convenient to choose from a drop-down list than to manually enter an email for each employee. The built-in function of collecting emails does not allow using the Dropdown list question type, so I had to disable it and send the response to the email using a script.</p>
  <p>In addition, when you select one of the options (Transfer) in the form, you need to insert an additional correction row with data. This is also done by the script.</p>
  <p><a href="https://i.stack.imgur.com/XYMwH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XYMwH.png" alt="enter image description here" /></a></p>
  <p>The script works fine, but unfortunately, its execution time reaches 45 seconds. I think this is abnormally large.</p>
  <p><strong>Updated code:</strong></p>
  <pre><code>var FORM_ID = '#####';
  var SHEET_NAME = 'Operations';
  
  function sendFormToEmail() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    console.time('section10'); // 25579ms
    var sheet = ss.getSheetByName(SHEET_NAME);
    console.timeEnd('section10');
    
    // Open a form by ID and log the responses to each question.
    var form = FormApp.openById(FORM_ID);
    
    var formResponses = form.getResponses();
    var i = formResponses.length - 1;
    var formResponse = formResponses[i]; // Last item
    var itemResponses = formResponse.getItemResponses();
    
    var length = itemResponses.length;
    Logger.log(&quot;length = &quot; + length);
  
    var emailTo = itemResponses[0].getResponse();  // Returns the email if given
    var cp = itemResponses[1].getResponse();  // Counterparty
    var subject = &quot;Input form: &quot;+ cp;
    var datePay = itemResponses[2].getResponse();  // Date of the operation
    var dateAccept = itemResponses[3].getResponse();  // Date of acceptance
    var sum = itemResponses[4].getResponse() ;  // Amount
    var what = itemResponses[5].getResponse();  // Operation type
  
    var comm =  &quot;Correction &quot; + itemResponses[length - 1].getResponse(); // Last response
    var sum_1 = parseFloat(sum.replace(/,/, '.')) * (-1);  // Amount * (-1)
    var timestamp = new Date();
    var textBody = &quot;Operation: &quot; + timestamp + &quot;;\n&quot;;
  
    for (var j = 0; j &lt; itemResponses.length; j++) {
      var itemResponse = itemResponses[j];
      var resp = itemResponse.getResponse();
      textBody += itemResponse.getItem().getTitle() + &quot;=&quot; + resp + &quot;;\n&quot;;
    }
  
    if (what == 'Transfer') { // If the transfer between your accounts
      var lr = sheet.getLastRow();
      sheet.insertRowBefore(lr);
      var values = [datePay, dateAccept, &quot;&quot;, sum_1, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, comm, &quot;&quot;, &quot;&quot;, &quot;&quot;, what, timestamp, &quot;&quot;, &quot;&quot;, &quot;&quot;];
      Logger.log(values);
      console.time('section12'); // 19449ms
      sheet.getRange(lr, 2, 1, 17).setValues([values]);
      console.timeEnd('section12')
    }
    
    if(emailTo !== undefined){
      GmailApp.sendEmail(emailTo, subject, textBody);
    }
  }
  </code></pre>
  <p>At first I thought it was the large size of the acceptance sheet (about 13 thousand rows). I created a copy of the table, reduced it to several dozen rows, but the speed did not increase much.
  Then I deleted the answers from the form (there were just under 9000 of them) - the same thing, I didn't get much performance gain.</p>
  <p>Anyone have any ideas how to change the script algorithm to improve its performance?</p>
  <p>===</p>
  <p><strong>Conclusions</strong></p>
  <p>Thanks to @TheMaster for the help with <code>console.time()</code>. Thanks to this tool I found bottlenecks in the code. More precisely, the code works well, but it's about the structure of the spreadsheets system with which the code interacts. The structure needs to be optimized.
  There was also an idea addressed, probably, to Google developers. It would be great if there was a tool that visually (graphically) displays the relationships between spreadsheets and sheets that make up a single system. Perhaps with some kind of numerical characteristics that reflect, for example, the interaction time between its blocks. This would make it possible to quickly eliminate such bottlenecks in the system and improve it.</p>