Multiple onEdit scripts but getting error on line 2 - google-apps-script

I can't figure out what I'm doing wrong. I'm still new at this, so there's plenty of places for mistakes. I'm trying to get two versions of the same script to run. When a box is checked, if it's in column F I want function 7R to run, but if the checked box is in column K, I want function 8R to run. I used threads with similar questions to try and combine the two scripts but keep getting an error in line 2.
There error I was getting is Missing ; before statement: Line 2
Here is what I have:
function onEdit(e) {
8R();
7R();
}
function 8R() {
//Get the sheet you want to work with.
var editrange = {
top : 2,
bottom : 260,
left : 11,
right : 11};
//getRow() and not getrow()
var thisrow = e.range.getRow();
if (thisrow < editrange.top || thisrow > editrange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn = e.range.getColumn();
if (thiscolumn < editrange.left || thiscolumn > editrange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX:
rangevalues
var range8 = sheet.getRange("K3:K90");
var range28 = sheet.getRange("M3:M90");
var range2values8 = range28.getValues();
var rangevalues8 = range8.getValues();
//Loops through range results
for (var i in rangevalues8) {
Logger.log("rangevalues8["+i+"]["+0+"] is:"+rangevalues8[i][0]);//Added
//Set the rules logic
if (rangevalues8[i][0] == true) { //Modified
//Set the cell
range2values8[i][0] += 1; //Directly add 1 to range2values
Logger.log(range2values8);//Added
}
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell8 = destination.getRange("I3:I90");
destCell8.setValues(range2values8);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
range8.setValues(cleaningcell8);
}
function 7R() {
//Get the sheet you want to work with.
var editrange = {
top : 2,
bottom : 260,
left : 6,
right : 6};
//getRow() and not getrow()
var thisrow7 = e.range.getRow();
if (thisrow7 < editrange.top || thisrow7 > editrange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn7 = e.range.getColumn();
if (thiscolumn7 < editrange.left || thiscolumn7 > editrange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX:
rangevalues
var range7 = sheet.getRange("F3:F90");
var range27 = sheet.getRange("H3:H90");
var range2values7 = range27.getValues();
var rangevalues7 = range7.getValues();
//Loops through range results
for (var i in rangevalues7) {
Logger.log("rangevalues7["+i+"]["+0+"] is:"+rangevalues7[i][0]);//Added
//Set the rules logic
if (rangevalues7[i][0] == true) { //Modified
//Set the cell
range2values7[i][0] += 1; //Directly add 1 to range2values
Logger.log(range2values7);//Added
}
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell7 = destination.getRange("I93:I180");
destCell7.setValues(range2values7);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell7 = cleaning.getRange("A3:A90").getValues();
range7.setValues(cleaningcell7);
}
Here is a link to a replica sheet of what I'm working on with all of the relevant information: https://docs.google.com/spreadsheets/d/1PWaWm7AryljOMd5Aq1O2RSADyRFNVZbUDqKO__SzW2w/edit?usp=sharing

Javascript variables and functions names cannot start with numbers. Replace 8R and 7R with a valid identifier, for example func8R and func7R.
Apart from that, you 8R and 7R functions try to access right away a e.range variable, but that is not defined anywhere. I'm assuming you're trying to read the onEdit event parameter, but you have to pass that down onto your functions and also define the parameter their, e.g.
function onEdit(e) {
f8R(e);
f7R(e);
}
function f8R(e) {
//... continue (remember to also declare on f7R)
If you want to manually test this function you have to fill in this e parameter, as the environment would do when you actually change something on the spreadsheet. I like to do that by writing another "caller" function, like this:
function testOnEdit() {
onEdit({range: SpreadsheetApp.getActiveSheet().getRange('F2')});
}

Finally was able to get the script working. First, I changed the names as Henrique suggested. Then got the onEdit block sorted out. Thanks everyone for the help and advice. I'd still be angry at my machine without you all.
function onEdit(e) {
//don't need entire range, just the column that was modified to figure out which function to call
var editColumn = e.range.getColumn();
//confirm edit was a box being checked before running the code (as this seems to run on ANY sheet edit)
if (e.oldValue === "false" && e.value === "TRUE") {
if (editColumn === 6)
func7R(e);
else if (editColumn === 11)
func8R(e);
}
}
function func8R(e) {
//Get the sheet you want to work with.
var editRange = {
top : 2,
bottom : 260,
left : 11,
right : 11};
//getRow() and not getrow()
var thisRow = e.range.getRow();
if (thisRow < editRange.top || thisRow > editRange.bottom) return;
//getColumn() and not getcolumn()
var thisColumn = e.range.getColumn();
if (thisColumn < editRange.left || thisColumn > editRange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
var range8 = sheet.getRange("K3:K90");
var range28 = sheet.getRange("M3:M90");
var range2values8 = range28.getValues();
var rangevalues8 = range8.getValues();
//Loops through range results
for (var i in rangevalues8) {
Logger.log("rangevalues8["+i+"]["+0+"] is:"+rangevalues8[i][0]);//Added
//Set the rules logic
if (rangevalues8[i][0] == true) { //Modified
//Set the cell
range2values8[i][0] += 1; //Directly add 1 to range2values
Logger.log(range2values8);//Added
}
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell8 = destination.getRange("I3:I90");
destCell8.setValues(range2values8);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
range8.setValues(cleaningcell8);
}
function func7R(e) {
//Get the sheet you want to work with.
var editRange = {
top : 2,
bottom : 260,
left : 6,
right : 6};
//getRow() and not getrow()
var thisrow7 = e.range.getRow();
if (thisrow7 < editRange.top || thisrow7 > editRange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn7 = e.range.getColumn();
if (thiscolumn7 < editRange.left || thiscolumn7 > editRange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
var range7 = sheet.getRange("F3:F90");
var range27 = sheet.getRange("H3:H90");
var range2values7 = range27.getValues();
var rangevalues7 = range7.getValues();
//Loops through range results
for (var i in rangevalues7) {
Logger.log("rangevalues7["+i+"]["+0+"] is:"+rangevalues7[i][0]);//Added
//Set the rules logic
if (rangevalues7[i][0] == true) { //Modified
//Set the cell
range2values7[i][0] += 1; //Directly add 1 to range2values
Logger.log(range2values7);//Added
}
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell7 = destination.getRange("I93:I180");
destCell7.setValues(range2values7);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell7 = cleaning.getRange("A3:A90").getValues();
range7.setValues(cleaningcell7);
}

Related

Running a function in apps script when a certain value in a range is changed to a specific Value

I have this function that I use to loop thru a spreadsheet and send emails depending on the value of the status Column. I'm currently using a trigger that runs the sendEmailFunction on change in the spreadsheet.
However, I would prefer to write a programmatic trigger, that runs this sendEmailFunction when the cell value in the statusCol Range is changed from blank -> Initiate. I have tried using the onEdit(e) method, but can't seem to get it to work. Thanks
function sendEmailFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheets()[0];
var totalRows = ss.getLastRow() -1;
var statusCol = ss.getRange(2, 16, totalRows, 1).getValues();
for (var i=0; i<statusCol.length; i++)
{
if(statusCol[i] == 'Email Sent' || statusCol[i] == null)
{
continue;
}
else
{
var col1 = ws.getRange(i+2, 1).getValue();
var col2 = ws.getRange(i+2, 2).getValue();
var status = ws.getRange(i+2, 16).getValue();
}
}
if (status == 'Initiate'){
MailApp.sendEmail('emailaddress#live.com', 'subject', 'body')
}
}

TIMEVALUE setting off a script

I'm trying to have TIMEVALUE set off a script. I have column P which is a Timer column that is subtracting a time from NOW to get a time value and then I am converting that to a TIMEVALUE in column O. When that value is either below or above certain values I want column N to then have a value so it would trigger my checkboxes script. But for some reason I can't get the TIMEVALUE to trigger. I tried to put it in another column and have the values CopyAndPasted Values Only into a column with that run on a time trigger of every minute, but apparently that doesn't count as an Edit or it's not reading the DisplayValue.
function onEdit(e) {
timeValue(e);
checkboxes(e);
rangerTime(e);
}
function rangerTime(e){
var editRow = e.range.getRow();
var editColumn = e.range.getColumn();
if (editColumn === 13 && e.value == "TRUE") {
const sh = e.range.getSheet();
sh.getRange(editRow, 25).setValue(sh.getRange(editRow, 25).getValue()+1);
sh.getRange(editRow, 13).setValue(" ")
}
}
function timeValue(e) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
var editRow = e.range.getRow();
var editColumn = e.range.getColumn();
if(editRow > 3) {
var rowRange = sheet.getRange("O" + editRow);
var kCell = sheet.getRange("K" + editRow);
var kValue = kCell.getValue();
var nCell = sheet.getRange("N" + editRow);
var kHasValue = kValue != "";
if(editColumn > 10) {
if(rowRange.getDisplayValue()<0.02 && !kHasValue){
nCell.setValue(1);
}
if (rowRange.getDisplayValue()>0.5 && !kHasValue){
nCell.setValue(1);
}
if(kHasValue) {
nCell.setValue(" ");
}
}
}
}
function checkboxes(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ui = SpreadsheetApp.getUi();
var names = ss.getRange("N4:N");
var namesValues = names.getValues();
var checkboxes = ss.getRange("M4:M");
var cbRows = checkboxes.getHeight();
var cbValues = checkboxes.getValues();
var newCBValues = new Array(cbRows);
for (var row = 0; row < cbRows; row++) {
newCBValues[row] = new Array(0);
if (namesValues[row] == "" || namesValues[row] == " ") {
newCBValues[row][0] = " ";
}else{
if (cbValues[row][0] === true) {
newCBValues[row][0] = true;
}else{
newCBValues[row][0] = false;
}
}
}
checkboxes.setValues(newCBValues);
}
This part is run on the every minute Time Trigger:
function CopyandPaste() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('AA4').activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
spreadsheet.getRange('O4').activate();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
spreadsheet.getRange('AA4:AA202').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
Column AA = TIMEVALUE(Column P)
From the question
I tried to put it in another column and have the values CopyAndPasted Values Only into a column with that run on a time trigger of every minute, but apparently that doesn't count as an Edit or it's not reading the DisplayValue.
You are right, only actions done by a user directly through the Google Sheets user interface will cause the edit trigger to be triggered.
One option is to make that your time-driven trigger besides changing the checkboxes also call the timeValue function but you have to refactor it or to mock the edit event object to pass it as the timeValue parameter.
Reference
https://developers.google.com/apps-script/guides/triggers

Sheets Run script on edit of a range of cells (False to True)

I have a script that needs to run when a sheet is edited. I need it to run anytime a box in column L (L2:L260) is checked. I originally wrote the script to run on a button push (check a number of boxes, click the button and it would run), but it can't be run by other people who need to be able to use it. The script originally worked great when it was click the button, but once I added in the onEdit stuff...it completely stopped working. Here's what I have:
function onEdit(e) {
//Get the sheet you want to work with.
var editrange = {
top : 2,
bottom : 260,
left : 11,
right : 11};
var thisrow = e.range.getrow();
if (thisrow < editrange.top || thisrow > editrange.bottom)
return;
var thiscolumn = e.range.getcolumn();
if (thiscolumn < editrange.left || thiscolumn > editrange.right)
return;
var ss = e.range.getSheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
var range8 = sheet.getRange("K3:K90");
var range28 = sheet.getRange("M3:M90");
var range2values8 = range28.getValues();
var rangevalues8 = range8.getValues();
//Loops through range results
for (var i in rangevalues8) {
// for (var j in rangevalues) {
Logger.log("rangevalues8["+i+"]["+0+"] is:"+rangevalues8[i][0]);//Added
//Set the rules logic
if (rangevalues8[i][0] == true) { //Modified
//Set the cell
range2values8[i][0] += 1; //Directly add 1 to range2values
Logger.log(range2values8);//Added
}
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell8 = destination.getRange("I183:I270");
destCell8.setValues(range2values8);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
range8.setValues(cleaningcell8);
}
Thanks for any help I can get!
After review of your code I guess the getcloumn() you've edited in an also erroneous method getcolumn(), the
var ss = e.range.getSheet();
var sheet = ss.getSheetByName("Responsible");
and the
e.range.getrow()
seems to be the major problems
correct syntax is:
getColumn();
getRow();
and you need to use the function getSheetByName(name) out from a spreadsheet object not a sheet object.
Here is the corrected code:
function onEdit(e) {
//Get the sheet you want to work with.
var editrange = {
top: 2,
bottom: 260,
left: 11,
right: 11
};
//getRow() and not getrow()
var thisrow = e.range.getRow();
if (thisrow < editrange.top || thisrow > editrange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn = e.range.getColumn();
if (thiscolumn < editrange.left || thiscolumn > editrange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
var range8 = sheet.getRange("K3:K90");
var range28 = sheet.getRange("M3:M90");
var range2values8 = range28.getValues();
var rangevalues8 = range8.getValues();
//Loops through range results
for (var i in rangevalues8) {
Logger.log("rangevalues8[" + i + "][" + 0 + "] is:" + rangevalues8[i][0]); //Added
//Set the rules logic
if (rangevalues8[i][0] == true) { //Modified
//Set the cell
range2values8[i][0] += 1; //Directly add 1 to range2values
Logger.log(range2values8); //Added
}
}
//copy new information
var destination = ss.getSheetByName('Compiled Data'); //whatever page
var destCell8 = destination.getRange("I183:I270");
destCell8.setValues(range2values8);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
range8.setValues(cleaningcell8);
}

How do I pull a Row from an Array in Google apps script Google sheets

My spreadsheet is composed of a main sheet that is populated using a form plus several other sheets for the people who work with the responses submitted through the form. A script delegates the form responses to these other sheets depending on the type of item described in the response.
The problem is, when Person A deletes an item from their respective sheet, it doesn't delete in the main sheet.
My idea is that when you type a set password into the corresponding cell in row 'Q' in Person A's sheet, it matches the item by timestamp to the original form submission and deletes both the version of the item in Person A's sheet as well as the main sheet. However, I can't figure out what to set the range to to get it to point to the row in the array. Everything I have tried has sent back "undefined" in the debugger and won't delete anything. I think the problem is that I don't know how to get the row from the array that I have made. See my code below:
function onEdit() {//copies edited items from individual selector sheets back onto main spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var actSheet = ss.getActiveSheet();
var responseSheet = ss.getSheetByName("Item Request");
var actCell = actSheet.getActiveCell();
var actRow = actCell.getRow();
var actVal = actCell.getValue();
var actLoc = actCell.getA1Notation();
var last = actSheet.getLastRow();
var respLast = responseSheet.getLastRow();
var dataA = responseSheet.getRange(1, 1, respLast, 1).getValues(); //compiles an array of data found in column A through last row in response sheet
var tstamp1 = actSheet.getRange(actCell.getRow(), 1);
var tsVal1 = tstamp1.getValue();
var colEdit = actCell.getColumn();
//===========THIS IS WHERE I'M STUCK=======================
if ((actVal == "p#ssword") && (colEdit == 17)) {
for (i = 1; i < dataA.length; i++) {
if (dataA[i][0].toString == tsVal1.toString()) {
responseSheet.deleteRow(i + 1);
actSheet.deleteRow(actRow);
break;
}
}
}
else if (colEdit == 15) { //checks the array to see if the edit was made to the "O" column
for (i = 1; i < dataA.length; i++) {//checking for timestamp match and copies entry
if (dataA[i][0].toString() == tsVal1.toString()) {
var toEdit = responseSheet.getRange(i + 1, 16);
toEdit.setValue(actVal);
}
}
}
else if (colEdit == 16) { // checks the array to see if the edit was made in the "P" column
for (i = 1; i < dataA.length; i++) {//checking for timestamp match and copies entry
if (dataA[i][0].toString() == tsVal1.toString()) {
var toEdit = responseSheet.getRange(i + 1, 17);
toEdit.setValue(actVal);
}
}
}
else {return;}
}//end onEdit
I don't believe these are proper commands delRow.deleteRow();actCell.deleteRow(); Take a look at the documentation;
Okay I rewrote that function for you a bit but I'm stilling wondering about a couple of lines.
function onEdit(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var actSheet = ss.getActiveSheet();
var responseSheet = ss.getSheetByName("Item Request");
var actCell = actSheet.getActiveCell();
var actRow = actCell.getRow();
var actVal = actCell.getValue();
var colEdit = actCell.getColumn();
var respLast = responseSheet.getLastRow();
var dataA = responseSheet.getRange(1, 1, respLast, 1).getValues();
var tstamp1 = actSheet.getRange(actRow, 1);
var tsVal1 = tstamp1.getValue();
for(var i=0;i<dataA.length;i++)
{
if(new Date(dataA[i][0]).valueOf()==new Date(tsVal1).valueOf())
{
if (actVal=="p#ssword" && colEdit==17)
{
responseSheet.deleteRow(i + 1);
actSheet.deleteRow(actRow);
}
else if(colEdit==15)
{
var toEdit = responseSheet.getRange(i + 1, 16);//?
toEdit.setValue(actVal);//?
}
else if (colEdit == 16)
{
var toEdit = responseSheet.getRange(i + 1, 17);//?
toEdit.setValue(actVal);//?
}
}
}
}
Can you explain the function of the lines with question marked comments?

Can't delete an event in my google calendar using scripts

I made a spreadsheet where I can easily manage my events in my google Calendar.
In the spreadsheet Row 1 has the dates, Row 2 has the specifics of the appointment and Row 3 saves the eventId of the event created by the script.
Every week starts on a new row.
The script works like it should and looks like this:
function CalenderUpdate() {
var calId = "xxxxxxxxxxxxxxxx#group.calendar.google.com";
var descr = "";
var date
var titel
var sheet = SpreadsheetApp.getActiveSheet();
var Afspraak = sheet.getRange(2,1,3,5).getValues();
var cal = CalendarApp.getCalendarById(calId);
for (j=2;j<9;j=j+3){
Afspraak = sheet.getRange(j,1,3,5).getValues();
for (i=0; i<5; i++){
descr ="";
date = Afspraak[0][i];
titel = Afspraak[1][i];
if (titel == "A"){ descr = "Late 14:00 - 21:00";}
if (titel == "M"){ descr = "Early 6:00 - 14:00";}
if (Afspraak[1][i] != ""){
var event = cal.createAllDayEvent(titel,date,{description:descr});
sheet.getRange(j+2,i+1).setValue(event.getId());
}
}
}
}
Now I want a new function. If I edit something in the schedule, I want it to update in my calendar. So I added a function onEdit that would delete the event if one created on that day, and if necessary create a new.
I started with this code to delete the event if something is edited. But it doesn't delete the appointment and I can't figure out why.
function onEdit(e){
var ss = SpreadsheetApp.getActiveSheet();
var range = e.range;
var rij = range.getRow();
var col = range.getColumn();
var afspraakId = ss.getRange(rij+1,col).getValue();
if (rij % 3 == 0) { ss.getRange(1,1).setValue(afspraakId); }
var calId = "xxxxxxxxxxxxxxx#group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
var event = cal.getEventSeriesById(id);
event.deleteEventSeries();
}
Hope someone can help me?
EDIT:
I changed the onEdit(e) to onEditInstallable(e) but the script never get triggered.
Even if I add a trigger by the menu
What do I do wrong?
EDIT2:
I've done some editing on my script. This is the end result:
function test_onEdit() {
onEditInstallable({
user : Session.getActiveUser().getEmail(),
source : SpreadsheetApp.getActiveSpreadsheet(),
range : SpreadsheetApp.getActiveSpreadsheet().getActiveCell(),
value : SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getValue(),
authMode : "LIMITED"
});
}
function onEditInstallable(e){
var ss = SpreadsheetApp.getActiveSheet();
var range = e.range;
var rij = range.getRow();
var col = range.getColumn();
if (rij % 3 == 0 && col < 8) {
var cal = CalendarApp.getCalendarById("xxxxxxxxxxxxxxxxxx#group.calendar.google.com");
var event
var geg = ["","",""];
var descr ="";
for (i = 0 ; i < 3 ; i++){ geg[i] = ss.getRange(rij-1+i,col).getValue();}
if (geg[2] != ""){
event = cal.getEventSeriesById(geg[2]);
event.deleteEventSeries();
ss.getRange(rij+1,col).setValue("");
}
if (geg[1] != ""){
event = cal.createAllDayEvent(geg[1],geg[0],{description:descr});
ss.getRange(rij+1,col).setValue(event.getId());
}
}
}
The script works if I run the test_OnEdit. But the onEditInstallable doesn't trigger automatic? I'm an amateur in programming and I don't understand much of this page. Hope someone can help me figure this out.
Your second edit is a good approach, I'd suggest to add a couple of logs in the script to check the condition values.
I'd rather use the comments for this but it would definitely be too long and hard to read so I use the 'answer' format.
function onEditInstallable(e){
var ss = SpreadsheetApp.getActiveSheet();
var range = e.range;
var rij = range.getRow();
var col = range.getColumn();
Logger.log('rij = '+rij+' col = '+col);
if (rij % 3 == 0 && col < 8) {
var cal = CalendarApp.getCalendarById("xxxxxxxxxxxxxxxxxx#group.calendar.google.com");
var event
var geg = ["","",""];
var descr ="";
for (i = 0 ; i < 3 ; i++){ geg[i] = ss.getRange(rij-1+i,col).getValue();}
Logger.log('geg[2] = '+geg[2]);
if (geg[2] != ""){
event = cal.getEventSeriesById(geg[2]);
event.deleteEventSeries();
ss.getRange(rij+1,col).setValue("");
}
if (geg[1] != ""){
event = cal.createAllDayEvent(geg[1],geg[0],{description:descr});
ss.getRange(rij+1,col).setValue(event.getId());
}
}
}
I used this last piece of code and linked the test_onEdit script to a button in the spreadsheet. On the first time testing it from there Google gave a pop-up asking for extra authorizations. From then on everything works like a charm.
Now my spreadsheet automatically edits my agenda if I change something in the spreadsheet.
Thanks for the help everyone.
function test_onEdit() {
onEditInstallable({
user : Session.getActiveUser().getEmail(),
source : SpreadsheetApp.getActiveSpreadsheet(),
range : SpreadsheetApp.getActiveSpreadsheet().getActiveCell(),
value : SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getValue(),
authMode : "LIMITED"
});
}
function onEditInstallable(e){
var ss = SpreadsheetApp.getActiveSheet();
var range = e.range;
var rij = range.getRow();
var col = range.getColumn();
Logger.log('rij = '+rij+' col = '+col);
if (rij % 3 == 0 && col < 8) {
var cal = CalendarApp.getCalendarById("xxxxxxxxxxxxxxxxxx#group.calendar.google.com");
var event
var geg = ["","",""];
var descr ="";
for (i = 0 ; i < 3 ; i++){ geg[i] = ss.getRange(rij-1+i,col).getValue();}
Logger.log('geg[2] = '+geg[2]);
if (geg[2] != ""){
event = cal.getEventSeriesById(geg[2]);
event.deleteEventSeries();
ss.getRange(rij+1,col).setValue("");
}
if (geg[1] != ""){
event = cal.createAllDayEvent(geg[1],geg[0],{description:descr});
ss.getRange(rij+1,col).setValue(event.getId());
}
}
}
Try this:
function deleteEvent(event) {
if (typeof event != 'undefined') {
Logger.log("Deleting event %s", event.getTitle())
event.deleteEvent()
}
}
function deleteEvents(eventCal,startTime,endTime,title){
// var oldEvents = eventCal.getEvents(startTime, endTime, {search: title});
var oldEvents = eventCal.getEvents(startTime, endTime);
Logger.log("oldEvents %s", oldEvents);
for (var j = 0; j < oldEvents.length; j++){
Logger.log("oldEvents[j] %s", oldEvents[j]);
deleteEvent(oldEvents[j]);
}
}