title: Optimizing Google Sheets .appendRow()
body: <p>I have been creating a script to iterate through a folder structure and copy specific data to rows in a Google Sheet.  The folder structure is similar to this</p>
  
  <ul>
  <li>Root folder
  
  <ol>
  <li>Street Name 1
  
  <ul>
  <li>102</li>
  <li>104</li>
  <li>etc</li>
  </ul></li>
  <li>Street Name 2
  
  <ul>
  <li>98-110</li>
  </ul></li>
  </ol></li>
  </ul>
  
  <p>After some work, I came up with this code</p>
  
  <pre><code>        // GetFolders function goes through first and second level subfolder 
  using while loop
  // Requires folderID string, an array for storage, and an active sheet to be 
  passed as arguments
      function GetFolders(folderID, dataArray, sheet)
      {
        var rootFolder = DriveApp.getFolderById(folderID).getFolders();
  
        while (rootFolder.hasNext()) 
    {
  var subFolder = rootFolder.next();
  var subFolderName = subFolder.getName();
  var subFolderID = subFolder.getId();
  
  //append first subfolder name to array
  dataArray[0] = subFolderName;
  
  var subFolderIterator = DriveApp.getFolderById(subFolderID).getFolders();
  
  while (subFolderIterator.hasNext())
  {
    var subFolder2 = subFolderIterator.next();
    var subFolder2Name = subFolder2.getName();
  
    //append second subfolder name to array
    dataArray[1] = subFolder2Name;
  
    //append second subfolder hyperlink to array
    dataArray[2] = subFolder2.getUrl();
  
    //default flag column to blank string
    dataArray[3] = '';
  
    //check subFolder2 for hyphen in name
    if (subFolder2Name.indexOf('-') &gt; -1) //if a hyphen is present in foldername
    {
      //append flag to dataArray
      dataArray[3] = 'X'; 
    } //end if      
  
    //write array contents to next empty row in spreadsheet
    sheet.appendRow(dataArray);        
  
      } //end while
    } //end while
  } //end GetFolders()
  </code></pre>
  
  <p>At first, this appeared to work wonderfully, writing about 3,500 rows before hitting the timeout limit.  However, I have over 12,000 rows that need to be written (preferrably with one single execution).  Looking at Google's "Best Practices" page tells me that this can be more efficient, but I'm at a loss with how to go about doing this.  From my research, it seems that batching would be my best option.  Does anyone know a good way to implement this in my script?</p>