Question

Thanks to info gathered from here, and direct help in other posts. An idiot like me has managed to form something resembling a script :)

And it does exactly what it should, with right results, perfectly. Happy days. But unfortunately it can take a while to execute (part of the reason its slow, is slow formula in the google sheet, hoping to replace formula with on demand scripts like this one and ditch most bulky formula)

Until I get to that stage, I was hoping someone would take a peek at it, and see if there is any overhead/bloat/processing that could be saved.

My current thoughts are that I'm checking the data more than once, when I could try to check all if's at the same time.

I'm also writing to the sheet a few times, perhaps it would be better to put returned values in an array and write once.

So I'm trying to look into that, but I'm still quite new at all this.

Code simply count's form responses based on a value and date then outputs results to a report.

function snapshot() {
  var dateColumn = 2;
  var findColumn = 4;
  var toFind = "Yes - Complete (going ahead)";  
  var daysWorth = 31; 
  var target = "Snapshot";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
   var targetSheet = ss.getSheetByName(target);
  var sheet = ss.getSheetByName('Form Responses');
  var values = sheet.getDataRange().getValues();

  var today = new Date();
  var thisMorning = today;
  thisMorning.setHours(0);
  thisMorning.setMinutes(0);



  var completed = 0;
  var attempted = 0;
  var totAtt = 0;
  var totCom = 0;

  for (var i = 0; i < daysWorth; i++){
  var startGap = new Date().setTime(thisMorning.getTime() - i*24*60*60*1000);
  var endGap = new Date().setTime(thisMorning.getTime() - (i-1)*24*60*60*1000);

  for(var counter in values){
   var testDate = new Date(values[counter][dateColumn -1]);
   if (testDate > startGap && testDate < endGap && values[counter][findColumn -1] == toFind)
   {completed++;
   totCom++;}
   if (testDate > startGap && testDate < endGap)
   {attempted++;
   totAtt++}
  }



  var output = new Date(startGap);
 //Logger.log("since "+output+" we had: "+completed+" completed and: "+attempted+" attempted");

 targetSheet.getRange(i+2,1).setValue(output);
 targetSheet.getRange(i+2,2).setValue(attempted);
 targetSheet.getRange(i+2,3).setValue(completed);   
 targetSheet.getRange(i+2,4).setValue(completed/attempted*100);

  var completed = 0;
  var attempted = 0;

}
targetSheet.getRange(1,1).setValue("Date");
targetSheet.getRange(1,2).setValue("Attempted");
targetSheet.getRange(1,3).setValue("Completed");
targetSheet.getRange(1,4).setValue("Success Rate");
targetSheet.getRange(33,1).setValue("Totals");
targetSheet.getRange(33,2).setValue(totAtt);
targetSheet.getRange(33,3).setValue(totCom);
targetSheet.getRange(33,4).setValue(totCom/totAtt*100);


}

Thanks for reading :)

EDIT

So here it is, with suggested edits and working.

function snapshot() {
  var dateColumn = 2;
  var findColumn = 4;
  var toFind = "Yes - Complete (going ahead)";  
  var daysWorth = 31; 
  var target = "Snapshot";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var targetSheet = ss.getSheetByName(target);
  var sheet = ss.getSheetByName('Form Responses');
  var values = sheet.getDataRange().getValues();

  var today = new Date();
  var thisMorning = today;
  thisMorning.setHours(0);
  thisMorning.setMinutes(0);
  var archive = [];


  var completed = 0;
  var attempted = 0;
  var totAtt = 0;
  var totCom = 0;

  for (var i = 0; i < daysWorth; i++){
  var startGap = new Date().setTime(thisMorning.getTime() - i*24*60*60*1000);
  var endGap = new Date().setTime(thisMorning.getTime() - (i-1)*24*60*60*1000);

  for(var counter in values){
   var testDate = new Date(values[counter][dateColumn -1]);
   if (testDate > startGap && testDate < endGap && values[counter][findColumn -1] == toFind)
   {completed++;
   totCom++;}
   if (testDate > startGap && testDate < endGap)
   {attempted++;
   totAtt++}

  }



  var output = new Date(startGap);
  if(!completed/attempted)
    {var success = 0;}
    else
   {var success = completed/attempted*100;}


 archive.push( [output, attempted, completed,success] ); 
  var completed = 0;
  var attempted = 0;

}
  var headers =[["Date","Attempted","Completed","Success Rate"]];
  var footers =[["Totals",totAtt,totCom,totCom/totAtt*100]];  
  targetSheet.getRange(1,1,1,4).setValues(headers);
  targetSheet.getRange(2,1).offset(0, 0, archive.length, archive[0].length).setValues(archive);
  targetSheet.getRange(33,1,1,4).setValues(footers);    


}

Thanks for the feedback and if anyone has anymore, I'm all ears.

Was it helpful?

Solution

"perhaps it would be better to put returned values in an array and write once." - yep, gotta figure out how to .setValues() once.

targetSheet.getRange(i+2,1).setValue(output); targetSheet.getRange(i+2,2).setValue(attempted); targetSheet.getRange(i+2,3).setValue(completed);
targetSheet.getRange(i+2,4).setValue(completed/attempted*100);

should be some thing like...

myArray.push( [output, attempted, completed, completed/attempted*100] );

then outside the loop...

targetSheet.getRange('A1').offset(0, 0, myArray.length, myArray[0].length).setValues(myArray);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top