简体   繁体   中英

Search for value in variable Google Apps Script/JS

Trying to determine if calendar events in Google Calendar were created by the user himself or if a CSV bulk upload was performed. If the script finds an ID with "CSV" somewhere in the value, then remove it. I tried to add an if statement where it looks at the variable 'res' that is using match , I think this is the part where I am wrong:

 var id_s = ev.getId(); 

    var res = id_s.match(/CSVConvert/g);

    if (res = true)  {

As you can see, i still get all events that do not contain 'CSVConvert'

在此处输入图片说明

This is what i have so far:

function myFunction() {

  var calendarId = 'user.name@altostrat.com';

  var calendar = CalendarApp.getCalendarById(calendarId); 
  var events = calendar.getEvents(new Date('June 18, 2015 00:00:00 CST'), new Date('June 18, 2015 18:00:00 CST'));
  for(var i=0; i<events.length;i++){
    var ev = events[i];

    var id_s = ev.getId(); 

    var res = id_s.match(/CSVConvert/g);

    if (res = true)  {


      Logger.log("ID: " + ev.getId());
      Logger.log("TITLE: " + ev.getTitle());
      Logger.log("DATECREATED: " + ev.getDateCreated());
      Logger.log("LAST UPDATED: " + ev.getLastUpdated());

    }
    else{
      Logger.log("No event ID's contains: CSVConvert")
    }

  }
}

Using the Logger a bit more would have given you the solution, or at least shown you where it failed.

Try like this :

Logger.log('match result = '+res);// when a match is found, res is equal to the matching string
if (res!=null)  { // otherwise it returns null

By the way, the equal condition is a == (double equal), in this case I used not equal ( != )

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