I have a problem with my code. It seems fine but it doesn\'t work and I really don\'t know why. I tried everything.
I want sync my google excel with my google calen
You want to run each function for the case for editing an empty cell and the case for deleting a value of cell. If my understanding is correct, how about this workaround? I think that there may be several workarounds. So please think of this as one of them. In this workaround, it supposes that you are using onEdit(e). When the cell is changed, e of onEdit(e) is changed as follows.
In the case for editing a value to empty cell
e.value is included in e.e.oldValue is NOT included in e.In the case for overwriting a cell with a value by other value
e.value and e.oldValue are included in e.In the case for deleting a value from a cell with a value
e.value and e.oldValue are NOT included in e.Using above results, in order to run each function for the case for editing an empty cell and the case for deleting a value of cell, you can use the following sample script.
function onEdit(e) {
if (("value" in e) && !("oldValue" in e)) {
Logger.log("In the case for editing a value to empty cell")
}
if (!("value" in e) && !("oldValue" in e)) {
Logger.log("In the case for deleting a value from a cell with a value")
}
}
Of course, you can also use the following script.
function onEdit(e) {
if ((e.value != null) && (e.oldValue == null)) {
Logger.log("In the case for editing a value to empty cell")
}
if ((e.value == null) && (e.oldValue == null)) {
Logger.log("In the case for deleting a value from a cell with a value")
}
}
onEdit().If I misunderstand your question, I'm sorry.
The syntax errors are removed and modified your script. In this modified script,
if ((e.value != null) && (e.oldValue == null)) { script } is run.else if(e.oldValue!=undefined) { script } is run.else if((e.value == null) && (e.oldValue == null)) { script } is run.function ToCalendar(e) {
var ss = SpreadsheetApp.getActiveSheet();
var cal = CalendarApp.getCalendarById("myID");
var range = e.range;
var column = range.getColumn();
var row = range.getRow();
var day = ss.getRange(1,column);
var dayRange = day.getValues();
if ((e.value != null) && (e.oldValue == null)) { // When the empty cell is edited, this becomes true.
var date = new Date(dayRange);
cal.createAllDayEvent(ss.getRange(row,2).getValue(),date,{
description: e.value,//ss.getRange(row, column).getValue(),
})
// In your situation, this might not be required.
} else if(e.oldValue!=undefined) { // When the cell with a value is overwritten by a value, this becomes true.
//If we edit cell:
var events= cal.getEventsForDay(date,{search: e.oldValue});
var ev= events[0];
Logger.log(ev);
ev.deleteEvent();
cal.createAllDayEvent(ss.getRange(row,2).getValue(),date,{description: ss.getRange(row, column).getValue()})
} else if((e.value == null) && (e.oldValue == null)) { // When the value of cell with a value is removed, this becomes true.
// If we delete cell
var events = cal.getEvents(date, {
search: ss.getRange(row,2)
});
for (i in events){
events[i].deleteEvent();
}
}
}
It was confirmed that the specification of the event object is changed. The following modification was confirmed. This was mentioned by @I'-'I.
e.value and e.oldValue are NOT included in e.e.value and e.oldValue are included in e.
e.value is an object like {"oldValue":"deleted value"}.e.oldValue is a string like "deleted value".By this modification, when the value was removed from a cell with the value, this can be checked by the following script.
if (e.value.oldValue) {
// value was removed from cell.
} else {
// value is NOT removed from cell.
}