I am brand new to Java Script and was trying to condense my code down by a lot. Here is my original code:
function createColoredCalendarEvent() {
let events = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("events").getValues();
events.forEach(function(e){
if (e[7] === "Weekly" && e[5].includes(":") === true) {
CalendarApp.getCalendarById("59a4dd9d635128eacd6e298dd977b68f7d8ee9aa9b779e9a58cc3c745eec23a2@group.calendar.google.com").createEvent(
e[0].concat(", ", e[3], ", ", e[6]),
new Date(e[5]),
new Date(e[5]),
{description: e[1].concat(", last compleated: ", e[4])},
).setColor(CalendarApp.EventColor.PALE_BLUE);
} else if (e[7] === "Monthly" && e[5].includes(":") === true) {
CalendarApp.getCalendarById("59a4dd9d635128eacd6e298dd977b68f7d8ee9aa9b779e9a58cc3c745eec23a2@group.calendar.google.com").createEvent(
e[0].concat(", ", e[3], ", ", e[6]),
new Date(e[5]),
new Date(e[5]),
{description: e[1].concat(", last compleated: ", e[4])},
).setColor(CalendarApp.EventColor.GREEN);
} else if (e[7] === "Quarterly" && e[5].includes(":") === true) {
CalendarApp.getCalendarById("59a4dd9d635128eacd6e298dd977b68f7d8ee9aa9b779e9a58cc3c745eec23a2@group.calendar.google.com").createEvent(
e[0].concat(", ", e[3], ", ", e[6]),
new Date(e[5]),
new Date(e[5]),
{description: e[1].concat(", last compleated: ", e[4])},
).setColor(CalendarApp.EventColor.MAUVE)
} else if (e[7] === "Biannually" && e[5].includes(":") === true) {
CalendarApp.getCalendarById("59a4dd9d635128eacd6e298dd977b68f7d8ee9aa9b779e9a58cc3c745eec23a2@group.calendar.google.com").createEvent(
e[0].concat(", ", e[3], ", ", e[6]),
new Date(e[5]),
new Date(e[5]),
{description: e[1].concat(", last compleated: ", e[4])},
).setColor(CalendarApp.EventColor.BLUE);
} else if (e[7] === "Annually" && e[5].includes(":") === true) {
CalendarApp.getCalendarById("59a4dd9d635128eacd6e298dd977b68f7d8ee9aa9b779e9a58cc3c745eec23a2@group.calendar.google.com").createEvent(
e[0].concat(", ", e[3], ", ", e[6]),
new Date(e[5]),
new Date(e[5]),
{description: e[1].concat(", last compleated: ", e[4])}
).setColor(CalendarApp.EventColor.GRAY);
} else if (e[5] === "N/A" || e[5] === "12/30/1899" || e[5].includes(":") === false) {
console.log(e[0], " is not an applicable event. This event was not added to your Calendar")
}
})
}
As you can see it can be condensed a lot, I am just too new to this language to know the correct syntax and methods.
This was my attempt at condensing it (that failed quite badly)
function createColoredCalendarEvent() {
let events = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("events").getValues();
events.forEach(function(e){
createCalendarEvent.apply(null, e).setColor(CalendarApp.EventColor.RED)
})
}
function createCalendarEvent(a, b, c, d, e, f, g){
CalendarApp.getCalendarById("59a4dd9d635128eacd6e298dd977b68f7d8ee9aa9b779e9a58cc3c745eec23a2@group.calendar.google.com").createEvent(
{a}.concat(", ", {d}, ", ", {g}),
new Date({f}),
new Date({f}),
{description: {b}.concat(", last completed: ", {e})},
)
}
I keep getting an error that says
“TypeError: {(intermediate value)}.concat is not a function”.
How can I effectively condense my code?
PonderingGoneAmuck is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.