mercredi 21 novembre 2018

Google Apps Script If Statement Matches Cell Value

I have a query regarding Google App Script and If Statements.

I have pushed data from my Ads account to a Google sheet. I then want multiple sheets to pull specific data from that. So I am trying to make one sheet the source of all data. I can then select parts of the data I need for each of my report.

The main one is to get the range where the day of the week == today.

I have tried doing this by adding a check variable which is then passed into a for loop checking if the day == the dayName variable.

I am currently getting an execution issue with it timing out. This is leading me to believe I have an issue with the way I have done this.

The data set is 21k rows: A is date, B is day of week, C is campaign and so on up to column I.

I'm still very new to the scripting game and any help is truly appreciated.

The reason I am looking to do it this way is because I seriously struggled to include a where statement within ads script to filter by DayOfWeek. I then thought this may an alternate solution.

Thanks, Liam

function myFunction() {

 //This is the days of the week for the today lookup
 var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

 //This is today's date
 var end = new Date().toISOString().slice(0,10);

 //This supplies the current day of week
 var dow = new Date(end);
 var dayName = days[dow.getDay()];

 var sss = SpreadsheetApp.openById('SheetID'); //ID of the sheet

 var ss = sss.getSheetByName('googleData'); //Sheet name

 var range = ss.getRange('A1:I'); //The range to copy
 var check = ss.getRange('B2:B').getValues(); The range to if statement against
 var data = range.getValues();

 var tss = SpreadsheetApp.openById('SheetID'); //destination sheet ID

 var ts = tss.getSheetByName('usedData'); //Destination sheet name

  for (var i=0;i<check.length; i++) {
   if(check[i]==dayName) { 
      ts.getRange(1, 1, data.length, data[0].length).setValues(data); //you will need to define the size of the copied data see getRange()
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire