title: Google Apps Script copies a sheet before I create a new one
body: <p>This question deals with the same script as my last post.
  I added a new feature, which will take the output of the script and send it back to the source workbook as a new sheet with the date it was modified as the name. The purpose is to create an order history. This script and sheet is used multiple times a day. Here is the code:</p>
  
  <pre><code>//Copy order to inventory
        newOrderSheet.clearContents();
        var newOrderSheetData = newOrderSheet.getRange(1,1,scoopsData.length, scoopsData[0].length);
            newOrderSheetData.setValues(scoopsData);
        var targetSheet = SpreadsheetApp.openById(thisSheetId);
            this.newOrderSheet.copyTo(targetSheet).setName(date);
  </code></pre>
  
  <p>This block runs in a switch, with a case for each workbook I need to access. The code in the block runs through each page, pulling the info into the workbook where the script runs. When it loops over a sheet, it gets all the info in the data range and copies that to a sheet in the calling workbook. It then sorts out anything with a zero value, dumps that to the order sheet, then moves on to the next sheet in the target workboook, copies and sorts, next page. It is getting the data fine. I get what I expect.</p>
  
  <p>However, while the top line of this block clears the contents, when I go to look at the sheet that has been copied back into the the data source sheet, the new copied sheet values match the values of the sheet the script was previously run on. To be clear, not the one generated by the script that just ran, but the one generated by the script having run previously. I am happy to provide samples if you need.
  Thank you StackOverflow community!</p>