I created a script in a Google Sheet to create events in Google Agenda, in order to study.
My goal is to create many events like this : if i study on D0, the first event must be at D+3, then D+10 , D+30, D+60.
I get many problems :
the script write "AJOUTE" in each box of the column (which means in french that the events are added to Agenda) , even if they are not completed with dates (I want to update for each chapter when it's done, and I do not do a whole chapter the same day!)
the script doesn't care if the events are already created, so i get multiple events for the same thing...
My script is the following :
var EVENT_IMPORTED = "AJOUTE";
var ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
var menuEntries = [{name: "Ajouter les événements à l'agenda", functionName: "importCalendar"}];
ss.addMenu("Agenda", menuEntries);
}
function importCalendar() {
var sheet = SpreadsheetApp.getActiveSheet();
var startcolumn = 1
var numcolumns = 30
var dataRange = sheet.getRange(startcolumn, 1, numcolumns, 6)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var column = data[i];
var titre = column[1];
var DateJ3 = column[2];
var DateJ10 = column[3];
var DateJ30 = column[4];
var DateJ60 = column[5];
var eventImported = column[6];
var setupInfo = ss.getSheetByName("agenda");
var calendarName = setupInfo.getRange("A1").getValue();
if (eventImported != EVENT_IMPORTED && titre != "") {
var cal = CalendarApp.openByName(calendarName);
cal.createAllDayEvent(titre, new Date(DateJ3));
cal.createAllDayEvent(titre, new Date(DateJ10));
cal.createAllDayEvent(titre, new Date(DateJ30));
cal.createAllDayEvent(titre, new Date(DateJ60));
sheet.getRange(startcolumn + i, 7).setValue(EVENT_IMPORTED);
SpreadsheetApp.flush();
}
}
}
Thank you in advance, i'm despair, i searched for hours but found nothing to help...
Related
Hi sorry this is probably a very newbie question, I am teaching myself and can't figure out how to google the answer to this one.
I have a loop in google sheets that goes through each field and updates some cells which pull info from a website that I then need to copy across into each row of the loop. It seems to be working through the loop okay however when trying to copy i cant get it into the correct row.
Im sure its something very simple hoping someone out there can point me in the right direction. I'm pretty sure its to do with the code at line 34 but can't figure out how to correct it.
function myLoop() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var Lsheet = ss.setActiveSheet(ss.getSheetByName('Loop'), true);
var rangeData = Lsheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var dataRange = Lsheet.getRange(2,1, lastRow-1, lastColumn-1);
var data = dataRange.getValues();
var len = data.length;
var i = 0
for (i; i < len; i++) {
var row = data[i];
var player = row[4]
var prop = row[6]
var number = row[7]
var overodds = row[8]
var underodds = row[9]
var overreq = row[10]
var underreq = row[11];
if(prop = "Points"){
ss.setActiveSheet(ss.getSheetByName('Stats'), true);
ss.getRange('B1').activate();
ss.getRange("B1").setValue(player);
ss.getRange('AB5').activate();
ss.setActiveSheet(ss.getSheetByName('Loop'), true);
ss.getRange(overreq).activate();
ss.getRange('Stats!AB5').copyTo(ss.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
ss.setActiveSheet(ss.getSheetByName('Stats'), true);
ss.getRange('AB6').activate();
ss.setActiveSheet(ss.getSheetByName('Loop'), true);
ss.getRange(underreq).activate();
ss.getRange('Stats!AB6').copyTo(ss.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
}
}
Modifications of the original code:
Corrected an error in line 24. In your original post, you are using = (assignment operator) to compare your variables. However, you want to use == (equality operator).
Added missing semicolons. Although not necessary, makes for good, consistent style and may prevent some errors.
Moved i declaration inside the for-loop. Makes for cleaner, safer code.
Modified for-loop body so that it uses ranges and setValue(), getValue() methods to copy data.
function myLoop() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var Lsheet = ss.setActiveSheet(ss.getSheetByName('Loop'), true);
var rangeData = Lsheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var dataRange = Lsheet.getRange(2,1, lastRow-1, lastColumn-1);
var data = dataRange.getValues();
var len = data.length;
var statsSheet = ss.getSheetByName('Stats');
var loopSheet = ss.getSheetByName('Loop');
for (var i = 0; i < len; i++) {
var row = data[i];
var player = row[4];
var prop = row[6];
var number = row[7];
var overodds = row[8];
var underodds = row[9];
var overreq = row[10];
var underreq = row[11];
if (prop == "Points") {
statsSheet.getRange('B1').setValue(player);
loopSheet.getRange(overreq).setValues(
statsSheet.getRange('AB5').getValues()
);
loopSheet.getRange(underreq).setValues(
statsSheet.getRange('AB6').getValues()
);
}
}
}
Could you try this:
var currentRow = ss.getActiveRange().getRow();
var currentCol = ss.getActiveRange().getColumn();
.
.
.
ss.getRange('State!AB5').copyTo(ss.getRange(currentRow+1, currentCol),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
I am working in Google Scripts in combination with Google spreadsheets. The idea is that I make a spreadsheet that is able to read out events in a calendar. I am able to read to through the sheet horizontally, however I want to be able to read through it vertically.
See the script I have so far below.
function caltest1() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 23; // First row of data to process
var numRows = 2; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 10);
var data = dataRange.getValues();
var cal = CalendarApp.getDefaultCalendar();
for (i in data) {
var row = data[i];
var tstart = new Date(row[1]);
var tstop = new Date(row[7]);
var title = row[0]; // First column
var desc = row[2]; // Second column
var guest = row[8];
var loc = row[9];
cal.createEvent(title, tstart, { description: desc, location: loc, sendInvites: true, guests: guest });
}
}
I'm working on a script that generates a calendar event from a sheet in a google spreadsheet. after debugging the code I come up with no errors but when I run the code I get this "Cannot call method 'setTitle' of null". I have been unsuccessful in my attempts in troubleshooting and do not fully understand the substance of this error. Can someone help me understand what I'm doing wrong here?
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Export Events",
functionName : "exportEvents"
}];
ss.addMenu("Calendar Actions", entries);
};
/**
* Export events from spreadsheet to calendar
*/
function exportEvents() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var headerRows = 389; // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getValues();
var calId = "somewhere#farfaraway.com";
var cal = CalendarApp.getCalendarById(calId);
for (i=0; i<data.length; i++) {
if (i < headerRows) continue; // Skip header row(s)
var row = data[i];
var date = new Date(row[11]); // column L
var title = row[9]; // Column J
var estimator = row[6];
var tstart = new Date(row[11]);
tstart.setDate(date.getDate());
tstart.setMonth(date.getMonth());
tstart.setYear(date.getYear());
var tstop = new Date(row[11]);
tstop.setDate(date.getDate());
tstop.setMonth(date.getMonth());
tstop.setYear(date.getYear());
var loc = row[10]; // Column K
var desc = row[13]; // Column N
var id = row[0]; // Column A == eventId
// Check if event already exists, update it if it does
try {
var calev = cal.getEventSeriesById(id);
}
catch (e) {
// do nothing - we just want to avoid the exception when event doesn't exist
}
if (estimator == "a person" && !calev) {
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}).getId();
row[0] = newEvent; // Update the data array with event ID
}
else {
calev.setTitle(title);
calev.setDescription(desc);
calev.setLocation(loc);
// event.setTime(tstart, tstop); // cannot setTime on eventSeries.
// ... but we CAN set recurrence!
var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
event.setRecurrence(recurrence, tstart, tstop);
}
debugger;
}
// Record all event IDs to spreadsheet
range.setValues(data);
}
I have a list of Google Email users in a google sheet(named Master, column E). Each user also has a blank sheet named after that email address (User#Whatever.com).
What I'm aiming to do it go through the user list, retrieve the calendar entries for today for each user, and enter them into their relevant sheet. So far I have the following code, but it's returning
TypeError: Cannot call method "getEventsForDay" of null. (line 15,
file "Calendar Update")
Code is below:
var employeeDataRange = SpreadsheetApp.getActiveSpreadsheet().getRange("Master!E2:E");
var employeeObjects = employeeDataRange.getValues();
for (var j=0; j<employeeObjects.length; j++) {
var cal = CalendarApp.getCalendarById(employeeObjects[j]);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(employeeObjects[j])
var today = new Date();
var events = CalendarApp.getCalendarById(cal).getEventsForDay(today);
for (var i=0;i<events.length;i++) {
var details=[[events[i].getTitle(), events[i].getStartTime(), events[i].getEndTime(), events[i].getAllTagKeys()]];
var row=i+1;
var range=sheet.getRange(row,1,1,4);
range.setValues(details);
}
}
}
Cheers,
Dan
Are you sure that you have all permissions to see those calendars and their events?
Moreover, you have this row
var cal = CalendarApp.getCalendarById(employeeObjects[j]);
where employeeObjects[j] is the calendar id and cal is the relative calendar.
Two rows after you have
var events = CalendarApp.getCalendarById(cal).getEventsForDay(today);
but cal is a calendar, not its id. Why you don't use cal.getEventsForDay(today)?
I Figured it out:
var calendar = SpreadsheetApp.getActiveSpreadsheet().getRange("Master!E2:E");
var employeeObjects = calendar.getValues();
for (var j=0; j<employeeObjects.length; j++) {
var cal = CalendarApp.getCalendarById(employeeObjects[j]);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(employeeObjects[j])
var today = new Date();
var events = CalendarApp.getCalendarById(employeeObjects[j]).getEventsForDay(today);
for (var i=0;i<events.length;i++) {
var details=[[events[i].getTitle(), events[i].getStartTime(), events[i].getEndTime(), events[i].getAllTagKeys()]];
var row=i+1;
var range=sheet.getRange(row,1,1,4);
range.setValues(details);
}
}
}
Here is my case.
A user fills a form for event booking, the submitted form is stored in a google spreadsheet which I have synced to a google calender so that it automatically sends the data to it.
Everything is working fine apart from the fact that event times could clash.
When customers book an event centre for let's say on 13/3/2015 T 10:00AM, if another user enters the same date and time, the entry should not be accepted.
To summarise it, I want to avoid a clash of events booking. Thank you all.
here is my script.
var calendarId = "mycalenderid";
//below are the column ids of that represents the values used in the spreadsheet (these are non zero indexed)
var startDtId = 9;
var endDtId = 10;
var titleId = 6;
var descId = 11;
var formTimeStampId = 1;
function getLatestAndSubmitToCalendar() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var lr = rows.getLastRow();
var startDt = sheet.getRange(lr,startDtId,1,1).getValue();
//set to first hour and minute of the day.
//startDt.setHours(0);
//startDt.setMinutes(00);
var endDt = sheet.getRange(lr,endDtId,1,1).getValue();
//set endDt to last hour and minute of the day
//endDt.setHours(23);
//endDt.setMinutes(59);
var subOn = "Submitted on:"+sheet.getRange(lr,formTimeStampId,1,1).getValue();
var desc = "Added by :"+sheet.getRange(lr,descId,1,1).getValue()+"\n"+subOn;
var title = sheet.getRange(lr,titleId,1,1).getValue()+"DIA";
createEvent(calendarId,title,startDt,endDt,desc);
}
function createEvent(calendarId,title,startDt,endDt,desc) {
var cal = CalendarApp.getCalendarById(calendarId);
var start = new Date(startDt);
var end = new Date(endDt);
var loc = 'Script Center';
var event = cal.createEvent(title, start, end, {
description : desc,
location : loc
});
};
Here's a pseudocode of what you're trying to do:
function findEvent(desiredDateTime)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getDataRange();
var data = range.getValues();
var lRow = range.getLastRow();
var flag = true;
var count = 0;
while (flag == true || count < lRow)
{
if (desiredDateTime >= data[count][startDtId] && desiredDateTime <= data[count][endDtId])
{
flag = false;
}
else
{
count++;
}
}
if (flag == true)
{
//Call function to add event
}else{
//Tell user desired date-time is not available.
//If you're asking for user's email address,
//simplest approach would be to send an email.
}
}
You might have to modify other bits and pieces of your code as well to accommodate this but it shouldn't be too hard. Hope this provides you with a certain direction to follow through.