I am trying to write a script that deletes all the events from every calendar in a certain month. Based on this method it looks like I am using the correct code, but every time I try to run this code, I get the error,
TypeError: Cannot find function getEvents in object Calendar. (line 17, file "Code")
My code is:
function myFunction() {
var year = 2018;
var month = 11;
var fromDate = new Date(year,month,1,0,0,0);
var toDate = new Date(year,month,28,0,0,0);
var calendars = ['def', 'Appointments', 'Chores', 'Contacts', 'Dates', 'Errand', 'IMPORTANT!!', 'Scheduling','Time-Sensitive', 'TV Show', 'Work'];
for (var x = 0; x < calendars.length; x++) {
var calendar = CalendarApp.getCalendarsByName(calendars[x])[0];
var events = calendar.getEvents(fromDate, toDate);
for (var i = 0; i < events.length; i++) {
var ev = events[i];
ev.deleteEvent();
}
}
}
Does anyone know what I am doing wrong?
The problem is that CalendarApp.getCalendarsByName(calendars[x]); returns an array. To access even the first and only element in the array you can do this:
var calendar=CalendarApp.getCalendarsByName(calendars[x])[0]; so that now the variable calendar is now a calendar and not an array of calendars.
Here's the form the script script I'm using:
function myFunction101() {
var year=2018;
var month=11;
var fromDate=new Date(year,month,1,0,0,0);
var toDate=new Date(year,month,28,0,0,0);
var calendars=['', ''];//using my calendars
for(var x=0;x<calendars.length;x++) {
var calendar=CalendarApp.getCalendarsByName(calendars[x])[0];
var events=calendar.getEvents(fromDate, toDate);
for(var i = 0; i < events.length; i++){
var ev = events[i];
Logger.log(ev.getTitle());
//ev.deleteEvent();
}
}
}
Related
I've implemented a script in my google sheet that grabs gcal calendar info and displays it in various tabs of the sheet. However, the end date of each event as displayed in the sheet is one day LONGER than what is displayed in the calendar itself. Code below. Through logging, I can't figure out why its happening. Is there something to do with the formatting of the date? Any help would be appreciated!
function populateAllTabs() {
var id = "[MY CAL ID HERE]"; // id is currently set to bookings calendar. NB: Takes a string!
var cal = CalendarApp.getCalendarById(id);
var startPeriod = new Date('January 1, 2020');
startPeriod.setHours(0, 0, 0, 0);
var endPeriod = new Date(startPeriod);
endPeriod.setDate(endPeriod.getDate() + 365); // looks for all events in the range one year
var ss = SpreadsheetApp.getActive();
for(var n in ss.getSheets()){// loop over all tabs in the spreadsheet
var sheet = ss.getSheets()[n];// look at every sheet in spreadsheet
var name = sheet.getName();//get name
if(name != 'List'){
var gig = sheet.getRange(1,1);
var gigName = gig.getValue();
var events = cal.getEvents(startPeriod, endPeriod, {search:gigName});
// find the title of each event in the calendar
var eventTitles = [];
for (var i = 0; i < events.length; i++) {
eventTitles.push([events[i].getTitle()]);
}
// find the start date of each event in the calendar
var starttime = [];
for (var i = 0; i < events.length; i++) {
starttime.push([Utilities.formatDate(events[i].getStartTime(), "GMT", "MM/dd/yy")]);
}
// find the end date of each event in the calendar
var endtime = [];
for (var i = 0; i < events.length; i++) {
endtime.push([Utilities.formatDate(events[i].getEndTime(), "GMT", "MM/dd/yy")]);
}
var cell = sheet.getRange("B3");
cell.setValue(starttime + ' - ' + endtime);
}
}
}
This works for me. No problem with the dates
function populateAllTabs() {
var cal = CalendarApp.getCalendarById('id');
var startyear=2019;
var startPeriod = new Date(startyear,0,1);
var endPeriod = new Date(startyear+1,1,1);
var ss=SpreadsheetApp.getActive();
var shts=ss.getSheets();
for(var n=0;n<shts.length;n++){
var sheet=shts[n];
var name=sheet.getName();
if(name!='List'){
var gigName = sheet.getRange(1,1).getValue();
var ev=cal.getEvents(startPeriod, endPeriod, {search:gigName});
var gigs=[]
for (var i=0;i< ev.length;i++) {
gigs.push([ev[i].getTitle(),Utilities.formatDate(new Date(ev[i].getStartTime()),Session.getScriptTimeZone(),"MM/dd/yy"),Utilities.formatDate(new Date(ev[i].getEndTime()),Session.getScriptTimeZone(),"MM/dd/yy")]);
}
if(gigs) {
sheet.getRange(3,2,gigs.length,gigs[0].length).setValues(gigs);
}
}
}
}
I wrote a google script, which is creating form, using data from google spreadsheet. I'm using drop-down answer options. The code works normally, all data appears, but sometimes ( 3 times of 5 attempts), after refreshing browser page, some questions disappeared. Everytime it's different question, so it isn't problem with specific question.
What is the problem and how can i fix it?
UPD: Solved. Problem was in opened Google Form while code was compiled.
function myFunk(){
var SHEET_ID = FormApp.getActiveForm().getDestinationId();
var ss = SpreadsheetApp.openById(SHEET_ID);
var form = FormApp.getActiveForm();
var sheets = ss.getSheets();
var numb = sheets[1].getLastRow();
var TypeSelect = form.addListItem()
.setRequired(true);
var TypeChoices = getData(TypeSelect,sheets[1],1,numb)
TypeSelect.setChoices(TypeChoices);
var ClientSelect = form.addListItem()
.setRequired(true)
var ClientChoices = getData(ClientSelect, sheets[1],2,numb)
ClientSelect.setChoices(ClientChoices);
var phases = ["Start","End"];
var PhasesChoices=[];
var PhaseSelect = form.addMultipleChoiceItem()
.setRequired(true);
for (var t=0;t<phases.length; t++){
PhasesChoices.push(PhaseSelect.createChoice(phases[t]));
}
PhaseSelect.setChoiceValues(phases);
}
function getData(DataSelect,sheet,numbColumn,lRow){
var DataChoices = [];
var DataValues = sheet.getRange(2,numbColumn,lRow,1).getValues()
.filter(CheckFull);
var data = [];
for(var i = 0; i < DataValues.length; i++) {
data.push(DataValues[i]);
}
for(var j =0; j < data.length; j++) {
DataChoices.push(DataSelect.createChoice(data[j]));
}
return DataChoices;
}
function CheckFull(value){
return value != '';
}
I have a google app script that collects information about Gmail messages and then pastes it into a google sheet. Trouble is it doesn't get ALL of the messages. It only picks up the first one of each thread. I feel like I am missing something to loop through each thread? Any suggestions?
function getMail(){
var myspreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var mysheet = myspreadsheet.getSheetByName("Sheet3");
var start = 0;
var max = 99;
var count =0;
var row = mysheet.getLastRow()+1
var maxDate = mysheet.getRange("B1").getValue()
while(count < 4)
{
var threads = GmailApp.getInboxThreads(start , max);
var messages = GmailApp.getMessagesForThreads(threads);
var froms = [];
messages.get
for(var i = 0; i < threads.length; i++)
{
var msgDate = messages[i][0].getDate();
if(msgDate>maxDate){
froms.push([messages[i][0].getDate(),messages[i][0].getFrom(),messages[i][0].getSubject(),messages[i][0].getPlainBody()]);
}
}
if(froms.length>0){
mysheet.insertRows(2, froms.length)
mysheet.getRange(2,1,froms.length,4).setValues(froms);
}
start = start + 100;
count++;
}
}
Your current script is only grabbing messages[i][0], the first message in that group for the thread. Instead you need to loop through all of the messages using two for loops, as you can see in the script below I use messages[i][j].
function getMail() {
var mySpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var mySheet = mySpreadsheet.getSheetByName("Sheet3");
var start = 0;
var max = 99;
var count = 0;
var maxDate = mySheet.getRange("B1").getValue();
while(count < 4) {
var threads = GmailApp.getInboxThreads(start, max);
var messages = GmailApp.getMessagesForThreads(threads);
var froms = [];
for(var i = 0; i < messages.length; i++) {
for(var j = 0; j < messages[i].length; j++) {
var msgDate = messages[i][j].getDate();
if(msgDate > maxDate) {
froms.push([msgDate,messages[i][j].getFrom(),messages[i][j].getSubject(),messages[i][j].getPlainBody()]);
}
}
}
if(froms.length > 0) {
mySheet.insertRows(2, froms.length);
mySheet.getRange(2, 1, froms.length, 4).setValues(froms);
}
start = start + 100;
count++;
}
}
Notable changes:
removed var rows because it wasn't used anywhere in the script.
changed first for loop to run for messages.length rather than
threads.
added another for loop to loop through every message in
messages[i].
you were getting messages[i][0].getDate() twice, so I just used the variable already defined for adding to the array.
minor grammatical/spacing changes for consistency across script.
I am writing a script to delete every event from each of my calendars in a single month. My code:
function myFunction() {
var year = 2018;
var month = 11;
var fromDate = new Date(year,month,1,0,0,0);
var toDate = new Date(year,month,28,0,0,0);
var calendars = ['cal1', 'cal2', 'cal3','cal4','cal5','cal6'];
for (var x = 0; x < calendars.length; x++) {
var calendar = CalendarApp.getCalendarsByName(calendars[x])[0];
var events = calendar.getEvents(fromDate, toDate);
for(var i = 0; i < events.length; i++){
var ev = events[i];
ev.deleteEvent();
}
}
}
When I run my code I get the following error:
TypeError: Cannot call method "getEvents" of undefined. (line 13, file
"Code")
Am I not running getEvents() on a calendar object? Why is this code give me an error when it tries to use that function?
Try this:
I removed the deleteEvents because I don't want to delete my events. But this code produced Calendar Name, Event Title, Event StartTime and Event EndTime for all event in the given period.
function myFunction() {
var year = 2018;
var month = 11;
var fromDate = new Date(year,month,1,0,0,0);
var toDate = new Date(year,month,28,0,0,0);
var calendars = ['You'll have to put your calendar names back in here.']
var html='';
for (var x=0;x<calendars.length;x++) {
var calendar = CalendarApp.getCalendarsByName(calendars[x]);
var events = calendar[0].getEvents(fromDate, toDate);
for(var i = 0; i < events.length; i++){
var ev = events[i];
html+=Utilities.formatString('<br />Calendar Name: %s<br />EventTitle: %s<br />EventStartTime: %s<br />Event End Time: %s',calendar[0].getName(),events[i].getTitle(),events[i].getStartTime(),events[i].getEndTime() );
}
}
var userInterface=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Calendar Events')
}
Im trying to save google groups emails into a spreadsheet but Im getting the 'maximum execution time' error. Any ideas?
function listGroupMembers() {
var GROUP_EMAIL = "prgg#googlegroups.com";
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
var users = group.getUsers();
var sheet = SpreadsheetApp.create("Group Mail");
for (var i = 0; i < users.length; i++) {
sheet.appendRow([users[i]]);
}
}
What is probably taking much of the time is the appendRow() call.
you should build an array with all the values and write this at once in your sheet.
Code could be like this :
function listGroupMembers() {
var GROUP_EMAIL = "prgg#googlegroups.com";
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
var users = group.getUsers();
var sheet = SpreadsheetApp.create("Group Mail");
var values = [];
for (var i = 0; i < users.length; i++) {
values.push([users[i]]);
}
sheet.getRange(1,1,values.length, values[0].length).setValues(values);
}
EDIT
I didn't check the begining of your initial code, SpreadsheetApp.create("Group Mail"); returns a spreadsheet, not a sheet... that's why it fails on getRange.
Since it's not clear what you wanted to get exactly I assumed you wanted to create a new Sheet with that name if it doesn't exist.
The appropriate code would be like this :
function listGroupMembers() {
var GROUP_EMAIL = "prgg#googlegroups.com";
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
var users = group.getUsers();
if(SpreadsheetApp.getActive().getSheetByName("Group Mail") == null){
var sheet = SpreadsheetApp.getActive().insertSheet("Group Mail");
}else{
var sheet = SpreadsheetApp.getActive().getSheetByName("Group Mail");
}
var values = [];
for (var i = 0; i < users.length; i++) {
values.push([users[i]]);
}
sheet.getRange(1,1,values.length, values[0].length).setValues(values);
}