Google Apps Script If Statement Matches Cell Value












0














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









share|improve this question



























    0














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









    share|improve this question

























      0












      0








      0







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









      share|improve this question













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






      javascript if-statement google-apps-script google-sheets






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 15:06









      Liam Hooper

      32




      32
























          1 Answer
          1






          active

          oldest

          votes


















          0














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





          share|improve this answer























          • Thank you for the very quick reply. I used sheet ID as I placed this script in the used data file so it pulls from the ID. Should I attach it to the Google data instead?
            – Liam Hooper
            Nov 21 '18 at 15:37












          • Just to add, I have just tested both methods and the script is not firing but also not throwing an error.
            – Liam Hooper
            Nov 21 '18 at 15:44










          • The execution log shows this: [18-11-21 15:46:58:454 GMT] Logger.log([ReferenceError: "result" is not defined., ]) [0 seconds] [18-11-21 15:46:58:455 GMT] Execution succeeded [2.282 seconds total runtime]
            – Liam Hooper
            Nov 21 '18 at 15:48










          • Oops typo I put result.push() it should have been results.push()
            – TheWizEd
            Nov 21 '18 at 16:04










          • Awesome and such a silly thing I should have noticed. I just tested and it pushed through the correct data. I noticed that it doesn't overwrite the whole sheet. I had more rows of data and it only replaced the rows it covered. Should it replace all the rows? if not I can have a look into how to get that to work. Trying to learn as much as possible and really appreciate the help given.
            – Liam Hooper
            Nov 21 '18 at 16:50











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414943%2fgoogle-apps-script-if-statement-matches-cell-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














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





          share|improve this answer























          • Thank you for the very quick reply. I used sheet ID as I placed this script in the used data file so it pulls from the ID. Should I attach it to the Google data instead?
            – Liam Hooper
            Nov 21 '18 at 15:37












          • Just to add, I have just tested both methods and the script is not firing but also not throwing an error.
            – Liam Hooper
            Nov 21 '18 at 15:44










          • The execution log shows this: [18-11-21 15:46:58:454 GMT] Logger.log([ReferenceError: "result" is not defined., ]) [0 seconds] [18-11-21 15:46:58:455 GMT] Execution succeeded [2.282 seconds total runtime]
            – Liam Hooper
            Nov 21 '18 at 15:48










          • Oops typo I put result.push() it should have been results.push()
            – TheWizEd
            Nov 21 '18 at 16:04










          • Awesome and such a silly thing I should have noticed. I just tested and it pushed through the correct data. I noticed that it doesn't overwrite the whole sheet. I had more rows of data and it only replaced the rows it covered. Should it replace all the rows? if not I can have a look into how to get that to work. Trying to learn as much as possible and really appreciate the help given.
            – Liam Hooper
            Nov 21 '18 at 16:50
















          0














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





          share|improve this answer























          • Thank you for the very quick reply. I used sheet ID as I placed this script in the used data file so it pulls from the ID. Should I attach it to the Google data instead?
            – Liam Hooper
            Nov 21 '18 at 15:37












          • Just to add, I have just tested both methods and the script is not firing but also not throwing an error.
            – Liam Hooper
            Nov 21 '18 at 15:44










          • The execution log shows this: [18-11-21 15:46:58:454 GMT] Logger.log([ReferenceError: "result" is not defined., ]) [0 seconds] [18-11-21 15:46:58:455 GMT] Execution succeeded [2.282 seconds total runtime]
            – Liam Hooper
            Nov 21 '18 at 15:48










          • Oops typo I put result.push() it should have been results.push()
            – TheWizEd
            Nov 21 '18 at 16:04










          • Awesome and such a silly thing I should have noticed. I just tested and it pushed through the correct data. I noticed that it doesn't overwrite the whole sheet. I had more rows of data and it only replaced the rows it covered. Should it replace all the rows? if not I can have a look into how to get that to work. Trying to learn as much as possible and really appreciate the help given.
            – Liam Hooper
            Nov 21 '18 at 16:50














          0












          0








          0






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





          share|improve this answer














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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 17:29

























          answered Nov 21 '18 at 15:29









          TheWizEd

          546147




          546147












          • Thank you for the very quick reply. I used sheet ID as I placed this script in the used data file so it pulls from the ID. Should I attach it to the Google data instead?
            – Liam Hooper
            Nov 21 '18 at 15:37












          • Just to add, I have just tested both methods and the script is not firing but also not throwing an error.
            – Liam Hooper
            Nov 21 '18 at 15:44










          • The execution log shows this: [18-11-21 15:46:58:454 GMT] Logger.log([ReferenceError: "result" is not defined., ]) [0 seconds] [18-11-21 15:46:58:455 GMT] Execution succeeded [2.282 seconds total runtime]
            – Liam Hooper
            Nov 21 '18 at 15:48










          • Oops typo I put result.push() it should have been results.push()
            – TheWizEd
            Nov 21 '18 at 16:04










          • Awesome and such a silly thing I should have noticed. I just tested and it pushed through the correct data. I noticed that it doesn't overwrite the whole sheet. I had more rows of data and it only replaced the rows it covered. Should it replace all the rows? if not I can have a look into how to get that to work. Trying to learn as much as possible and really appreciate the help given.
            – Liam Hooper
            Nov 21 '18 at 16:50


















          • Thank you for the very quick reply. I used sheet ID as I placed this script in the used data file so it pulls from the ID. Should I attach it to the Google data instead?
            – Liam Hooper
            Nov 21 '18 at 15:37












          • Just to add, I have just tested both methods and the script is not firing but also not throwing an error.
            – Liam Hooper
            Nov 21 '18 at 15:44










          • The execution log shows this: [18-11-21 15:46:58:454 GMT] Logger.log([ReferenceError: "result" is not defined., ]) [0 seconds] [18-11-21 15:46:58:455 GMT] Execution succeeded [2.282 seconds total runtime]
            – Liam Hooper
            Nov 21 '18 at 15:48










          • Oops typo I put result.push() it should have been results.push()
            – TheWizEd
            Nov 21 '18 at 16:04










          • Awesome and such a silly thing I should have noticed. I just tested and it pushed through the correct data. I noticed that it doesn't overwrite the whole sheet. I had more rows of data and it only replaced the rows it covered. Should it replace all the rows? if not I can have a look into how to get that to work. Trying to learn as much as possible and really appreciate the help given.
            – Liam Hooper
            Nov 21 '18 at 16:50
















          Thank you for the very quick reply. I used sheet ID as I placed this script in the used data file so it pulls from the ID. Should I attach it to the Google data instead?
          – Liam Hooper
          Nov 21 '18 at 15:37






          Thank you for the very quick reply. I used sheet ID as I placed this script in the used data file so it pulls from the ID. Should I attach it to the Google data instead?
          – Liam Hooper
          Nov 21 '18 at 15:37














          Just to add, I have just tested both methods and the script is not firing but also not throwing an error.
          – Liam Hooper
          Nov 21 '18 at 15:44




          Just to add, I have just tested both methods and the script is not firing but also not throwing an error.
          – Liam Hooper
          Nov 21 '18 at 15:44












          The execution log shows this: [18-11-21 15:46:58:454 GMT] Logger.log([ReferenceError: "result" is not defined., ]) [0 seconds] [18-11-21 15:46:58:455 GMT] Execution succeeded [2.282 seconds total runtime]
          – Liam Hooper
          Nov 21 '18 at 15:48




          The execution log shows this: [18-11-21 15:46:58:454 GMT] Logger.log([ReferenceError: "result" is not defined., ]) [0 seconds] [18-11-21 15:46:58:455 GMT] Execution succeeded [2.282 seconds total runtime]
          – Liam Hooper
          Nov 21 '18 at 15:48












          Oops typo I put result.push() it should have been results.push()
          – TheWizEd
          Nov 21 '18 at 16:04




          Oops typo I put result.push() it should have been results.push()
          – TheWizEd
          Nov 21 '18 at 16:04












          Awesome and such a silly thing I should have noticed. I just tested and it pushed through the correct data. I noticed that it doesn't overwrite the whole sheet. I had more rows of data and it only replaced the rows it covered. Should it replace all the rows? if not I can have a look into how to get that to work. Trying to learn as much as possible and really appreciate the help given.
          – Liam Hooper
          Nov 21 '18 at 16:50




          Awesome and such a silly thing I should have noticed. I just tested and it pushed through the correct data. I noticed that it doesn't overwrite the whole sheet. I had more rows of data and it only replaced the rows it covered. Should it replace all the rows? if not I can have a look into how to get that to work. Trying to learn as much as possible and really appreciate the help given.
          – Liam Hooper
          Nov 21 '18 at 16:50


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414943%2fgoogle-apps-script-if-statement-matches-cell-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)