简体   繁体   中英

Google-App-Script Conditional Loop is running but is not behaving as expected (java script)

I am pulling data from a google sheets which looks like this: 在此处输入图片说明

Now, I want to generate google slides for ONLY rows where the Timestamp column is between Last Deadline and Next Deadline. In the example, it would pull the record in A2:B2 as the Timestamp is between these two dates. I added this logic to my script but when I run it, it does not generate the slide, ie it does behave as expected but neither do I get an error. What could it be?

function generateSlides_master() 
{
  
  var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1hhf"; 
  var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
  var deck = SlidesApp.getActivePresentation();
  var sheet = ss.getSheetByName('Form_Responses');
  var values = sheet.getRange('A2:N20000').getValues();
  var slides = deck.getSlides();
  var templateSlide = slides[1];
  var last_deadline = sheet.getRange('P4:P4').getValues();
  var next_deadline = sheet.getRange('P2:P2').getValues();
  
 
  values.forEach(function(page){ //for each row in google sheets
  if(page[0]){
    if (page[0] > last_deadline && Work_Week<= next_deadline ){ //THIS IS NOT WORKING AS EXPECTED!
  
   var Work_Week = Utilities.formatDate(page[0], "GMT", "MM/dd/yyyy");
   var Email = page[1];
 
 
   templateSlide.duplicate(); //duplicate the template page
   slides = deck.getSlides(); //update the slides array for indexes and length
   newSlide = slides[2]; // declare the new page to update
    
    
   var shapes = (newSlide.getShapes());
     shapes.forEach(function(shape){
       shape.getText().replaceAllText('{{Email}}',Email);
      
    }); 
   presLength = slides.length; 
   newSlide.move(presLength); 
  }
  }// end our conditional statement
  }); //close our loop of values

//Remove the template slide
//templateSlide.remove();
  
}

You've defined last_deadline and next_deadline to be 2-dimensional arrays, so your if-statement isn't actually checking against the dates. Use getValue() instead to get the individual values.

var last_deadline = sheet.getRange('P4').getValue();
var next_deadline = sheet.getRange('P2').getValue();

I also think you meant page[0] <= next_deadline , instead of comparing against Work_Week .

Here's a heavily edited example that will simply log the timestamp rather than creating a slide.

function generateSlides_master() {
  var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1hhf"; 
  var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
  var sheet = ss.getSheetByName('Form_Responses');
  var values = sheet.getRange('A2:N20000').getValues();
  var last_deadline = sheet.getRange('P4').getValue();
  var next_deadline = sheet.getRange('P2').getValue();

  values.forEach(function(page) { //for each row in google sheets
    var timestamp = page[0];
    if (timestamp) {
      if (timestamp > last_deadline && timestamp<= next_deadline) {
        Logger.log('Create slide ' + timestamp);
      }
    }
  }); 
}

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