简体   繁体   中英

Search on a literal comma (string) in Google apps script

I'm using a script to retrieve items from Google Calendar and put it into Google Sheets. In my agenda, I've formatted the calendar items that I want to retrieve in a comma separated fashion as follows: Client, Project, Activities.

Now, I want to retrieve ONLY the agenda items that contain a comma in the title (or, better yet, ultimately two comma's), but I cannot find a way to search on comma's as a string. I use the "search" function, and replacing the comma with a word, perfectly filters only the items with that word. But searching on just the comma (or any other special character) doesn't work. I tried dozens of combinations, including REGEX expressions, but nothing works. Part of the problematic code:

I'm using a script to retrieve items from Google Calendar and put it into Google Sheets. In my agenda, I've formatted the calendar items that I want to retrieve in a comma separated fashion as follows: Client, Project, Activities.

Now, I want to retrieve ONLY the agenda items that contain a comma in the title (or, better yet, ultimately two comma's, separated bij any character or characters), but I cannot find a way to search on comma's as a string. I use the "search" function, and replacing the comma with a word, perfectly filters only the items with that word. But searching on just the comma (or any other special character) doesn't work. I tried dozens of combinations, including REGEX expressions, but nothing works. My code so far:

function getCalendarEvents() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Brondata"); 
  var cal = CalendarApp.getCalendarById("mail@domain."); 
  var eind = new Date;
  eind.setDate(eind.getDate()+1);
  var events = cal.getEvents(new Date("1/1/2022"), eind, {search: '(/,{2}/)'});
  var lr = ss.getLastRow();
  ss.getRange(3, 1, lr,14).clearContent();
  var result = []
  events.forEach(e => {
    result.push([e.getStartTime(),
                e.getEndTime(),
                e.getTitle(),
                e.getDescription(),
                e.getDateCreated(),
                e.getLastUpdated(),
                e.isRecurringEvent(),
                e.getCreators()])
  })
  ss.getRange(3,1,result.length,result[0].length).setValues(result)
}

What code do I need to use after the "search" command, to find only the items that contain a comma?

Some things I've tried:

'\,' (filters out nothing)
'==\,' (filters out nothing)
'"\,"' (filters out a bit more, but I can't figure out what exactly)
'"==\,"' (same result as previous)

Updated the script with the input of @TheWizEd, and it now runs fine perfectly, The complete: working script is as follows:

function getCalendarEvents() {
  let ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Brondata"); 
  let cal = CalendarApp.getDefaultCalendar();
  let eind = new Date();
  eind.setDate(eind.getDate()+1);
  let events = cal.getEvents(new Date("1,1,2022"), eind);
  let lr = ss.getLastRow();
  ss.getRange(3, 1, lr,14).clearContent();
  let result = []
  let match = events.filter( e =>  { if( e.getTitle().match(".*\,.*\,") !== null ) return e; } );
  if( match ) {
    match.forEach(e => {
      let title = e.getTitle();
      let codes = [];
        for( let i=0; i<title.length; i++ ) {
          codes.push( title.charCodeAt(i) );
        }
      result.push([e.getStartTime(),
                  e.getEndTime(),
                  e.getTitle(),
                  e.getDescription(),
                  e.getDateCreated(),
                  e.getLastUpdated(),
                  e.isRecurringEvent(),
                  e.getCreators()])
    })
  ss.getRange(3,1,result.length,result[0].length).setValues(result)
  }
}

Descrition

I have created 3 events in my calander that have "Test, event *, ***" in the title with and without spaces. This example script will find all event with that in there title.

Code.gs

function getCalanderEvent() { 
  try {
    let cal = CalendarApp.getDefaultCalendar();
    let start = new Date("1,1,2022");
    let end = new Date();
    let events = cal.getEvents(start,end);
    let match = events.filter( event =>  { if( event.getTitle().match(/.*\,.*\,/) !== null ) return event; } );
    if( match ) {
      match.forEach( event => {
          let title = event.getTitle();
          console.log(title);
        }
      );
    }
  }
  catch(err) {
    console.log(err)
  }
}

Execution log

3:20:56 AM  Notice  Execution started
3:20:56 AM  Info    Test, event one, test
3:20:56 AM  Info    Test, event 3, test
3:20:56 AM  Info    Test,event,two
3:20:56 AM  Notice  Execution completed

Reference

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