简体   繁体   中英

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()
    }
  }
}

I believe what you want is to find all rows that match the current day of week and copy to userData. Try this.

function myFunction() {
  try {
    // javascript getDay starts with Sunday
    var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    var today = new Date();
    var dayOfWeek = days[today.getDay()];
    // Do you need id or just active spreadsheet?
    var spread = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = spread.getSheetByName("googleData");
    // Get all data columns A thru I
    var data = sheet.getRange(1,1,sheet.getLastRow(),9).getValues();
    // results will become a 2D array to use setValues
    var results = [];
    for( var i=0; i<data.length; i++ ) {
      // Day of week in column B
      if( data[i][1] === dayOfWeek ) results.push(data[i]);
    }
    sheet = spread.getSheetByName("usedData");
    sheet.clear();
    sheet.getRange(1,1,results.length,results[0].length).setValues(results);
  }
  catch(err) {
    Logger.log(err);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM