Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

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

Post a Comment for "Google Apps Script If Statement Matches Cell Value"