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)
}
}
calendar items that I want to retrieve in a comma separated fashionand the comma inagenda items that contain a comma in the title?